The performance cost of the Java Collection collection class Sizing

Source: Internet
Author: User

Some specific implementations of the Java collection class because the underlying data store is based on arrays, and as the number of elements increases, the cost of resizing is significant. As the collection element grows to an upper limit, it may be possible to adjust its size to a performance issue.

When the collection element reaches the maximum value of the internal array, a new array needs to be created. and the old array elements are copied to the new array through the Arrays.copyof method, which consumes the CPU time slices and also requires garbage collection, especially when the collection object has a long life cycle and is already in the old age and needs to go through a full GC to free up memory. The memory of the new array allocates memory in the young generation's Eden area (which may not be allocated in the Eden area when the object is particularly large), and it takes two times to enter the survivor area to enter the old age, the mark of garbage collection, The performance consumed by the purge operation can be avoided at the time of initialization of the collection, given a reasonable size.

There is still a problem when resizing an array. The new copy of the array reference element may be assigned by the JVM in a location that is not in the same block memory as the other field references in the collection class, which may cause a CPU cache miss (in general, the object and its fields need to be accessed concurrently. The memory location where the object and its fields are placed in the same location as possible will increase the CPU memory hit ratio, and the time to access the field will be longer (albeit soon).


Note: This paper considers the collection class based on the array implementation, and does not discuss the chain list such as LinkedList, TREEMAP, etc.

The performance cost of the Java Collection collection class Sizing

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.