JVM concept-Java object size and reference type

Source: Internet
Author: User
Document directory
  • Java object size
  • Reference Type
References

1. JVM optimization Summary (2)-some concepts

2. Java deep adventure (4) -- Java garbage collection mechanism and reference type

Body

In reference 1, we summarize the Java object size in the JVM concept, and define and distinguish the three reference types.

Java object size

The size of the basic data type is fixed. The size of non-basic types of Java objects is debatable. In Java, the size of an empty object is 8 bytes, which only saves the size of an object without any attribute in the heap. See the following statement:

Object ob = new Object();

This completes the life of a Java object in the program, but it occupies 4 bytes + 8 bytes. 4 byte is the space required to save the reference in the java stack mentioned above. The 8byte is the object information in the Java heap. Because all non-basic Java objects must inherit object objects by default, No matter what Java objects are, their size must be greater than 8 bytes.

With the object size, we can calculate the size of other objects.

Class NewObject {    int count;    boolean flag;    Object ob;}

Its size is: null object size (8 bytes) + int size (4 bytes) + Boolean size (1 byte) + Null Object Reference size (4 bytes) = 17 bytes. However, because Java divides the object memory allocation by an integer multiple of 8, the size of the object is 24 because the nearest integer multiple of 8 is greater than 17 bytes.

Pay attention toPackaging type. Because this type of packaging has become an object, we need to treat them as objects. The size of the packaging type is at least 12 bytes (minimum space required to declare an empty object), and 12 bytes does not contain any valid information. Meanwhile, because the size of the Java object is an integer multiple of 8, therefore, the size of a Basic Package class must be at least 16 bytes. This memory usage is terrible. It is n times of the basic type (n> 2), and some types of memory usage are even more exaggerated (you can find it if you want ). Therefore, the packaging class should be used as little as possible. After JDK and later, the Java Virtual Machine will optimize the storage because automatic type replacement is added.

Reference Type

Object reference types include strong reference, soft reference, weak reference, and virtual reference.

  Strong reference: Generally, we declare that the object is a reference generated by the VM. In the strong reference environment, when garbage collection is performed, we must strictly determine whether the current object is strongly referenced. if the object is strongly referenced, it will not be recycled.

  Soft reference: Soft references are generally used as cache. The difference between soft reference and strong reference is that when soft reference is garbage collection, the virtual opportunity determines whether to recycle soft reference based on the remaining memory of the current system. If the remaining memory is insufficient, the space referenced by the soft reference will be reclaimed by the virtual opportunity. If the remaining memory is relatively rich, the space will not be recycled.In other words, when a Virtual Machine occurs in outofmemory, there must be no soft reference.

  Weak reference: A weak reference is similar to a soft reference and is used as a cache. However, unlike soft references,Weak references are recycled during garbage collection. Therefore, they only exist in one garbage collection cycle..

Strong references Needless to say, our system generally uses strong references for use. "Soft reference" and "weak reference" are rare. They are generally used as cache, and generally used as cache when the memory size is limited. If the memory is large enough, you can directly use the strong reference as the cache, and the controllability is higher. Therefore, they are often used in the desktop application system cache.

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.