Your java objects account for how much memory?

At the beginning of learning java, know the concept of inheritance, the subclass inherits the properties, private and default parent class (when not in a package) otherwise. At that time I did not understand the parent and child classes is how to preserve the value of the property, but to understand the JOL, finally have the opportunity to solve the long plagued by heart problems.

First of all, the Internet has a saying, when you create a subclass object when creates a parent object simultaneously and subclass object will have a sub-needle points to the object of the parent class for this statement I doubt it, because in that case, then you create an object created out when there are many objects, this design is way too wasted memory. JOL used today to verify whether or not the case. (Note: the size of the object of this experiment is based on the case where the compression of the pointer, the pointer does not affect the result of this experiment is not compressed, but of different size of the object)

First, you need to import the package JOL

<dependency>
        <groupId>org.openjdk.jol</groupId>
        <artifactId>jol-core</artifactId>
        <version>0.9</version>
</dependency>

  Then we look at the first question: How large a class object Object Memory

public class JolTest {
    public static void main(String[] args) {
        Object o = new Object();
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }
}

  Above code is executed following results

 

 Object can be seen that a 16-byte occupies the first 12 bytes of the first object, the integral multiple of 4 bytes is added to data alignment java used, the size of a java object always 8

Then we try to create a class that contains a variable of type int, to see what he is in memory

public class JolTest {
    public static void main(String[] args) {
        Object o = new Parent();
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }
}

class Parent{
    private int age;
}

Execution results are as follows

 

Still is 16 bytes, but the difference is 12-15 this is not four alignment, but int, int we know the type of possession of four bytes

Then we write a class that inherits Parent, take a look at his situation

public class JolTest {
    public static void main(String[] args) {
        Object o = new Child();
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }
}
class Parent{
    private int age;
}
class Child extends Parent{
    private int age;
}

 

 Will only get a sub-class of objects we can see from the results, the subclass has a target Parent.age, not a pointer to the parent class, so we can conclude that create sub-class object, but the child class object contains attributes of the parent class, even if it is private property, even if the property of the same name and properties of the parent class

Now I do not know got the answer I wanted, and there are three additional benefits:

1. How to calculate the size of an object (pointer compression enabled): ((12 + + size attribute their size attributes of all of its ancestor classes + 4) / 8) * 8

2. Another does not support inheritance depth basis, the parent class property will not be used will also affect the size of the object.

3. (class object is generally in the project will create a lot of) objects require strict control, it is to use a combination of inheritance or need to think about, after all, inherit property may be wasted, and the combination would be a waste object header and alignment, if only to consider the size of the object, if the inherited property exists necessary, recommend the use of inheritance; if all non-essential combinations may be used, when needed for the property re-create the object.

Guess you like

Origin www.cnblogs.com/fiftyonesteps/p/12554104.html