[Four] Java runtime data area

Recently, I was looking at some interview questions and found that I did not have a thorough understanding of the allocation of Java underlying variable memory. So I searched for information on the Internet and read many other people's blogs. So I also sorted it out and shared it with you:

 

Automatic garbage collection of the Java virtual machine in the heap :

Reference variables are ordinary variables. They are allocated on the stack when they are defined. Reference variables are released after the program runs outside of its scope. The array and the object themselves are allocated in the heap. Even if the program runs outside the code block where the statement that uses the new to generate the array or object is located, the memory occupied by the array and the object itself will not be released, and the array and the object will not be referenced to it when there is no reference variable. When it becomes garbage, it cannot be used, but it still occupies the memory space, and is taken (released) by the garbage collector at an indeterminate time afterwards. This is also the reason why Java takes up more memory.

 

The data stored in the stack can be shared by the same thread:

Suppose we also define int a = 3; int b = 3; the compiler first processes int a = 3; first it creates a reference to the variable a in the stack, and then looks for an address with a literal value of 3, but it didn’t find it. , Open an address to store the literal value of 3, and then point a to the address of 3. Then process int b = 3; after creating the reference variable of b, since there is already a literal value of 3 in the stack, b directly points to the address of 3. In this way, there is a situation where both a and b point to 3 at the same time. Special attention is that this kind of literal reference is different from the reference of the class object. Assuming that the references of two class objects point to an object at the same time, if an object reference variable modifies the internal state of this object, then the other object reference variable immediately reflects this change. On the contrary, modifying its value through a reference to a literal value will not cause the value of another reference to this literal value to also change. As in the above example, after we define the values ​​of a and b, let a=4; then, b will not be equal to 4, but still equal to 3. Inside the compiler, when it encounters a=4;, it will search again to see if there is a literal value of 4 in the stack. If not, it will reopen the address to store the value of 4; if it already exists, it will directly point a to this address. . Therefore, the change in the value of a will not affect the value of b.

 

Automatic packing of packaging:

Example:

Integer c = 3;          

Integer d = 3;

In auto-boxing, call the valueOf(int i) method. There are rules when turning int into Integer. When the value of your int is -128-127, what is returned is not a new Integer object, but an Integer object that has been cached in the heap, ( We can understand this way, the system has cached Integers between -128 and 127 in an Integer array. If you want to turn an int into an Integer object, first go to the cache to find it, and if it finds it, it will return a reference directly to you. That's it, you don't need to make a new one), if it is not at -128-127, a new Integer object will be returned.

In addition, other packaging types except Float and Double implement caching.    

 

String constant pool:

String ss3 = newString("china"); First find whether there is a china string in the constant pool, if not, create a string object in the constant pool, and then create a "china" object in the constant pool in the heap The copy object. So two objects may be created.

String str1 = "abc"; String str2 = "ab"; String str3 = str2 + "c"; str1==str3 is false because Stringstr3 = str2 + "c" involves variables (not all constants, including null) Add, so a new object will be generated. Its internal implementation is to first create a StringBuilder in the heap, then append(str2), append("c"); and then let str3 refer to the object returned by toString().

For equals strings, there is always only one copy in the constant pool and multiple copies in the heap.

 

String intern() method:

String's intern() method will find whether there is an equal string in the constant pool, if there is, it will return a reference to the string, if not, it will add its own string into the constant pool.

There may be some deviations in your own understanding. If there are mistakes, I hope you can point them out, thank you! ! !

 

Guess you like

Origin blog.csdn.net/Jack_PJ/article/details/80545341