In Java, empty HashMap space allocation

How can I find out how much space a pre-configured HashMap takes before adding any items? For example, how to determine how much memory the following takes:

HashMap<String, Object> map = new HashMap<String, Object>(1000000);
+3
source share
8 answers

Basically you can:

  • calculate it by theory:
    • look at the HashMap implementation to find out what this method does.
    • Look at the implementation of the virtual machine to find out how much space the individual created objects take.
  • Measure it somehow.

Most of the other answers relate to the second method, so I will consider the first (in OpenJDK source, 1.6.0_20).

capacity, >= initialCapacity, , 1048576 = 2 ^ 20 . new Entry[capacity] table. ( ).

, HashMap ( 3 ints, float ) Entry []. ( ) (, ).

, , . - 32- 32 (= 4 ), 64- 64 (= 8 ).

, 32- 4 , 64- 8 , .


HashTable , Entry. int , 24 32- , , . , 1000000- HashMap ( > 1) ~ 28 32- ~ 56 64- .

, .

+4

. :

long preMemUsage = Runtime.getRuntime().totalMemory() -
      Runtime.getRuntime().freeMemory();
HashMap<String> map = new HashMap<String>(1000000);
long postMemUsage = Runtime.getRuntime().totalMemory() -
      Runtime.getRuntime().freeMemory();
+2

Java, JVM , .

~4 * 2^20, ~8 * 2^20 32- 64- jvm .

:

  • HashMap Sun Java 1.6 table, -.

  • () HashMap null, - , initialCapacity. (... .)

  • 4 32- JVM 8 64- JVM. 64- JVM ( " oops" ), JVM, .

  • 5 , table, .

  • , .

, table , 2^20 ( 2 , 1,000,000), .


, , . , , , , . ( .)

+2

: http://www.javaworld.com/javaworld/javatips/jw-javatip130.html

, java C-style sizeof. , IMO, , .

, : java 40 . , , 40 ...

+1

VisualVM ( JDK 6 ) .

0

, - . , 32- 64- JVM. - () , . , .

0

Java 1.7 ( 1.7.0_55) HashMap . put() - . "inflateTable()". , HashMap, - , , .

0
source

Source: https://habr.com/ru/post/1795338/


All Articles