In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how many bytes are occupied by new Object ()". In daily operation, I believe many people have doubts about the number of bytes occupied by new Object (). The editor consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the question of "how many bytes are occupied by new Object ()"! Next, please follow the editor to study!
Let's analyze the in-heap layout and the layout of Java objects in memory.
The direction of the object starts with a piece of code: package com.zwx.jvm;public class HeapMemory {private Object obj1 = new Object (); public static void main (String [] args) {Object obj2 = new Object ();}} 123456789 what's the difference between obj1 and obj2 in memory?
Let's recall from the JVM series 1 article that the method area stores the structure of each class, such as runtime constant pools, property and method data, and data such as methods and constructors. So our obj1 exists in the method area, and new creates an object instance, which is stored in the heap, so we have the following picture (the method area points to the heap): obj2 is a local variable within the method, stored in the local variable table in the stack frame of the Java virtual machine stack, which is the classic stack pointing heap: let's think about it again, and one of our variables points to the heap. There is only one instance object stored in the heap, so how does the sample object in the heap know which Class it belongs to, that is, how does the instance know its corresponding class meta-information? This involves how a Java object is laid out in memory.
Java memory model
Object memory can be divided into three areas: object header (Header), instance data (Instance Data) and alignment fill (Padding). Take the 64-bit operating system as an example (without pointer compression) the layout of Java objects is shown below: the details of the Mark Word in the object header are described in detail in the article synchronized lock upgrade principle. The alignment padding in the image above is not necessary, and if the object header and instance data add up to a multiple of 8 bytes, then there is no need for alignment padding.
Now that we know the Java memory layout, let's look at an interview question.
Object obj=new Object () occupies bytes
This is a problem that many people will mention on the Internet, so combined with the Java memory layout above, let's take a 64-bit operating system as an example, the size of new Object () is divided into two situations:
Unenabled pointer compression occupies a size of 8 (Mark Word) + 8 (Class Pointer) = 16 bytes
When pointer compression is enabled (default is on) when pointer compression is enabled, Class Pointer is compressed to 4 bytes, with a final size of 8 (Mark Word) + 4 (Class Pointer) + 4 (alignment padding) = 16 bytes
Is this the result or not? Let's check it out. First introduce a pom dependency:
Org.openjdk.joljol-core0.1012345
Then create a simple demo:
Package com.zwx.jvm;import org.openjdk.jol.info.ClassLayout;public class HeapMemory {public static void main (String [] args) {Object obj = new Object (); System.out.println (ClassLayout.parseInstance (obj). ToPrintable ());} 12345678910 output is as follows: the final result is 16 bytes, no problem, this is because pointer compression is turned on by default, so let's turn off pointer compression and try again. -XX:+UseCompressedOops turns on pointer compression-XX:-UseCompressedOops turns off pointer compression 12 to run again, and the result is as follows: you can see that there is no alignment padding at this time, but the size is still 16 bits. Let's demonstrate the size of an object with attributes. Create a new class with only one byte attribute inside: package com.zwx.jvm;public class MyItem {byte I = 0;} 12345, and then output the size of the class in the scenarios where pointer compression is turned on and off, respectively. Package com.zwx.jvm;import org.openjdk.jol.info.ClassLayout;public class HeapMemory {public static void main (String [] args) {MyItem myItem = new MyItem (); System.out.println (ClassLayout.parseInstance (myItem). ToPrintable ());} 12345678910
Turn on pointer compression, 16 bytes: turn off pointer compression, 24 bytes: this time you can see the advantage of pointer compression, if you continue to create a large number of objects, pointer compression is still optimized for performance.
Access to objects
After creating an object, of course we need to access it, so when we need to access an object, how do we navigate to it? At present, there are two mainstream ways to access objects: handle access and direct pointer access.
Handle access uses handle access, the Java virtual machine divides a piece of memory in the heap to store the handle pool, then the handle address is stored in the object, and then the object instance data and object type data address are stored in the handle pool.
Direct pointer access (the way used by Hot Spot virtual machines) if direct pointer access is used, object type data is directly stored in the object.
Comparison between handle access and direct pointer access
In the above figure, we are easy to compare, that is, if you use the handle to access, there will be one more pointer positioning, but it also has an advantage that if an object is moved (the address has been changed), then you only need to change the point of the handle pool, there is no need to modify the point within the reference object, and if you use direct pointer access, you also need to modify the reference point to the local variable table.
Heap memory
As we mentioned above, the Mark Word in the Java object header stores the generational age of the object, so what is the generational age?
The generation age of an object can be understood as the number of garbage collection. When an object still exists after a garbage collection, the generation age will be increased by 1. In 64-bit virtual machines, the generation age accounts for 4 places, with a maximum value of 15. The generation age defaults to 0000, which increases gradually with the number of garbage collection.
Java heap memory is divided into Young area and Old area according to generation age. Object allocation will first go to Young area, and when it reaches a certain generation age (- XX:MaxTenuringThreshold can be set size, default is 15), it will enter Old area (Note: if an object is too large, it will directly enter Old area).
The reason for this division is that if there is only one zone in the whole heap, all objects in the heap need to be scanned every time during garbage collection, which is a waste of performance. In fact, the life cycle of most Java objects is very short. Once an object cannot be recycled many times, it can be considered that it may not be recycled in the next garbage collection, so garbage collection in Young area and Old area can be carried out separately. Only when there is no space in Young area after garbage collection, then trigger the garbage collection in Old area.
Young area
Now that it is split into Young areas, let's take a look at the following scenario. The following Young is an overview of garbage collection: if an object comes now and takes up the size of two objects, you will find that it cannot be put down, and GC (garbage collection) will be triggered, but once GC (garbage collection) is triggered, it will affect the user thread. Because all user threads need to be stopped during the GC process to ensure that the object reference does not change, Sun calls this event: Stop the World (STW). These will be described in detail in the next article on garbage collection, which will not go any further here.
So generally speaking, the less GC, the better, but in fact, you can see in the above figure that at least 3 objects can be placed, as long as the objects are put in order, then they can be put down, so this gives rise to a problem. Obviously, there is space, but because the space is discontinuous, the object fails to apply for memory, resulting in triggering GC, so how to solve this problem?
The solution is to put the objects in the Young area in order, so there is a method to divide the Young area into two areas: the Eden area and the Survivor area. The specific operation is as follows: after an object comes, it is first assigned to the Eden area, and when the Eden area is full, the GC is triggered. After GC, in order to prevent spatial discontinuity, the surviving objects are copied to the Survivor area, and then the Eden area can be completely cleaned up. Of course, there is a premise that most objects have a very short life cycle. Basically, most of the objects in the Eden area can be recycled in a single garbage collection (this premise is summed up after testing).
When the GC is triggered, the Survivor area will also be recycled together, which does not mean that only the Eden area will be triggered, but here comes the problem again. The Eden area ensures that the space is basically continuous, but the Survivor area may produce space debris, resulting in discontinuity, so the Survivor area is divided into two: at this time, the workflow becomes like this again: first, the space is allocated in the Eden area, and the GC is triggered when the Eden area is full. After GC, copy the surviving object to S0 area (S1 area is empty), and then continue to allocate objects in Eden area. After triggering GC again, if you find that S0 area can not fit (space debris is actually still available), then copy the S0 area object to S1 area, and copy the surviving object to S1 area, then S0 area is empty, and operate repeatedly in turn. If it is said that the space object in S0 area or S1 area can not be put down after copying and moving, it means that it is really full at this time, then go to the elderly area to borrow some space (this is the guarantee mechanism, the old age needs to provide this kind of space allocation guarantee), if there is not enough space in the elderly area, it will trigger Full GC, if it is still not enough, it will throw an OutOfMemeoyError exception.
Note: in order to ensure that each replication between S0 and S1 can proceed smoothly, the size of S0 and S1 must be the same, and one area must be empty at the same time. Although this practice will lead to a small waste of space, but in terms of other performance improvements, it is worth it.
Old area
When the object in the Young area reaches the set generation age, the object will enter the Old area, and the Full GC will be triggered when the Old area is full. If the space still cannot be cleared, an OutOfMemeoyError exception will be thrown.
Noun literacy
There are a lot of new nouns mentioned above, but in fact, many of these nouns have other names, but I still think it is necessary to know about this.
Garbage collection: GC for short.
Minor GC: GC for the new generation
Major GC: for the old GC, generally speaking, when the old age triggers GC, it also triggers Minor GC, which is equivalent to triggering Full GC.
Full GC: GC occurred at the same time in the Cenozoic era and the old age.
Young area: Cenozoic era
Old District: the Old Age
Eden area: no Chinese translation is found yet (Garden of Eden?)
Surcivor area: survival area
S0 and S1: also known as from area and to area. Note that the identities of from and to are constantly exchanged, and S0 and S1 must be equal, and ensure that an area is empty.
The life trajectory map of an object
From the above introduction, you should have a general impression that an object will continue to flow in Eden area, S0 area, S1 area and Old area (except for short-lived objects that will be recycled in the first place, of course). We can get the following flow chart:
At this point, the study of "how many bytes is occupied by new Object ()" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
The method introduced in this paper is simple, fast and practical.
© 2024 shulou.com SLNews company. All rights reserved.