The memory structure of object and calculation method of occupying space

Source: Internet
Author: User

The idea of a deep understanding of the structure and volume of data in memory is very early. Usually in the process of writing code, for these are completely in a perceptual understanding of the data structure and objects used in the code, especially when dealing with large amounts of data, there is always a sense of uncontrollable. Take advantage of the time of week 6th, by looking at some information about virtual machines and memory objects, and Eclipse to view the source code, I figured out how much space an object actually occupies, what it looks like in memory. So after two days of exploration, This article is always in the following. for the data involved in the article, different JDK environments may have some minor discrepancies, including the JDK version, the JDK32/64 bit, the memory size allocated by the JVM parameters, and the type of garbage collector. the data in this article is derived from jdk1.7.0_79 64-bit, WIN7 64-bit, Eclipse version:mars Release (4.5.0). everyThe size of the occupied space calculated by an object is verified on its own machine, and the specific verification method is posted in an attached article. If the text of the data in doubt, very welcome to communicate, the spirit of small rookie constantly learning attitude, I hope we all progress together.
Object = Object Header + member variable + align fill
Object Header structure :java object inHeapinside the structure is this: object head and Object body, object body withCinside the structure is the same, the object header consists of two fields: for storing hashcode, Sync, GC 's _mask domain , and point to the method area of the object Class Pointer to object -- _klassdomain, for -bit system, head length should theoretically be8+8=16bytes. However, starting from Java6u23, the 64-bit machine automatically turns on the function of pointer compression, at which point the reference pointer is 4 bytes long. Therefore, the object head length should be 8+4=12. member variables : Two classes, including some basic types, such as Int,long.byte,short,boolean, and reference types, such as string,date references. If it is a reference type, the object to which the reference type points should also be included in the current object. Snap To fill :The JVM specifies that the size of the object must be an integer multiple of 8 bytes, if not sufficient.
also, for arrays, there is a field that indicates the length of the array. In fact, the array is also a class, which is described later in this article. based on this theory, let's calculate the size of the common object footprint.
  • Integer
    • Class structure diagram: You can see that there is only one private int type of data
    • So the integer length is: Header (8+4) + int (4) = 16 bytes
  • Long
    • Class structure diagram
    • Similar to integer, there is only a private member of a long type.
    • So the total length is: Head (8+4) +long (8) +padding (4) = 24 bytes


  • Object
    • Class structure diagram
    • No member variable, so occupy the space Head (8+4) +padding (4) = 16 bytes


  • String : "String"
        / li>
      • This structure is a little bit more complex, involving array members. The array is also a type, except that this type is the type that the JVM generates at run time, not defined in the class file, and we treat it as a special class. Since the member variable is an object, we will divide the string into two parts:
        • Span style= "COLOR: #666666" >string type: Head (8+4) +int (4) +int (4) + point to char[] object reference type (4) = 24 bytes
        • char[] Type: An array type is more than a normal object, a field that indicates the length of an array, accounting for 4 bytes. For string "string", Head (8+4) + array Length (4) + "string" (2*6) +padding (4) = 32 bytes
      • Therefore, it occupies a total space of 56 bytes


  • ArrayList
    • Class structure diagram
    • In fact, there is also a Modcount member, inherited from the Abstractlist class, then for a list = new arraylist<string> (); List.add ("String"); For list, it has two int, an array of size 10 (when List.add () first element, it initializes elementdata to an array of length 10)
      • ArrayList: Head (8+4) +int (4) +int (4) + array reference (4) = 24 bytes
      • Elementdata[]: Head (8+4) + length (4) +string reference (4*10) = 56 bytes
      • "String" string: We've calculated this before, for 56 bytes.
    • Therefore, the total space size is 24+56+56=136 bytes


  • HashMap
    • Class structure diagram
    • HASHMAP Internal structure comparison complex Miscellaneous, in addition to some basic types, there are more complex types of collections. such as table, is a entry array, used to hold the key value pairs, all put into the map key-value will be encapsulated into a entry into the table. There are also helper objects, such as entry, that inherit from the Abstractmap keyset,values, which are the collections used to traverse the map elements, and their main function is to output the data in the table by maintaining an iterator inside themselves. Key-value data is not actually stored.
    • MAP<STRING,STRING>  map  =  new  < Span style= "Font-family:consolas" >hashmap<string,string> ();   At this point we'll calculate his occupancy:

      • total space is: 48+ 16=64 bytes
        • ha Shmap: Head (8) +int (4*4) +float (4) +table array Reference (4) +entryset reference (4) +keyset reference (4) +values reference (4) +padding (4) = 48 bytes
        • table: Head (8+4) + length (4) = 16 bytes
    • and then we put in a piece of data: Map . Put ( "100002", "Zhang Ming");
      • when the HashMap is initialized, he will open a table array of length 16, whenever put a new key-value, he will be based on the current threshold to determine whether the need for expansion, if necessary to expand, The table array is enlarged in multiples increments. such as 16, 32, 64. Please refer to http://blog.csdn.net/zq602316498/article/details/39351363 for specific principles.
      • Now let's figure out how much space this map occupies.

        • HashMap: Head (8) +int (4*4) +float (4) +table array Reference (4) +entryset reference (4) +keyset reference (4) +values reference (4) +padding (4) = 48 bytes
        • table:80+32+16+16+56+48+0= 216 bytes
          • Table: Head (8+4) + length (4) +entry (4*16) = 80 bytes
          • Entry: Head (8+4) +k (4) +value (4) +next (4) +int (4) +padding (4) = 32 bytes
          • Key (String): 56 bytes
          • Value (String): 48 bytes
          • Next: Because there is only one element, the next value is null,0 bytes
        • EntrySet: null pointer, 0 bytes
        • KeySet: null pointer, 0 bytes
        • Values: null pointer, 0 bytes
      • In summary analysis, this map takes up 48+216+0+0+0=264 bytes
    • Then we continue to call the Map.keyset () method, at which point the KeySet is given an object of type Hashmap$keyset, which has the following structure:
      • As you can see, it's not complicated, just a tool class used to traverse the map key collection.
        • KeySet: Head (8+4) +padding (4) = 16 bytes
      • So, the total size is 264+16=280 bytes
    • Then we continue to mobilize map.values (), and like above
      • Values: Head (8+4) +padding (4) = 16 bytes
      • So, the total size is 280+16=296 bytes
    • Then we continue to call Map.entryset (),
      • EntrySet: Head (8+4) +padding (4) = 16 bytes
      • So the total size is 296+16=312 bytes


If you want to reprint, please specify the original address:http://blog.csdn.net/zq602316498/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The memory structure of object and calculation method of occupying space

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.