java的DirectBuffer源码分析(主要是DirectBuffer的资源分配和回收)

来源:互联网 发布:女装淘宝货源 编辑:程序博客网 时间:2024/06/01 09:38
可以看到淘宝的一个说明:http://www.tbdata.org/archives/801

Java 2 SE 6 doc :

Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer’s content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system’s native I/O operations.

DirectBuffer通过免去中间交换的内存拷贝, 提升IO处理速度;

Java 2 SE 6 doc :

The contents of direct buffers may reside outside of the normal garbage-collected heap, and so their impact upon the memory footprint of an application might not be obvious.

DirectBuffer在-XX:MaxDirectMemorySize=xxM大小限制下[1], 使用Heap之外的内存, GC对此”无能为力”[2] ,也就意味着规避了在高负载下频繁的GC过程对应用线程的中断影响.
因此, 当系统应用场景满足:


大量原生类型数据使用
频繁IO操作
系统处理响应速度要求快且稳定
典型场景是网络数据传输, 可考虑合理应用DirectBuffer.


那么,DirectBuffer是怎么实现这些的呢:
Java代码  收藏代码
  1. DirectByteBuffer(int paramInt)  
  2.   {  
  3.     super(-10, paramInt, paramInt, false);  
  4.     Bits.reserveMemory(paramInt);  
  5.     int i = Bits.pageSize();  
  6.     long l = 0L;  
  7.     try  
  8.     {  
  9.       l = unsafe.allocateMemory(paramInt + i); //这个是分配内存  
  10.     }  
  11.     catch (OutOfMemoryError localOutOfMemoryError)  
  12.     {  
  13.       Bits.unreserveMemory(paramInt);  
  14.       throw localOutOfMemoryError;  
  15.     }  
  16.     unsafe.setMemory(l, paramInt + i, 0);  
  17.     if (l % i != 0L)  
  18.       this.address = (l + i - (l & i - 1));  
  19.     else  
  20.       this.address = l;  
  21.     this.cleaner = Cleaner.create(thisnew Deallocator(l, paramInt, null));//这个是负责回收资源。  
  22.     this.viewedBuffer = null;  
  23.   }  



Java代码  收藏代码
  1. public native long allocateMemory(long paramLong);  

allocateMemory 是一个本地方法,功能就是做一些参数检查和字节对齐,然后使用系统调用 malloc 申请一块内存。

从上面的分析也可以看出,创建一个 DirectByteBuffer 对象代价还是很大的,因为 malloc 系统调用代价就很大,需要 60 ~ 100 条 CPU 指令(一个 new Object() 只需要不足 10 条机器指令)。

注意,这个是操作系统直接分配的内存,java的垃圾回收是管不了的。


那是如何实现资源回收的呢。
Java代码  收藏代码
  1. this.cleaner = Cleaner.create(thisnew Deallocator(l, paramInt, null));  

cleaner 的类型是Cleaner,Cleaner是PhantomReference(俗称的幽灵引用)的子类。当整个对象被回收的时候,幽灵引用会被java的虚拟机加到一个队列里去,ReferenceQueue。Reference有一个静态内部类ReferenceHandler。
Java代码  收藏代码
  1. private static class ReferenceHandler extends Thread  

然后还有个静态代码块:

Java代码  收藏代码
  1. static  
  2.  {  
  3.    Object localObject1 = Thread.currentThread().getThreadGroup();  
  4.    for (Object localObject2 = localObject1; localObject2 != null; localObject2 = ((ThreadGroup)localObject1).getParent())  
  5.      localObject1 = localObject2;  
  6.    localObject2 = new ReferenceHandler((ThreadGroup)localObject1, "Reference Handler");  
  7.    ((Thread)localObject2).setPriority(10);  
  8.    ((Thread)localObject2).setDaemon(true);  
  9.    ((Thread)localObject2).start();  
  10.  }  


也就是说虚拟机已启动,ReferenceHandler 这个线程就启动了,那ReferenceHandler 会做什么呢
Java代码  收藏代码
  1. public void run() {  
  2.         for (;;) {  
  3.   
  4.         Reference r;  
  5.         synchronized (lock) {  
  6.             if (pending != null) {  
  7.             r = pending;  
  8.             Reference rn = r.next;  
  9.             pending = (rn == r) ? null : rn;  
  10.             r.next = r;  
  11.             } else {  
  12.             try {  
  13.                 lock.wait();  
  14.             } catch (InterruptedException x) { }  
  15.             continue;  
  16.             }  
  17.         }  
  18.   
  19.         // Fast path for cleaners  
  20.         if (r instanceof Cleaner) {  
  21.             ((Cleaner)r).clean();  
  22.             continue;  
  23.         }  
  24.   
  25.         ReferenceQueue q = r.queue;  
  26.         if (q != ReferenceQueue.NULL) q.enqueue(r);  
  27.         }  
  28.     }  
  29.     }  


可以看到其中有:
Java代码  收藏代码
  1. if (r instanceof Cleaner) {  
  2.             ((Cleaner)r).clean();  
  3.             continue;  
  4.         }  

就是会执行幽灵引用的clean方法。

在clean方法执行的其实是:
Java代码  收藏代码
  1. this.thunk.run();  

thunk是在构造方法传进去的。对于DirectByteBuffer来说,他的thunk是Deallocator:
Java代码  收藏代码
  1. this.cleaner = Cleaner.create(thisnew Deallocator(l, paramInt, null))  


Deallocator的run方法会做什么事情呢:
Java代码  收藏代码
  1. public void run() {  
  2.         if (address == 0) {  
  3.         // Paranoia  
  4.         return;  
  5.         }  
  6.         unsafe.freeMemory(address);  
  7.         address = 0;  
  8.         Bits.unreserveMemory(capacity);  
  9.     }  


Java代码  收藏代码
  1. public native void freeMemory(long paramLong);  


可以看到这个也是个native方法。这里就会把资源回收。


有些人问为什么不用finalize去做资源回收,搞个幽灵索引,搞怎么麻烦。主要是因为java的
finalize不是很安全。如果finalize再搞一个对象本身的强引用,那么这个对象就永远不会回收掉了。但是幽灵引用不一样,幽灵引用起作用在对象的finalize之后,也就是说这个对象这个时候已经回收了。这样会更安全。
原创粉丝点击