博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NETTY 4的内存管理(一)
阅读量:5997 次
发布时间:2019-06-20

本文共 2146 字,大约阅读时间需要 7 分钟。

http://www.infoq.com/news/2013/11/netty4-twitter

Reducing GC pressure and memory bandwidth consumption

A problem was Netty 3’s reliance on the JVM’s memory management for buffer allocations. Netty 3 creates a new heap buffer whenever a new message is received or a user sends a message to a remote peer. This means a ‘new byte[capacity]’ for each new buffer. These buffers caused GC pressure and consumed memory bandwidth: allocating a new byte array consumes memory bandwidth to fill the array with zeros for safety. However, the zero-filled byte array is very likely to be filled with the actual data, consuming the same amount of memory bandwidth. We could have reduced the consumption of memory bandwidth to 50% if the Java Virtual Machine (JVM) provided a way to create a new byte array which is not necessarily filled with zeros, but there’s no such way at this moment.

To address this issue, we made the following changes for Netty 4.

Netty版本3不管是接受或发送消息都会创建一个新的堆缓冲,过多就导致GC压力和消耗内存带宽(zero-filled(补零)字节数组占用与实际数据同样的带宽)

1、Netty4去除了事件对象(removal of event objects),版本4为不同的事件类型定义了不同的方法

Netty3

class Before implements ChannelUpstreamHandler {  void handleUpstream(ctx, ChannelEvent e) {    if (e instanceof MessageEvent) { ... }    else if (e instanceof ChannelStateEvent) { ... }      ...    }}

Netty4

class After implements ChannelInboundHandler {  void channelActive(ctx) { ... }  void channelInactive(ctx) { ... }  void channelRead(ctx, msg) { ... }  void userEventTriggered(ctx, evt) { ... }  ...}

2、Netty 4 引入了一个新的接口,提供缓冲区池(a buffer pool implementation via that interface and is a pure Java variant of  (另参考), which implements  and .)Netty不再需要补零填充buffer。不过有了自己的内存配置器,不能再依赖GC,必须小心泄露(Now that Netty has its own memory allocator for buffers, it doesn’t waste memory bandwidth by filling buffers with zeros. However, this approach opens another can of worms—reference counting. Because we cannot rely on GC to put the unused buffers into the pool, we have to be very careful about leaks. Even a single handler that forgets to release a buffer can make our server’s memory usage grow boundlessly.)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/treeliang/p/3598693.html

你可能感兴趣的文章
网络知识汇总(1)-朗文和牛津英语词典网址
查看>>
选择排序(C语言实现) 分类: 数据结构 2015-...
查看>>
Quartz_1_简单编程式任务调度使用(SimpleTrigger)
查看>>
web api 初体验 解决js调用跨域问题
查看>>
centos 安装docker
查看>>
互联网架构的三板斧
查看>>
阿里巴巴MySQL DBA面试题答案[转]
查看>>
JS乘法口诀表(一行代码)
查看>>
网络、会话建立与信任
查看>>
系统级性能分析工具perf的介绍与使用
查看>>
spring remoting源码分析--Hessian分析
查看>>
phpMyAdmim和Yii 连接Mysql报错。
查看>>
shell语法简单介绍
查看>>
MyEclipse 6.5 代码自动提示功能配置教程
查看>>
Java程序员面试失败的5大原因
查看>>
我认识的python(5)
查看>>
Promise实现
查看>>
报表性能优化
查看>>
js设计模式--迭代器模式
查看>>
Python 学习笔记之——用 sklearn 对数据进行预处理
查看>>