Java Memory allocation and management is one of the core technologies of java, before we introduced the Java memory management and memory leaks and Java garbage collection knowledge, today we go deep into the Java core, the detailed introduction of Java in memory allocation Knowledge. In general, Java involves the following areas in memory allocation:
Register: we can't control it in the program
Stack: a reference to a primitive type of data and objects, but the object itself is not stored in the stack, but is stored in the heap (new Object)
Heap: storing data generated with new
Static domain: static members that are stored in the object with static definitions
Constant Pool: Storing constants
Non-ram storage: Persistent storage space such as hard drives
Stacks in Java memory allocation
Some basic types of variable data and object reference variables that are defined in the function are allocated in the Function's stack memory.
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 frees the memory space allocated for the variable, and the memory space is immediately available for another use. The data size and life cycle in the stack can be determined, and the data disappears when no references point to the Data.
Heap in Java memory allocation
Heap memory is used to hold 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 an object in the heap, you can also define a special variable in the stack that is equal to the first address of the array or object in the heap memory, and this variable in the stack becomes the reference variable of the array or Object. A reference variable is a name that is an array or an object, and you can use reference variables from the stack to access the arrays or objects in the heap later in your Program. A reference variable is equivalent to a name that is an array or an object.
A reference variable is a normal variable that is allocated in the stack when defined, and the reference variable is freed after the program runs outside its scope. While the array and the object itself are allocated in the heap, the memory occupied by the array and the object itself is not freed when the program runs beyond the block of code that uses new to produce the array or Object's statement, and the array and object become garbage when no reference variable points to it, not in use, but still occupy memory space. The garbage collector takes off (releases) at a later indeterminate time. This is why the Java comparison accounts 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
The Java heap is a run-time data area in which objects allocate 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. Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use Data. however, The disadvantage is that the access speed is slower 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. however, The disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. The stack mainly contains some basic types of variable data (int, short, long, byte, float, double, boolean, Char) and object handle (reference).
Stack has a very important particularity, is that there is data in the stack can be shared. Let's say we define Both:
Example Code:
int a = 3; int B = 3;
The compiler processes int a = 3 first, it creates a reference to a variable in the stack, and then finds out if there is a value of 3 in the stack, and if it does not, it stores the 3 in and then points a to 3. then the int b = 3 is processed, and after the reference variable of B is created, because there are already 3 values in the stack, B points directly to 3. In this case, A and B both point to 3.
At this point, if you make a=4 again, then the compiler will re-search the stack for 4 values, if not, then store 4 in, and a point to 4; Therefore the change of a value does not affect the value of B.
It is important to note that this sharing of data with two object references also points to an object where this share is different, because the modification of a does not affect b, which is done by the compiler, which facilitates space saving. An object reference variable modifies the internal state of the object, affecting another object reference Variable.
Example Code:
int i1 = 9; int i2 = 9; int i3 = 9; public static final int INT1 = 9; public static final int INT2 = 9; public static final int INT3 = 9;
For member variables and local variables: member variables are external to the method, variables defined internally by the class, and local variables are variables defined inside a method or statement block. Local variables must be Initialized.
The formal parameter is a local variable, and the data for the local variable exists in the stack memory. Local variables in the stack memory disappear as the method Disappears.
Member variables are stored in objects in the heap and are collected by the garbage Collector.
such as the following code:
Example Code:
classBirthDate {Private intday ; Private intmonth; Private intyear ; publicBirthDate (intDintMintY) { day=d; Month=m; year=y; } //omit the Get,set method ...} public classtest{ public Static voidmain (String Args[]) {intDate = 9; Test test=NewTest (); Test.change (date); BirthDate D1=NewBirthDate (7,7,1970); } public voidChange1 (intI) {i= 1234; }
For this code, date is a local variable, i,d,m,y is a local variable, and Day,month,year is a member Variable. The following analysis of the code execution time changes:
1. The main method begins execution: int date = 9;
Date local variables, underlying types, references, and values are present in the STACK.
2. Test test = new test ();
Test is an object reference, exists in the stack, and the object (new Test ()) exists in the heap.
3. Test.change (date);
I is a local variable, and the reference and value exist in the STACK. When the method change execution is complete, I will disappear from the STACK.
4. BirthDate d1= New BirthDate (7,7,1970);
D1 is an object reference, exists in the stack, the object (new BirthDate ()) exists in the heap, where D,m,y is stored in the stack for local variables, and their type is the underlying type, so their data is also stored in the STACK. Day,month,year are member variables that are stored in the heap (new BirthDate ()). When the Birthdate construction method finishes executing, the d,m,y disappears from the Stack.
After the 5.main method executes, the date variable, test,d1 reference will disappear from the stack, new test (), 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.
In addition to the constant values (final) that contain the various basic types defined in the code (such as int, long, and so On) and object types (such as strings and arrays), there are also 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;
Methods and names and Descriptors.
If the compilation period has been created (defined directly in double quotation marks), it is stored in a constant pool, which is stored in the heap if the runtime (new) is Determined. for a string equal to equals, there is always only one copy in the constant pool, with multiple copies in the HEAP.
String is a special wrapper class Data. Can be used:
Example Code:
New String ("abc"= "abc";
Two forms, the first is to create new objects with new (), which is stored in the HEAP. A new object is created each time the call is Made. The second is to create an object reference to the string class in the stack str, and then through the symbol reference to the string constant pool to find there is no "abc", if not, the "abc" into the string constant pool, and the Str point to "abc", if there is already "abc" The STR directly points to "abc".
Use the Equals () method when comparing values within a class, and when testing two wrapper classes for reference to the same object, use = =, The following example illustrates the above theory.
Example Code:
String str1 = "abc"= "abc"; System.out.println (str1//true
You can see that str1 and str2 are pointing to the same object.
Example Code:
String str1 =new string ("abc"=new string ("abc"); System.out.println (str1// false
The new method is to generate different objects. Each time one is Generated.
So the second way to create multiple "abc" strings, in memory in fact there is only one object. This writing 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 data in the STACK. In the case of string str = new String ("abc"), The code creates a new object in the heap, regardless of whether the string value is equal or not, and it is necessary to create a new object, thereby aggravating the burden of the Program.
On the other hand, it is important to note that when you define a class using a format such as String str = "abc", you always want to assume, of course, that the object that created the string class is Str. Worry about traps! The object may not have been created! instead, it might just point to an object that was previously Created. Only through the new () method can you guarantee that a new object is created each Time.
Several examples of string constant pool problems
Example 1:
Example Code:
String s0= "kvill"; String S1= "kvill"; String s2= "kv" + "ill"; System.out.println (s0= =s1); System.out.println (s0= =s2); // The result is:truetrue
Analysis: first, We need to know that the result is that Java ensures that a string constant has only one copy.
Because the "kvill" in 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 also string constants, and when a string is concatenated by multiple string constants, It is itself a string constant. So S2 is also parsed as a string constant at compile time, so S2 is also a reference to "kvill" in the constant Pool. So we come to s0==s1==s2;
Example 2:
Example Code:
String s0= "kvill"; string S1=new string ("kvill"); String S2new string ("ill"); System.out.println (s0= =s1); System.out.println (s0= =s2); System.out.println (s1= =s2); // result: false false
Parsing: strings created with new string () are not constants and cannot be determined at compile time, so the strings created by new string () are not placed in the constant pool, they have their own address space.
S0 is also an application of "kvill" in a constant pool, S1 because it cannot be determined at compile time, it is a reference to the new object "kvill" created at run time, S2 because there is a second half part new String ("ill") so it cannot be determined at compile time, so it is also a newly created object "kvill" The application of the same, and understand that this will know why the result is Reached.
Example 3:
Example Code:
String a = "a1"= "a" + 1//= "atrue"= "a" + "true"//
= "a3.4"= "a" + 3.4//result = True
Analysis: JVM for string constant "+" number connection, The program compile period, the JVM will be the constant string "+" connection optimization to the concatenated value, take "a" + 1, The compiler is optimized in class is already a1. The value of its string constants is determined at compile time, so the final result of the above program is True.
Example 4:
Example Code:
String a = "ab"= "b"= "a" +//result = False
Analysis: JVM for string reference, because in the string "+" connection, there is a string reference exists, and the reference value in the program compilation period is not determined, that is, "a" + BB can not be optimized by the compiler, only during the program run time to dynamically allocate and the new address after the connection to B. So the result of the above program is also false.
Example 5:
Example Code:
String a = "ab"; Final String BB = "b"= "a" +//result = True
Analysis: the only difference between [4] is that the BB string has a final decoration, and for a final modified variable, it is parsed at compile time to a local copy of the constant value stored in its own constant pool or embedded in its byte stream. So at this point the "a" + BB and "a" + "b" effect is the Same. therefore, The result of the above program is True.
Example 6:
Example Code:
String a = "ab"; Final string bb == "a" +//result = Falseprivatestatic String GETBB () { c10/>return "b"; }
Analysis: the JVM for the string reference bb, its value in the compilation period can not be determined, only after the program run time call method, The return value of the method and "a" to dynamically connect and assign the address to b, so the result of the above program is False.
About string is immutable
From the above example, we can tell:
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 equals:
New= temp.tostring ();
From the above analysis results, it is not difficult to infer that the string using the join operator (+) inefficiency reason analysis, such as the Code:
Example Code:
public class Test { public staticvoid main (String args[]) { null' c10>; for (int i = 0; i <; i++) { + = "a"; }}}
Every time you do it, you produce a StringBuilder object and then throw it away after append. The next time the loop arrives, it re-generates the StringBuilder object and then append the string so that it loops until the end. If we use the StringBuilder object directly for append, we can save N-1 time to create and destroy Objects. So for applications that want to concatenate strings in a loop, the StringBuffer or Stringbulider objects are generally used for append Operations.
Because of the immutable nature of the string class, this is said a lot, as long as we know that the string instance once generated will not be changed, such as: string str= "kv" + "ill" + "+" ans "; There are 4 string constants, first "kv" and "ill" generate "kvill" in memory, and then "kvill" and "generate" Kvill "in memory, and finally generated" kvill ans ", and the address of the string to the str, This is because the "immutable" of string produces a lot of temporary variables, which is why it is recommended to use stringbuffer, because StringBuffer can be changed.
Final usage and understanding in string
Example Code:
Final New StringBuffer ("111"); Final New StringBuffer ("222"); a=b; // This sentence compilation does not pass Final New StringBuffer ("111"); a.append ("222"); // compiled by
As you can see, final is valid only for the reference "value" (that is, the memory address), which forces the reference to point only to the object that was initially pointed to, and changes its point to cause a compile-time error. Final is not responsible for the change in the object it points to.
Summarize
A reference to the local variable data and objects (String, array, object, and so on) that are used to hold some raw data types in the STACK. but not the object content
The heap contains objects created using the new Keyword.
A string is a special wrapper class whose reference is stored in the stack, and the object content must be set differently (Chang and Heap) depending on how it was created. some compile time has been created, stored in a string constant pool, and some runtime is created. use the New keyword to store in the heap.
Java memory allocation and variable storage location examples Explained