Differences between allocate and allocatedirect of bytebuffer

Source: Internet
Author: User
Differences between allocate and allocatedirect of bytebuffer

In Java, when we want to perform more underlying operations on the data, it is usually in the byte format of the operation data. In this case, we often use the bytebuffer class. Bytebuffer provides two static instance methods:

public static ByteBuffer allocate(int capacity)  public static ByteBuffer allocateDirect(int capacity) 

Why are two methods available? This is related to the memory usage mechanism of Java.

  1. The memory overhead produced by the first allocation method is in the JVM,
  2. The second allocation method generates overhead outside JVM, Which is system-level memory allocation.

When the Java program receives external data, it is first obtained by the system memory, and then copied to the JVM memory for use by the Java program. Therefore, in the second allocation method, you can save the copy operation, which improves the efficiency. However, system-level memory allocation is much time-consuming than JVM memory allocation, so allocatedirect is not always the most efficient. The following is a comparison of the two allocation methods in different capacities:

As shown in the figure, when the volume of data operated is very small, the operation time of the two allocation methods is basically the same. The first method may be faster, but when the volume of data is large, the second method is much larger than the first method.

 

 

 

From: http://blog.sina.com.cn/s/blog_67dc11000101cpsk.html

 

Differences between allocate and allocatedirect of bytebuffer

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.