How much memory does a new object object occupy?

Source: Internet
Author: User

/***************************************************

 * todo:description.

* @author: Gao_chun

* @since: 2015-4-2

* @version: 1.0.0

 *  @remark: Reprint Please specify the source

**************************************************/


The automatic memory management mechanism of Java saves a lot of coding work, greatly improves the productivity of Java, and the performance of the JVM is getting better, especially G1, which improves the situation of stop the world in garbage collection.

Perhaps a lot of people have not considered this question, how much memory does new object objects occupy (Object obj = new Object ())?
It is very clear here that obj is a reference to an object, the length of the reference determines the addressing capability of Java, the32-bit JDK is 4 bytes, and the 64-bit JDK is 8 bytes .

because the Obj object does not have any data (field), will it allocate space on the heap? If you allocate space, what is stored inside it?

in object-oriented thinking, the object encapsulates data and behavior, which is a unified whole, although the Obj object has no data, but there is behavior (the object class defines 12 methods). When we are done with the new operation, the value of obj is the address of the heap memory, since obj points to a piece of memory, which means that it allocates space on the heap.

So how much space is allocated and what is stored? I did not find the relevant description in the JVM specification and in the Java language Specification, which may be part of the JVM's implementation of free control. We can use the tool Jvisualvm.exe from the JDK to see how much space is allocated, and what is saved is unclear.


    /**     * Constructs an empty object with no fields to occupy how much memory     * @author Gao_chun *     /Public      class Emptytest {public         static void main ( String[] args) throws interruptedexception{              //Added to the collection so that garbage cannot be reclaimed              list<emptyobject> emptys = new ArrayList <> ();              for (int i=0;i<100;i++) {                  emptys.add (new Emptyobject ());              }              Open JVISUALVM, view the size of the Emptyobject as 16 bytes              thread.sleep (60*1000);          }          private Static Class emptyobject{}      


This is the number of different objects (for loops), the result is the same, my JDK is 64 bits, 8 bytes is a reference, 8 bytes is heap memory, the total is 16 bytes. As shown in the following:

 

It may not be meaningful for us to study how much memory the new object object occupies, because we can determine the object tree when programming, basically can determine the object size, in addition to the variable length field, of course, the variable length field we will generally have a length limit. So what we really care about is the final size of all the data, the size of the database.

So why should we study this question without any practical significance? I think it can only be described in three words: curiosity. Curiosity is a powerful force that drives us to research technology, and when we've been working for years, especially in companies that don't pay attention to technology, do we have a passion for technology? keep a sensitive and curious heart, perhaps the path of technology can go longer and farther.

How much memory does a new object object occupy?

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.