Java hashmap limit capacity size limit memory usage

Source: Internet
Author: User

What is the maximum capacity of hashmap? I searched for a long time and couldn't find the answer. I am too lazy to continue searching ~~~

It should be very big anyway ~~

 

However, hashmap accounts for a large amount of memory, and many people are more concerned about how to calculate the memory occupied by hashmap.

After running the command, the maximum capacity of hashmap was not found, but it was found that hashmap was very memory-consuming and outofmemoryerror immediately.

 Integer  =     1  ;
Long Start = 0 ;
Long End = 0 ;
// First garbage collection
System. GC ();
Start
= Runtime. getruntime (). freememory ();
Hashmap Map
= New Hashmap ();
For ( Int I = 0 ; I < 1000000 ; I ++ ){
Map. Put (I, );
}
// Clean up again when computing is approaching
System. GC ();
End
= Runtime. getruntime (). freememory ();
System. Out. println (
" Memory occupied by a hashmap object: " + (End - Start ));

 

When 2000000 items are added, the memory overflows.

The hashmap of the first item occupies nearly 60 MB of memory ~~ Exaggerated

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.