NIO direct buffers release method memory

Create a heap buffer and direct buffers

allocateDirect (int capacity):. allocate a new new direct byte buffer location of the buffer will be zero, which limits their capacity, which mark is uncertain whether or not it has a backing array, which is labeled. not sure.

allocate (int capacity): a new non-allocated buffer location directly new byte buffer is 0, which limits its capacity, and it is uncertain which tag having a backing array, and its array bias. shift amount will be zero.

If the byte buffer is direct byte buffer, then the JVM will try to execute on the machine direct byte buffer I / O operations, i.e. directly on the kernel space principle accessible to improve operating efficiency. The efficiency of operation that is, before each call to the operating system I / O operations on or after, the JVM will avoid copying the contents of the buffer to the intermediate buffer, or copy the content from the intermediate buffer, thus saving a step.

Time cost required for memory allocation and release by the allocateDirect () Returns the buffer is generally higher than non-direct buffers. Data buffers operation is not directly JVM stack, but in the kernel space, this structure can be analyzed out, direct buffers good saving those large, long-term storage data susceptible native operating system operation affects I O / a.

Created out of the use allocateDirect () method buffer type DirectByteBuffer, created out using allocate () method buffer type HeapByteBuffer.

When creating the allocateDirect ByteBuffer buffer usage () method, capacity refers to the number of bytes, the buffer is created IntBuffer, int capacity refers to the number worth, if the byte to be converted, the capacity multiplied by 4, total number of bytes occupied calculated.

Immediate release of the buffer memory method

  1. Manually free up space
package me.qianlv;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaoshu
 * 直接缓冲区释放内存方法
 * 1. 手动释放
 * 2. 交给JVM处理
 * <p>
 * 此程序运行的效果就是1秒钟之后立即回收内存
 * 也就是回收"直接缓冲区"所占用的内存
 */
public class Test9 {
    public static void main(String[] args) throws InterruptedException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        System.out.println("A");
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(Integer.MAX_VALUE);
        System.out.println("B");
        byte[] byteArray = new byte[]{1};
        System.out.println(Integer.MAX_VALUE);
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            byteBuffer.put(byteArray);
        }
        System.out.println("put end.");
        TimeUnit.SECONDS.sleep(1);
        Method cleanerMethod = byteBuffer.getClass().getMethod("cleaner");
        cleanerMethod.setAccessible(true);
        Object returnValue = cleanerMethod.invoke(byteBuffer);
        Method cleanMethod = returnValue.getClass().getMethod("clean");
        cleanMethod.setAccessible(true);
        cleanMethod.invoke(returnValue);
    }
}
复制代码
  1. JVM to be processed
package me.qianlv;

import java.nio.ByteBuffer;

/**
 * @author xiaoshu
 * 直接缓冲区释放内存方法
 * 1. 手动释放
 * 2. 交给JVM处理
 * <p>
 * 此程序多次运行后,一直在耗费内存
 * 进程结束后,也不会马上回收内存
 * 而是会在某个时机触发GC垃圾回收器进行内存回收
 */
public class Test10 {
    public static void main(String[] args) {
        System.out.println("A");
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(Integer.MAX_VALUE);
        System.out.println("B");
        byte[] byteArray = new byte[]{1};
        System.out.println(Integer.MAX_VALUE);
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            byteBuffer.put(byteArray);
        }
        System.out.println("put end.");
    }
}
复制代码

* .Java this class you can run multiple times, resulting in more than one process, and then view the memory usage will be more intuitive.

reference:

  1. "NIO with Socket Programming Guide"

Guess you like

Origin juejin.im/post/5d0638b4e51d45108223fc5c