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