In-depth understanding of the Java virtual machine-direct memory-super detailed chapter-chapter 5

In-depth understanding of the Java virtual machine-Introduction to the Java virtual machine-Chapter 1 In-
depth understanding of the Java virtual machine-Class loading subsystem-Chapter 2 In-
depth understanding of the Java virtual machine-Runtime data area and native method interface-details Article-Chapter 3 In-
depth understanding of Java virtual machine-Object instantiation memory layout and access location-Super detailed article-Chapter 4 In-
depth understanding of Java virtual machine-Direct memory-Super detailed article-Fifth Chapter In-
Depth Understanding of Java Virtual Machine-Execution Engine-Super Detailed Chapter-Chapter 6 In-
depth Understanding of Java Virtual Machine-StringTable-Super Detailed Chapter-Chapter 7 In-
depth Understanding of Java Virtual Machine-Java Garbage Collector- -The worst combination of
pictures and texts in history-Chapter 8 In- depth understanding of the Java virtual machine-Class file structure-Chapter 9-Part 2 In-
depth understanding of the Java virtual machine-Bytecode instruction set and parsing instructions-Ten Chapter-Part II In-
depth understanding of the Java virtual machine-class loading process (class life cycle)-Chapter 11-Part II-In-
depth understanding of the Java virtual machine-talk about class loader-twelfth Chapter-Medium

1: Introduction to Direct Memory

  • It is not part of the runtime data area of ​​the virtual machine, nor is it the memory area defined in the Java Virtual Machine Specification.

  • Direct memory is a memory interval outside the Java heap and directly applied to the system.

From NIO, operating Native memory through Di rectByteBuffer stored in the heap. Generally, the speed of accessing direct memory is better than that of the Java heap. That is, high read and write performance.
➢ For performance considerations, direct memory may be considered for frequent reads and writes. .
➢Java’s NIO library allows Java programs to use direct memory for data buffers

We use code to view the occupation and release of direct memory

public class BufferTest {
    
    
    private static final int BUFFER = 1024 * 1024 * 1024;//1GB

    public static void main(String[] args){
    
    
        //直接分配本地内存空间
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER);
        System.out.println("直接内存分配完毕,请求指示!");

        Scanner scanner = new Scanner(System.in);
        scanner.next();

        System.out.println("直接内存开始释放!");
        byteBuffer = null;
        System.gc();
        scanner.next();
    }
}

After allocating memory, we see that the occupied memory is as follows. After
Insert picture description here
releasing the memory, we see the following occupied memory
Insert picture description here

2: Use local memory to read data test

Insert picture description here
Insert picture description here

public class BufferTest1 {
    
    

    private static final String TO = "F:\\test\\异界BD中字.mp4";
    private static final int _100Mb = 1024 * 1024 * 100;

    public static void main(String[] args) {
    
    
        long sum = 0;
        String src = "F:\\test\\异界BD中字.mp4";
        for (int i = 0; i < 3; i++) {
    
    
            String dest = "F:\\test\\异界BD中字_" + i + ".mp4";
//            sum += io(src,dest);//54606
            sum += directBuffer(src,dest);//50244
        }

        System.out.println("总花费的时间为:" + sum );
    }

    private static long directBuffer(String src,String dest) {
    
    
     //......

    }
    private static long io(String src,String dest) {
    
    
      //...
    }

Test result: the time to draw with io is longer than the time to draw with nio

3: OOM and memory size setting of direct memory

  • Because the direct memory is outside the Java heap, its size is not directly limited by the maximum heap size specified by -Xmx, but the system memory is limited, and the sum of Java heap and direct memory is still limited by what the operating system can give Maximum memory.

Disadvantages
➢ Higher cost of allocation and recycling
➢ Not subject to JVM memory recycling management

  • The direct memory size can be set by MaxDirectMemorySize
    Insert picture description here
  • If not specified, the default is the same as the maximum value of the heap -Xmx parameter value

Guess you like

Origin blog.csdn.net/qq_44891295/article/details/106537737