`
lixjluck
  • 浏览: 101550 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

DirectByteBuffer

    博客分类:
  • nio
阅读更多

DirectByteBuffer特性说明

 

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处理速度;也就是常说的zero-copy

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过程对应用线程的中断影响.
  • 如果系统没有指定-XX:MaxDirectMemorySize,默认通-Xmx大小
  •        //directMemory  ,默认64M
           String s = (String)     properties.remove("sun.nio.MaxDirectMemorySize");
            if (s != null) if (s.equals("-1")) {
                directMemory = Runtime.getRuntime().maxMemory();
            } else {
                long l = Long.parseLong(s);
                if (l > -1L) directMemory = l;
            }
     

DirectByteBuffer如何内存回收

实现了一个Cleaner

 

cleaner = Cleaner.create(this, new Deallocator(base, size, cap));

 

 Deallocator,实现内存回收 

            if (address == 0) {
                // Paranoia
                return;
            }
            unsafe.freeMemory(address);
            address = 0;
            Bits.unreserveMemory(size, capacity);
        }
 

在什么时候被调用?

看看Reference里的奥秘,它内部有一个Thread ReferenceHandler,它的run 方法会调用Cleaner.clean

 

//截取部分代码
                Reference r;
                synchronized (lock) {
                    if (pending != null) {
                        r = pending;
                        Reference rn = r.next;
                        pending = (rn == r) ? null : rn;
                        r.next = r;
                    } else {
                        try {
                            lock.wait();
                        } catch (InterruptedException x) { }
                        continue;
                    }
                }

                // Fast path for cleaners
                if (r instanceof Cleaner) {
                    ((Cleaner)r).clean();
                    continue;
                }
 

 

DirectByteBuffer如何实现zero-copy

sun.nio.ch.IOUtil

 

    static int read(FileDescriptor filedescriptor, ByteBuffer bytebuffer, long l, NativeDispatcher nativedispatcher, Object obj)
        throws IOException
    {
        ByteBuffer bytebuffer1;
        if(bytebuffer.isReadOnly())
            throw new IllegalArgumentException("Read-only buffer");
        if(bytebuffer instanceof DirectBuffer)
            return readIntoNativeBuffer(filedescriptor, bytebuffer, l, nativedispatcher, obj);
        bytebuffer1 = null;
        int j;
        bytebuffer1 = Util.getTemporaryDirectBuffer(bytebuffer.remaining());
        int i = readIntoNativeBuffer(filedescriptor, bytebuffer1, l, nativedispatcher, obj);
        bytebuffer1.flip();
        if(i > 0)
            bytebuffer.put(bytebuffer1);
        j = i;
        Util.offerFirstTemporaryDirectBuffer(bytebuffer1);
        return j;
        Exception exception;
        exception;
        Util.offerFirstTemporaryDirectBuffer(bytebuffer1);
        throw exception;
    }
    
NativeDispatcher的实现类SocketDispatcher的read方法是native

   static native int read0(FileDescriptor filedescriptor, long l, int i) throws IOException;

 

DirectByteBuffer用在什么地方

 

  • jetty用来处理和socket通信使用了DirectByteBuffer

 

附录:

-XX:MaxDirectMemorySize

This option specifies the maximum total size of java.nio (New I/O package) direct buffer allocations.

Format

-XX:MaxDirectMemorySize=size[g|G|m|M|k|K]

Example

java -XX:MaxDirectMemorySize=2g myApp

Default Value

    The default value is zero, which means the maximum direct memory is unbounded.

 

 

参考:http://www.tbdata.org/archives/801

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics