Buffer provides a class-directbuffer that can directly access the physical memory of the system. Directbuffer inherits from Bytebuffer,bytebuffer still allocates memory on the JVM heap, its maximum memory, limited by the maximum heap. The directbuffer is allocated directly to physical memory and does not occupy heap space.
When accessing Bytebuffer, the system uses a "kernel buffer" for indirect operations. The Directbuffer position is equivalent to this "kernel buffer".
As a result, using Directbuffer is a much closer to the bottom of the system, faster than Bytebuffer (about one times faster). But the cost of creating and destroying Directbuffer is far higher than bytebuffer.
The method for applying Directbuffer is as follows
Bytebuffer B = bytebuffer.allocatedirect (500);
When you need to create a buffer frequently, because the cost of creating and destroying Directbuffer is relatively high, it is not suitable to use directbuffer, but if the directbuffer is reused. It can greatly improve the performance of the system when read and write is more frequent.
Real-world applications require monitoring of directbuffer to enhance directbuffer availability.
private void Mondirectbuffer () throws classnotfoundexception,exception,nosuchfieldexception{
Class C = Class.forName ("Java.nio.Bits"); Get private data by reflection
Field maxmemory = C.getdeclaredfield ("MaxMemory");
Maxmemory.setaccessibly (true);
Field reservedmemory = C.getdeclaredfield ("Reservedmemory");
Reservedmemory.setaccessible (true);
Synchronized (c) {
long maxmemoryvalue = (long) maxmemory.get (null); Total size
Long Reservedmemoryvalue = (long) reservedmemory.get (null);//remaining size
System.out.println (" Maxmemoryvalue: "+maxmemoryvalue);
System.out.println ("Reservedmemoryvalue:" +reservedmemoryvalue);
}