标签:bool 插入 基本实现 socket rtl enabled noi source 完成
通过WeakReference和ReferenceQueue做针对需要手动释放的资源的侦测
ServerBootstrap b =new ServerBootstrap();
b.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 2048)
.handler(new LoggingHandler(LogLevel.DEBUG))
.childHandler(new ChildChannelHandler());
2017-01-19 10:04:49 [ nioEventLoopGroup-1-0:1628830 ] - [ ERROR ] LEAK: ByteBuf.release() was not called before it's garbage-coll...
2017-01-19 10:35:59 [ nioEventLoopGroup-1-0:665092 ] - [ ERROR ] LEAK: ByteBuf.release() was not called before it's garbage-collected. See http://netty.io/wiki/reference-counted-objects.html for more information.
Recent access records: 5
#5:
io.netty.buffer.AdvancedLeakAwareByteBuf.readBytes(AdvancedLeakAwareByteBuf.java:435)
[ ERROR ] LEAK:
// ResourceLeakDetector
protected void reportTracedLeak(String resourceType, String records) {
logger.error(
"LEAK: {}.release() was not called before it's garbage-collected. " +
"See http://netty.io/wiki/reference-counted-objects.html for more information.{}",
resourceType, records);
}
private void reportLeak() {
if (!logger.isErrorEnabled()) {
clearRefQueue();
return;
}
// Detect and report previous leaks.
for (;;) {
@SuppressWarnings("unchecked")
DefaultResourceLeak ref = (DefaultResourceLeak) refQueue.poll(); // 为什么能拿到?什么时候 放进去的?是weakreference回收过程中放进去的,相当于GC过程让你插入hook。那为什么被GC了还有资源泄露呢?这个问题其实是这样的,泄露是池化内存等那些需要手动释放资源。
if (ref == null) {
break;
}
if (!ref.dispose()) { // return allLeaks.remove(this); 所以当有人显式释放过,那么此处就返回false 就不会往下走report了
continue;
}
String records = ref.toString();
if (reportedLeaks.putIfAbsent(records, Boolean.TRUE) == null) {
if (records.isEmpty()) {
reportUntracedLeak(resourceType);
} else {
reportTracedLeak(resourceType, records);
}
}
}
}
基本实现原理是:
此处资源泄露,是指那种需要手动释放的资源,因为引用他的对象已经不在程序逻辑中使用了,那么最终会被GC回收,但是那种需要手动释放的资源不显式释放就泄露了。比如内存池,比方说里面有5个杯子,你用的buf指向一个杯子被你占着,你不用时没显式告诉内存池说这个杯子不用了,那么就一直占着,但是buf对象不用了会被GC回收,那么此时内存池资源就泄露了。
[netty4][netty-common]netty之ResourceLeakDetector的使用与实现
标签:bool 插入 基本实现 socket rtl enabled noi source 完成
原文地址:https://www.cnblogs.com/simoncook/p/11980170.html