5.Java Program Optimization-Direct Memory Access __java

Source: Internet
Author: User

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);
     }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.