preface

Direct memory is also mostly referred to as off-heap memory, and its use has become more common since the INTRODUCTION of NIO in the JDK. Out-of-heap memory can be allocated via native methods and operated via DirectByteBuffer objects.

Direct memory is not part of the Java heap, so it is not limited by the heap size, but it is limited by the physical memory size.

configuration

The maximum available direct memory can be set with the -xx :MaxDirectMemorySize parameter, which defaults to the maximum heap memory size if not set at startup, which is the same as -xmx. If the maximum heap size is 1 gb, and the default direct memory size is 1 GB, then the maximum memory size required by the JVM is a little more than 2 GB. GC is triggered when direct memory reaches its maximum limit, and an OutOfMemoryError is raised if the collection fails.

Memory Allocation Time

The environment is JDK9, and the two memory allocations take as follows: Run them twice to warm up. As you can see, the direct memory allocation is time-consuming, while the heap memory allocation operation takes several times less time.

public static void directMemoryAllocate() {
		long tsStart = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
			ByteBuffer buffer = ByteBuffer.allocateDirect(400);

		}
		System.out.println("direct memory allocate: " + (System.currentTimeMillis() - tsStart) + " ms");
		tsStart = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
			ByteBuffer buffer = ByteBuffer.allocate(400);
		}
		System.out.println("Heap memory allocate:" + (System.currentTimeMillis() - tsStart) + " ms");
	}
Copy the code
Direct memory allocate: 149 MS Heap memory allocate: 41 MS Direct memory allocate: 122 MS Heap memory allocate: 31 msCopy the code

Read/write Operation Time

The environment is JDK9, and the time of read and write operations of the two types of memory is as follows. Run the same two times to warm up, and you can see that the speed of direct memory read and write operations is relatively faster.

public static void memoryRW() {
		ByteBuffer buffer = ByteBuffer.allocateDirect(400);
		ByteBuffer buffer2 = ByteBuffer.allocate(400);
		long tsStart = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
			for (int j = 0; j < 100; j++) {
				buffer.putInt(j);
			}
			buffer.flip();
			for (byte j = 0; j < 100; j++) {
				buffer.getInt();
			}
			buffer.clear();
		}
		System.out.println("Direct memory RW:" + (System.currentTimeMillis() - tsStart) + " ms");

		tsStart = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
			for (int j = 0; j < 100; j++) {
				buffer2.putInt(j);
			}
			buffer2.flip();
			for (byte j = 0; j < 100; j++) {
				buffer2.getInt();
			}
			buffer2.clear();
		}
		System.out.println(Heap memory RW: + (System.currentTimeMillis() - tsStart) + " ms");
	}
Copy the code
Direct Memory RW: 39 ms Heap memory RW: 34 ms Direct Memory RW: 23 ms Heap memory RW: 46 msCopy the code

conclusion

In theory, direct memory is faster, but it is not arbitrary to say that direct memory is faster. In addition, direct memory is slower in memory allocation operations. Direct memory is more suitable for scenarios where memory requests are small but read and write operations are frequent.

————- Recommended reading ————

My 2017 article summary – Machine learning

My 2017 article summary – Java and Middleware

My 2017 article summary – Deep learning

My 2017 article summary — JDK source code article

My 2017 article summary – Natural Language Processing

My 2017 Article Round-up — Java Concurrent Article


Talk to me, ask me questions:

The public menu has been divided into “reading summary”, “distributed”, “machine learning”, “deep learning”, “NLP”, “Java depth”, “Java concurrent core”, “JDK source”, “Tomcat kernel” and so on, there may be a suitable for your appetite.

Why to write “Analysis of Tomcat Kernel Design”

Welcome to: