Analysis of concepts such as heap, stack, and constant pool in Java

Source: Internet
Author: User
Analysis of concepts such as heap, stack, and constant pool in Java

ProgramDuring runtime, we 'd better know where the data is stored. Pay special attention to the memory allocation. Data can be stored in six places:
(1) registers. This is the fastest Storage AreaBecause it is located in a different place than all other storage methods: Inside the processor. However, the number of registers is very limited, so the registers are allocated by the compiler as needed. We have no direct control over this, and it is impossible to find any trace of the Register in our program.

(2) stack ). Stores basic types of variable data and object references, but the object itself is not stored in the stack, but stored in the heap (new object) or constant pool (String constant objects are stored in the constant pool .)It resides in the regular RAM (Random Access to memory) region, but can be directly supported by its "Stack pointer. If the stack pointer moves downward, a new memory is created. If it moves upward, the memory is released. This is a fast and efficient data storage method, second only to registers. When creating a program, the Java compiler must accurately know the "length" and "time of existence" of all data stored in the stack ". This is because it must generate the correspondingCodeTo move the pointer up or down. This restriction undoubtedly affects the flexibility of the program. Although some java data must be stored in the stack, especially the object handle, Java objects are not put in it.

(3) heap ). Store all new objects, A general-purpose Memory Pool (also in the ram region), which stores Java objects. Unlike the stack, the most attractive thing about "memory Heap" or "Heap" is that the compiler does not have to know how much storage space is allocated from the heap, you do not need to know how long the stored data will stay in the heap. Therefore, it is more flexible to store data in a heap. When creating an object, you only need to use the new command to compile the relevant code. When the code is executed, data is automatically saved in the heap. Of course, to achieve this flexibility, you must pay a certain price: It will take longer to allocate storage space in the heap!

(4) static storage. Stores static members (defined in static mode ),Here "static" refers to "in a fixed position" (although also in Ram ). During the running of the program, the static storage data will be waiting for calling at any time. The static keyword can be used to indicate that a specific element of an object is static. However, Java objects are never placed in static buckets.

(5) constant storage. Stores string constants and basic type constants (public static final ).Constant values are usually placed directly inside the program code. This is safe because they will never change. Some constants must be strictly protected, so you may consider placing them in read-only memory (ROM ).

(6) Non-ram storage.If the data is completely independent from a program, the program still exists when it is not run and is out of the control scope of the program. Two major examples are "stream object" and "fixed object ". For stream objects, the object is converted into a byte stream, which is usually sent to another machine. For fixed objects, objects are stored on disks. Even if the program stops running, they can remain in their own State. A particularly useful technique for these types of data storage is that they can be stored in other media. Once needed, they can even be restored to common, Ram-based objects. Java 1.1 supports lightweight persistence. Future versions may even provide more complete solutions

 

Here we mainly care about stacks, stacks, and constant pools. Objects in stacks and constant pools can be shared, but objects in stacks cannot be shared. The data size and lifecycle in the stack can be determined. When no reference points to the data, the data will disappear. The object in the heap is recycled by the garbage collector. Therefore, the size and lifecycle of the object do not need to be determined, so it has great flexibility.
For strings: the object references are stored in the stack. If a string has been created during the compilation period (defined in double quotation marks), it is stored in the constant pool, if it is confirmed during the runtime (new), it will be stored in the heap. For strings with equal equals, there is always only one copy in the constant pool, and there are multiple copies in the heap.
Run the following code:

Java code

String S1 = "China ";
String S2 = "China ";
String S3 = "China ";
String SS1 = new string ("China ");
String ss2 = new string ("China ");
String ss3 = new string ("China ");
 

Here, we will explain the yellow arrows. When a new string (assumed as "China") is generated, we will first go to the constant pool to find whether the "China" object exists, if not, create a copy object for this "China" object in the constant pool, and then create a copy object for this "China" object in the heap. This is an interview question: String S = new string ("XYZ"); how many objects are generated? One or two, if there is no "XYZ" in the constant pool, it is two.

For basic types of variables and constants: variables and references are stored in the stack, and constants are stored in the constant pool.
Run the following code:

Java 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: The member variables are the external methods, the variables defined inside the class, and the local variables are the variables defined inside the method or statement block. Local variables must be initialized.
The formal parameter is a local variable, and the data of the local variable is stored in the stack memory. The local variables in the stack memory disappear as the method disappears.
The member variables are stored in the heap object and are recycled by the garbage collector.
Run 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;
}< br> omit get, set method .........
}

public class test {
Public static void main (string ARGs []) {
int date = 9;
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, and I, d, M, and Y are all member variables with the form parameters as local variables, day, month, and year. The following describes the changes in code execution:
1. Run the main method: int date = 9;
Date local variables, basic types, references, and values all exist in the stack.
2. Test test = new test ();
Test is an object reference. It 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. After the method change is executed, I disappears from the stack.
4. Birthdate d1 = new birthdate (1970 );
D1 is an object reference and is in the stack. The object (New birthdate () is in the heap, where D, M, and Y are local variables stored in the stack, and their types are the basic types, so their data is also stored in the stack. Day, month, and year are member variables, which are stored in the heap (New birthdate ). After the birthdate constructor is executed, D, M, and Y will disappear from the stack.
5. After the main method is executed, the date variables, test, and D1 references will disappear from the stack. New Test () and new birthdate () will wait for garbage collection.

Analysis of concepts such as heap, stack, and constant pool in Java

When running the program, we 'd better know where the data is saved. Pay special attention to the memory allocation. Data can be stored in six places:
(1) registers. This is the fastest Storage AreaBecause it is located in a different place than all other storage methods: Inside the processor. However, the number of registers is very limited, so the registers are allocated by the compiler as needed. We have no direct control over this, and it is impossible to find any trace of the Register in our program.

(2) stack ). Stores basic types of variable data and object references, but the object itself is not stored in the stack, but stored in the heap (new object) or constant pool (String constant objects are stored in the constant pool .)It resides in the regular RAM (Random Access to memory) region, but can be directly supported by its "Stack pointer. If the stack pointer moves downward, a new memory is created. If it moves upward, the memory is released. This is a fast and efficient data storage method, second only to registers. When creating a program, the Java compiler must accurately know the "length" and "time of existence" of all data stored in the stack ". This is because it must generate the corresponding code to move the pointer up and down. This restriction undoubtedly affects the flexibility of the program. Although some java data must be stored in the stack, especially the object handle, Java objects are not put in it.

(3) heap ). Store all new objects, A general-purpose Memory Pool (also in the ram region), which stores Java objects. Unlike the stack, the most attractive thing about "memory Heap" or "Heap" is that the compiler does not have to know how much storage space is allocated from the heap, you do not need to know how long the stored data will stay in the heap. Therefore, it is more flexible to store data in a heap. When creating an object, you only need to use the new command to compile the relevant code. When the code is executed, data is automatically saved in the heap. Of course, to achieve this flexibility, you must pay a certain price: It will take longer to allocate storage space in the heap!

(4) static storage. Stores static members (defined in static mode ),Here "static" refers to "in a fixed position" (although also in Ram ). During the running of the program, the static storage data will be waiting for calling at any time. The static keyword can be used to indicate that a specific element of an object is static. However, Java objects are never placed in static buckets.

(5) constant storage. Stores string constants and basic type constants (public static final ).Constant values are usually placed directly inside the program code. This is safe because they will never change. Some constants must be strictly protected, so you may consider placing them in read-only memory (ROM ).

(6) Non-ram storage.If the data is completely independent from a program, the program still exists when it is not run and is out of the control scope of the program. Two major examples are "stream object" and "fixed object ". For stream objects, the object is converted into a byte stream, which is usually sent to another machine. For fixed objects, objects are stored on disks. Even if the program stops running, they can remain in their own State. A particularly useful technique for these types of data storage is that they can be stored in other media. Once needed, they can even be restored to common, Ram-based objects. Java 1.1 supports lightweight persistence. Future versions may even provide more complete solutions

 

Here we mainly care about stacks, stacks, and constant pools. Objects in stacks and constant pools can be shared, but objects in stacks cannot be shared. The data size and lifecycle in the stack can be determined. When no reference points to the data, the data will disappear. The object in the heap is recycled by the garbage collector. Therefore, the size and lifecycle of the object do not need to be determined, so it has great flexibility.
For strings: the object references are stored in the stack. If a string has been created during the compilation period (defined in double quotation marks), it is stored in the constant pool, if it is confirmed during the runtime (new), it will be stored in the heap. For strings with equal equals, there is always only one copy in the constant pool, and there are multiple copies in the heap.
Run the following code:

Java code

String S1 = "China ";
String S2 = "China ";
String S3 = "China ";
String SS1 = new string ("China ");
String ss2 = new string ("China ");
String ss3 = new string ("China ");
 

Here, we will explain the yellow arrows. When a new string (assumed as "China") is generated, we will first go to the constant pool to find whether the "China" object exists, if not, create a copy object for this "China" object in the constant pool, and then create a copy object for this "China" object in the heap. This is an interview question: String S = new string ("XYZ"); how many objects are generated? One or two, if there is no "XYZ" in the constant pool, it is two.

For basic types of variables and constants: variables and references are stored in the stack, and constants are stored in the constant pool.
Run the following code:

Java 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: The member variables are the external methods, the variables defined inside the class, and the local variables are the variables defined inside the method or statement block. Local variables must be initialized.
The formal parameter is a local variable, and the data of the local variable is stored in the stack memory. The local variables in the stack memory disappear as the method disappears.
The member variables are stored in the heap object and are recycled by the garbage collector.
Run 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;
}< br> omit get, set method .........
}

public class test {
Public static void main (string ARGs []) {
int date = 9;
test = new test ();
test. change (date);
birthdate d1 = new birthdate (7, 7, 1970);
}

Public void Change1 (int I) {
I = 1234;
}< BR >}< br>
for the above Code, date is a local variable, and I, d, M, and Y are all member variables. The following describes the changes in code execution:
1. run the main method: int date = 9;
date local variable, basic type, reference value, and so on exist in the stack.
2. Test test = new test ();
test is an object reference. It 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. After the method change is executed, I disappears from the stack.
4. birthdate d1 = new birthdate (1970,);
d1 is an object reference and exists in the stack. The object (New birthdate () exists in the heap, where D, m, Y is a local variable stored in the stack, and their type is the basic type, so their data is also stored in the stack. Day, month, and year are member variables, which are stored in the heap (New birthdate ). After the birthdate constructor is executed, D, M, and Y will disappear from the stack.
5. After the main method is executed, the date variables, test, and D1 references will disappear from the stack. New Test () and new birthdate () will wait for garbage collection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.