Java memory analysis [incisive]

Source: Internet
Author: User

In Java, data can be stored in six different places:
1. Register (register ). This is the fastest storage zone, because it is located in a different place from other storage areas-inside the processor. However, the number of registers is extremely limited, so the registers are allocated by the compiler as needed. You cannot directly control it or feel any signs of register in the program.
2. Stack ). It is located in generic Ram, but its "Stack pointer" can be used to obtain support from the processor. If the stack pointer moves down, new memory is allocated. If the stack pointer moves up, the memory is released. This is a fast and effective distribution storage method, second only to registers. When creating a program, the Java compiler must know the exact size and lifecycle of all data stored in the stack, because it must generate code to move the stack pointer up and down. This constraint limits the flexibility of the program, so although some java data is stored in the stack-especially object reference, Java objects are not stored in it.
3. Heap ). A universal memory pool (also exists in Ram) used to store Java objects. The benefit of heap is that the compiler does not need to know how many storage areas are allocated from the heap or how long the stored data remains in the heap. Therefore, it is flexible to allocate storage in the heap. When you need to create an object, you only need to write a new line of simple code. When you execute this line of code, the storage is automatically allocated in the heap. Of course, you must pay the appropriate code for this flexibility. It takes more time to store data by heap and stack.
4. static storage ). Here "static" refers to "at a fixed position ". Static storage stores the data that persists when the program is running. You can use the keyword static to identify the specific elements of an object as static, but Java objects are never stored in static buckets.
5. Constant storage ). Constant values are usually directly stored in the program code. This is safe because they will never be changed. Sometimes, in an embedded system, constants are separated from other parts, so in this case, you can choose to put them in the Rom.
6. Non-ram storage. If the data exists completely outside of the program, it can exist without any control of the program.

Thinking in Java

---------------------------------------------------------------------
A heap is a runtime data zone, and class objects are allocated space from it. These objects are created through new and do not require program code to be explicitly released. The heap is responsible for garbage collection. The advantage of the heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told in advance because the heap dynamically allocates memory at runtime, the Java Garbage Collector automatically collects the unused data. However, the slow access speed is due to the need to dynamically allocate memory during runtime. Objects and arrays in Java are stored in the heap.
The advantage of stack is that the access speed is faster than that of stack, second only to register, and stack data can be shared. However, the disadvantage is that the data size and lifetime in the stack must be fixed, and there is a lack of flexibility. The stack mainly stores some basic types of variables (, Int, short, long, byte, float, double, Boolean, char) and object references.
A very important feature of stacks is that data in stacks can be shared. Suppose we define both:
Int A = 3;
Int B = 3;
The compiler first processes int A = 3. First, it creates a reference with the variable A in the stack, and then finds whether the value 3 in the stack exists. If no value is found, store 3 and point A to 3. Then process int B = 3. After the referenced variable of B is created, B is directed to 3 because there is already 3 in the stack. In this way, both A and B point to 3 at the same time. At this time, if A is set to 4 again, the compiler will re-search whether there are 4 values in the stack. If not, it will store 4 and make a point to 4; if yes, direct a to this address. Therefore, changing the value of A does not affect the value of B. Note that the sharing of data is different from the sharing of two objects pointing to one object at the same time, because the modification of a does not affect B, which is completed by the compiler, it facilitates space saving. A variable referenced by an object modifies the internal state of the object, which affects the variable referenced by another object.

The above content is also excerpted from the Internet.

---------------------------------------------------------------------
Here are some examples:

