Allocated memory for Hashtable.put ()

So, I read Peter Norwig’s IAQ (rarely asked questions - link ) and came across this:

You may be surprised to find that the Object accepts 16 bytes or 4 words in the Sun JDK VM. This breaks down as it should: there is a two-word heading, where one word is a pointer to an object class and other points to instance variables. Although the Object does not have instance variables, Java still allocates one word for the variables. Finally, there is a “handle”, which is another pointer to a two-word header. The sun says this extra level of indirection makes garbage collection easier. (There have been high-performance Lisp and Smalltalk garbage collectors that do not use an extra layer for at least 15 years. I heard, but did not know, confirmed that the Microsoft JVM did not have an extra layer of indirection.)

An empty new line String () takes up 40 bytes, or 10 words: 3 words of pointer overhead, 3 words for instance variables (start index, end index, and character array) and 4 words for an empty char array. Creating a substring of an existing string takes "only" 6 words, because the char array is shared. Putting an Integer key and an Integer value in a Hashtable accepts 64 bytes (in addition to the four bytes that were previously allocated to the Hashtable Array): I let you work on why.

So, I obviously tried, but I can not understand. In the following, I only count the words: Hashtable put creates one variable Hashtable $ Entry: 3 (service) + 4 (3 links, which, I believe, are 1 word + 1 int). I also assume that it means that integers are allocated (so they are not cached by the Integer class or already exist), which comes to 2 * (3 [service] + 1 [1 value int]).

So, in the end, we ended up with 15 words or 60 bytes. So, at first I thought that Entry as an inner class needs a reference to its external object, but, alas, it is static, so this does not make much sense (of course, we should store a pointer to the parent class, but I would think that the information stored in the VM class header).

, , JVM ( 64- ), , :)

: , : , , , . , , Hashtable - , char ( C). , Java/JVM:)

+5
1

, 3 16- 2 32- Map.Entry 2 x 1 32- int. 64

, Sun/Oracle JVM 8- , , 20 , 24 ( 8)

, JVM 64- , Map.Entry 16 .

, , TIntIntHashMap, .

, , , , . 40 , 10 , , . ( , ) 10 .

, .

+2

All Articles