Java memory allocation and management is one of the core technologies of Java, before we have described the Java memory management and memory leaks and Java garbage collection knowledge, today we dive into the Java core, a detailed description of Java in memory allocation knowledge. General Java involves the following areas when allocating memory:
Registers: We have no control in the program
Stacks: A reference to the underlying type of data and objects, but the object itself is not stored on the stack, but is stored in the heap (the new object)
Heap: Storing data produced with new
Static fields: Static members that are stored in the object with static definitions
Constant pools: Storing constants
Non-RAM storage: Permanent storage space such as hard disk
Stacks in Java memory allocation
Some basic types of variable data and object reference variables defined in a function are allocated in the stack memory of the function.
When a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the variable exits the scope, Java automatically releases the memory space allocated for the variable, which can be used as an immediate alternative. The data size and lifecycle in the stack can be determined, and the data disappears when no reference is directed to the data.
Heap in Java memory allocation
Heap memory is used to store objects and arrays created by new. The memory allocated in the heap is managed by the automatic garbage collector of the Java Virtual machine.
After generating an array or object in the heap, you can also define a special variable in the stack so that the value of the variable in the stack equals the first address of the array or object in the heap memory, and the variable in the stack becomes the reference variable for the array or object. A reference variable is equivalent to a name that is an array or an object, and you can later use the reference variable in the stack to access the array or object in the heap. A reference variable is equivalent to a name that is an array or an object.
A reference variable is a normal variable that is assigned in the stack when defined, and the reference variable is released after the program runs beyond its scope. and the arrays and the objects themselves are allocated in the heap, even if the program runs to a block of code that uses new to produce an array or an object's statements, the memory occupied by the array and the object itself is not freed, and the array and object become garbage when they point to it without a reference variable, and cannot be used, but still occupy the memory space. Be taken Away (released) by the garbage collector at a later uncertain time. This is why Java comparisons account for memory.
In fact, the variables in the stack point to the variables in the heap memory, which is the pointer in Java!
Heap and Stack
Java's heap is a run-time data area in which the object allocates space. These objects are established through directives such as new, NewArray, Anewarray, and Multianewarray, and they do not require program code to be explicitly released. The heap is responsible for garbage collection, the advantage of the heap is the dynamic allocation of memory size, the lifetime does not have to tell the compiler in advance, because it is dynamically allocating memory at runtime, the Java garbage collector will automatically take away these no longer used data. The disadvantage is that the access rate is slow due to the dynamic allocation of memory at run time.
The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared. The disadvantage is that the data size and lifetime in the stack must be fixed and inflexible. There are some basic types of variable data in the stack (int, short, long, byte, float, double, Boolean, char) and object handles (references).
Stack has a very important particularity, is the existence of the stack of data can be shared. Let's say we both define:
Java code
int a = 3;
int b = 3;
The compiler deals with int a = 3 First, it creates a reference to a in the stack, finds out if there is a 3 value in the stack, and if not, it stores 3 in, then points a to 3. then process int b = 3, after you create the reference variable for B, because you already have 3 in the stack, point B directly to 3. In this way, there is a case where A and B both point to 3.
At this point, if you make a=4 again, the compiler will search for a 4 value in the stack, and if not, put 4 in and point A to 4, and if so, direct a to this address. Therefore the change of a value does not affect the value of B.
Note that this sharing of data is different from the two-object reference to an object, because the modification of a does not affect B, it is done by the compiler, and it helps save space. While an object reference variable modifies the internal state of the object, it affects another object reference variable.
Java code
1.int I1 = 9;
2.int i2 = 9;
3.int i3 = 9;
4.public static final int INT1 = 9;
5.public static final int INT2 = 9;
6.public static final int INT3 = 9;
For member variables and local variables: member variables are external to the method, internally defined variables of the class, and local variables are variables defined within the method or statement block. Local variables must be initialized.
The formal parameter is a local variable, and the data of the local variable exists in the stack memory. The local variables in the stack memory disappear as the method disappears.
The member variable is stored in the object in the heap and is reclaimed by the garbage collector.
such as the following code:
Java code
Class Birthdate {
private int day;
private int month;
private int year;
Public birthdate (int d, int m, int y) {Day
= D;
month = m;
Year = y;
}
Omit the Get,set method ...
}
public class test{public
static void Main (String args[]) {
int date = 9;
Test test = new test ();
Test.change (date);
Birthdate d1= New Birthdate (7,7,1970);
}
public void Change1 (int i) {
i = 1234;
}
For the above code, date is a local variable, i,d,m,y is a local variable, and Day,month,year is a member variable. The following is an analysis of the changes in code execution:
1. The main method begins execution: int date = 9;
Date local variables, underlying types, references, and values all exist in the stack.
2. Test test = new test ();
Test is a reference to an object, where the object (new Test ()) exists in the heap.
3. Test.change (date);
I is a local variable, and references and values exist in the stack. When the method change execution completes, I will disappear from the stack.
4. Birthdate d1= new Birthdate (7,7,1970);
D1 is a reference to an object, where the object (new Birthdate ()) exists in the heap, where D,m,y is stored on the stack for local variables, and their types are underlying types, so their data is also stored on the stack. Day,month,year are member variables that are stored in the heap (new Birthdate ()). When the birthdate construction method is finished, the d,m,y disappears from the stack.
5. After the main method finishes, the date variable, the TEST,D1 reference disappears from the stack, new test (), and new birthdate () will wait for garbage collection.
Chang (Constant Pool)
Chang refers to some data that is determined at compile time and is saved in the compiled. class file.
The constant value (final) that contains the various base types defined in the code (such as int, long, and so on) and the object type (such as String and array) also contains some symbolic references that appear as text, such as:
The fully qualified name of the class and interface;
The name and descriptor of the field;
Method and name and descriptor.
If the compilation period has been created (as defined directly in double quotes), it is stored in a constant pool and stored in the heap if the runtime (new) is established. For equals equal strings, there is always only one copy in the constant pool, with multiple copies in the heap.
A string is a special wrapper class data. Can be used:
Java code
String str = new String ("abc");
String str = "ABC";
In two forms, the first is to create a new object with new (), which is stored in the heap. Each time a call is made, a new object is created. And the second is to create an object reference variable str in the stack on a string class, and then through the symbolic reference go to the string constant pool to find there is no "ABC", if not, then put "ABC" into the string constant pool, and make str point to "ABC", if there is already "ABC" Then direct str to "ABC".
When comparing the values in a class with the Equals () method, use = = when testing whether references to two wrapper classes are pointing to the same object, use the following example to illustrate the above theory.
Java code
String str1 = "abc";
String str2 = "abc";
System.out.println (STR1==STR2); True
You can see that str1 and str2 are pointing to the same object.
Java code
String str1 =new string ("abc");
String str2 =new string ("abc");
System.out.println (STR1==STR2); False
The way to new is to generate different objects. Generate one each time.
So the second way to create multiple "ABC" strings is that there is only one object in memory. This formulation is advantageous and saves memory space. At the same time it can improve the speed of the program to some extent, because the JVM will automatically determine whether it is necessary to create new objects based on the actual situation of the data in the stack. For code with string str = new String ("abc"), the new object is created in the heap, regardless of whether its string value is equal or not, and it is necessary to create a new object, which increases the burden on the program.
On the other hand, note that when we use a format-definition class such as String str = "ABC", we always assume that the object str of the string class was created. Worry about the traps! The object may not have been created! Instead, you might just point to an object that you have previously created. Only the new () method can be used to guarantee that one object is created each time.
Several examples of string constant pool problems
Example 1:
Java code
String s0= "Kvill";
String s1= "Kvill";
String s2= "kv" + "ill";
System.out.println (S0==S1);
System.out.println (S0==S2);
The result is:
true
Analysis: First of all, we have to know that the result of the Tao Java will ensure that a string constant has only one copy.
Because the "Kvill" in the S0 and S1 in the example are string constants, they are determined at compile time, so s0==s1 is true, and "kv" and "ill" are string constants, and when a string is concatenated by multiple string constants, it is definitely a string constant, So S2 is also parsed as a string constant at compile time, so S2 is also a reference to "Kvill" in a constant pool. So we come to s0==s1==s2;
Example 2:
Example:
Java code
Analysis: A string created with new string () is not a constant and cannot be determined at compile time, so the string created by new string () is not placed in a constant pool and has its own address space.
S0 or the application of "Kvill" in a constant pool, S1 is a reference to the new object "Kvill" created at run time because it cannot be determined at compile time, S2 is also a newly created object "Kvill" because it cannot be determined at compile time because it has the latter part new String ("ill"). , and you know how to get the results.
Example 3:
Java code
String a = "A1";
String B = "a" + 1;
System.out.println ((A = = b)); result = True
String a = "atrue";
String B = "a" + "true";
System.out.println ((A = = b)); result = True
String a = "a3.4";
String B = "a" + 3.4;
System.out.println ((A = = b)); result = True
Analysis: JVM for string constant "+" connection, the program compile period, the JVM will be the constant string of "+" connection optimization to the value after the connection, take "a" + 1, after the compiler optimization in class is already A1. The value of the string constant at compile time is determined, so the final result of the above program is true.
Example 4:
Java code
String a = "AB";
String BB = "B";
String B = "a" + BB;
System.out.println ((A = = b)); result = False
Analysis: JVM for string references, because there is a string reference in the string's "+" connection, and the referenced value is not determined at the program compile time, the "a" + BB cannot be optimized by the compiler, and only dynamically assigns and assigns the new address after the connection to B during the program run time. So the result of the above program is false.
Example 5:
Java code
String a = "AB";
Final String bb = "B";
String B = "a" + BB;
System.out.println ((A = = b)); result = True
Analysis: the only difference in [4] is the BB string with the final modification, which, for final-modified variables, is stored in its own constant pool or embedded in its bytecode stream at compile time as a local copy of a constant value. So at this point "a" + BB and "a" + "B" effect is the same. Therefore, the result of the above program is true.
Example 6:
Java code
String a = "AB";
Final String bb = GETBB ();
String B = "a" + BB;
System.out.println ((A = = b)); result = False
private static String GETBB () {return "B"; }
Analysis: JVM for string reference BB, its value can not be determined at compile time, the result of the above program is False if the method is dynamically connected and assigned the address of "a" after the procedure is invoked.
About string is immutable
It can be concluded from the example above that:
String s = "a" + "B" + "C";
is equivalent to string s = "abc";
String a = "a";
String B = "B";
String c = "C";
String s = a + B + C;
This is not the same, the end result is equal to:
Java code
StringBuffer temp = new StringBuffer ();
Temp.append (a). Append (b). append (c);
String s = temp.tostring ();
From the analysis above, it is not difficult to infer that string uses the Join operator (+) inefficiency reason analysis, in the form of such code:
Java code
public class Test {public
static void main (string args[]) {
string s = null;
for (int i = 0; i < i++) {
s + + ' a ';}}}
Every time you do it, you create a StringBuilder object and then throw it away after append. Recreate the StringBuilder object when the next loop arrives, and then append the string so that it loops until the end. If we use the StringBuilder object directly to append, we can save N-1 time to create and destroy objects. So for the application of string concatenation in the loop, the StringBuffer or Stringbulider object is generally used for append operation.
Because of the immutable nature of the string class, this one has to say a lot, as long as you know that a string instance once generated will not change again, for example: string str= "kv" + "ill" + "" + "ans"; Is that there are 4 string constants, first "KV" and "ill" generate "Kvill" in memory, and then "Kvill" and "" Generate "Kvill" exist in memory, and then "Kvill ans" is generated, and the address of this string is assigned to STR, It is because string "immutable" produces a lot of temporary variables, which is why it is suggested that StringBuffer be used because StringBuffer is changeable.
Final usage and understanding in string
Java code
Final StringBuffer a = new StringBuffer ("a");
Final StringBuffer B = new StringBuffer ("222");
a=b;//This sentence compiles not through
final stringbuffer a = new StringBuffer ("a");
A.append ("222");/compile through
It is visible that final is valid only for reference "value" (that is, memory address), forcing the reference to point only to the object to which it was initially directed, and changing its point will result in a compile-time error. Final is not responsible for the change in the object it points to.
Summarize
A reference (String, Array, object, and so on) of local variable data and objects used in the stack to hold some of the original data types, but no object content
The heap holds objects created using the New keyword.
A string is a special wrapper class, the references are stored on the stack, and the object content must be set differently depending on how it was created (Chang and heap). Some compile-time is already created, stored in a string constant pool, and some runtime is created. Use the new keyword to store in the heap.
This article discusses the java+ memory allocation and variable storage location of the difference is small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.