Public class teststr {<br/> Public static void main (string [] ARGs) {<br/> // The following two statements create an object. "Fengshan" is stored in the String constant pool <br/> string str1 = "fengshan"; <br/> string str2 = "fengshan"; <br/> system. out. println (str1 = str2); // true </P> <p> // three objects are created in the following two statements. "Tianye" is stored in the String constant pool, and two new string () objects are stored in heap memory <br/> string str3 = new string ("Tianye "); <br/> string str4 = new string ("Tianye"); <br/> system. out. println (str3 = str4); // false </P> <p> // one object is created in the following two statements. 9 is stored in the stack memory <br/> int I = 9; <br/> Int J = 9; <br/> system. out. println (I = J); // true </P> <p> // The following two statements create an object. 1. The object is stored in the stack memory <br/> integer L = 1; // boxed <br/> integer k = 1; // boxed <br/> system. out. println (L = k); // true </P> <p> // because no packing is available, the following two statements create two objects. Two 1 objects are stored in heap memory <br/> integer L1 = new INTEGER (1); <br/> integer k1 = new INTEGER (1); <br/> system. out. println (L1 = k1); // false </P> <p> // The following two statements create an object. I1 and I2 variables are stored in the stack memory, and two 256 objects are stored in the heap memory <br/> integer I1 = 256; <br/> integer I2 = 256; <br/> system. out. println (I1 = I2); // false <br/>}< br/>}

 

For the last two examples of integer objects above, an instance is used for values between-128 and 127 during automatic packing.
The following is an example of the String constant pool:
String S1 = "AAA" + "BBB"; // an object is generated.
The constant value is determined during compilation. Here, both "AAA" and "BBB" are constants, so the value of variable S1 can be determined during compilation. After this line of code is compiled, the effect is equivalent:
String S1 = "aaabbb ";
Therefore, only one object "aaabbb" is created and saved in the string pool.

String str1 = "fengshan ";
String str2 = "fengshan ";
The preceding two statements only save one "fengshan" object in the constant pool.

String str3 = new string ("Tian e ");
String str4 = new string ("Tian e ");
The preceding two statements create three objects. First, create a "Tianye" object in the String constant pool, and then create two new string () objects in the heap memory, it stores references to the "Tianye" object.
In addition, "=" when determining an object, it is actually based on the address of the object in the stack to determine whether the object is the same, rather than based on the hashcode value.


The summary of hashcode and equal in Java string is better on the Internet. The record is as follows:
1. The reason for comparing repeated hashset values is a. hascode () = B. hascode () & A. Equals (B)
2. String hashcode basis: calculated based on the char [I] int value and the char [I] sort order algorithm (you can look at the source code ). Does not rely on string ref.
3. String equals: A = B | (A. Length = B. Length & {A [I] = B [I]})
4. Only when a = B is used for the comparison, the ref of the comparison is used. That is to say, the comparison between A and B is not the same object.
5. Conclusion: two strings with different ref may be considered the same element in the set.

---------------------------------------------------------------------
The following is an example code:

Class birthdate {<br/> private int day; <br/> private int month; <br/> private int year; <br/> Public birthdate (int d, int m, int y) {<br/> day = D; <br/> month = m; <br/> year = y; <br/>}< br/> omit get, set method... <Br/> Public void display () {<br/> system. out. println <br/> (day + "-" + month + "-" + year ); <br/>}</P> <p> public class test {<br/> Public static void main (string ARGs []) {<br/> test = new test (); <br/> int date = 9; <br/> birthdate d1 = new birthdate (7, 1970 ); <br/> birthdate D2 = new birthdate (2000,); <br/> test. change1 (date); <br/> test. change2 (D1); <br/> test. change3 (D2); <br/> d1.display (); <br/> d2.display (); <br/>}</P> <p> Public void Change1 (int I) {<br/> I = 1234; <br/>}</P> <p> Public void Change2 (birthdate B) {<br/> B = new birthdate, (2004); <br/>}</P> <p> Public void Change3 (birthdate B) {<br/> B. setday (22); <br/>}< br/>}

Memory analysis is as follows :{
Function onclick ()
{
Window. Open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. SRC)
}
} 'Src = "http://img1.51cto.com/attachment/200903/200903031236048228765.jpg" border = "0" alt = "">

Member variables: variables defined inside the class outside the method. Local variables: variables defined in the method or statement block. Paste a picture of the Program Execution Process (captured from Shang xuexiang ):{
Function onclick ()
{
Window. Open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. SRC)
}
} 'Src = "http://img1.51cto.com/attachment/200903/200903031236048258484.jpg" border = "0" alt = "">

This article from the "Qingshan" blog, please be sure to keep this source http://java999.blog.51cto.com/259217/134359

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.