Java Virtual Machine Learning-JVM Tuning Summary (6)

Source: Internet
Author: User

Size of 1.Java objects

The size of the basic data type is fixed, and here is not much to say. For non-basic types of Java objects, its size is debatable.

In Java, the size of an empty Object object is 8byte, which is just the size of an object that holds no attributes in the heap. Look at the following statement:

Object OB = new Object ();

This completes the life of a Java object in the program, but it occupies a space of:4byte+8byte. 4byte is the space required to save references in the Java stack as described in the above section. And that 8byte is information about the objects in the Java heap. Because all Java non-primitive types of objects require default inheritance of object objects, the size of any Java object must be greater than 8byte.

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

Class NewObject {

int count;

Boolean flag;

Object ob;


Its size is: Empty object size (8byte) +int size (4byte) +boolean size (1byte) + NULL objects reference size (4byte) =17byte. However, because Java is divided by an integer multiples of 8 for object memory allocations, the nearest 8 integer times greater than 17byte is 24, so the size of this object is 24byte.

It is important to note the size of the wrapper type for the base type . Because this type of packaging has become an object, they need to be treated as objects. The size of the wrapper type is at least 12byte (the space required to declare an empty object), and 12byte does not contain any valid information, and since the Java object size is an integer multiple of 8, the size of a basic type wrapper class is at least 16byte. This memory consumption is very scary, it is using the basic type of N times (n>2), some types of memory consumption is more exaggerated (just think about it). Therefore, it is possible to use the packing class sparingly. After JDK5.0, the Java virtual opportunity is optimized for storage by adding automatic type swapping.

2. Reference types

Object reference types are divided into strong references, soft references, weak references, and virtual references .

Strong References : that is, we generally declare that the object is a reference generated by the virtual machine, under a strong reference environment, garbage collection needs to be strictly judged whether the current object is strongly referenced, and if it is strongly referenced, it will not be garbage collected

Soft References : Soft references are generally used as caches. The difference from strong references is that when a soft reference is garbage collected, the virtual opportunity determines whether to recycle the soft reference based on the remaining memory of the current system. If the remaining memory is strained, the virtual opportunity reclaims the space referenced by the soft reference, and if the remaining memory is relatively rich, it will not be recycled. In other words, when a virtual machine occurs outofmemory, there must be no soft reference present.

Weak references : weak references are similar to soft references, and are used as caches. However, unlike soft references, a weak reference is bound to be reclaimed when it is garbage collected, so its life cycle only exists during a garbage collection cycle.

Strong references Needless to say, our system is generally used as a strong reference. Soft references and weak references are relatively rare. They are generally used as caches, and generally are cached when the memory size is limited. Because if the memory is large enough, you can use the strong reference directly as the cache, while the controllability is higher. As a result, they are commonly used in the desktop application system cache.

Java Virtual Machine Learning-JVM Tuning Summary (6)

Related Article

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: 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.