Detailed explanations of Java heap, Stack and Chang, and related string

Source: Internet
Author: User
Tags format definition

One: in java, There are six different places where data can be stored:

1. Register (register). This is the fastest storage area because it is located in a different location from the other store-the inside of the Processor. however, the number of registers is extremely limited, so the registers are allocated by the compiler on Demand. You cannot directly control or feel any sign of register presence in the Program.

------the Fastest Storage area, which is allocated by the compiler on demand, is beyond our control in the Program.

2. Stacks (stack). is located in Universal ram, but it can be supported from the processor with its "stack pointer". If the stack pointer moves down, The new memory is allocated, and if you move up, the memory is Freed. This is a fast and efficient method of allocating storage, second only to Registers. When you create a program, the Java compiler must know the exact size and lifecycle of all the data stored in the stack, because it must generate the appropriate code to move the stack pointer up and DOWN. This constraint limits the flexibility of the program, so while some Java data is stored on the Stack-especially object references-the Java object does not store it.

------a reference to an array of variable data and objects of the underlying type, but the object itself is not stored in the stack, but is stored in the heap (the new Object) or in a constant pool (the string constant object is stored in a constant Pool)

3. heap. A universal memory pool (also available in Ram) for storing so Java Objects. The benefit of a heap that differs from the stack is that the compiler does not need to know how many storage areas to allocate from the heap, or how long the stored data survives in the HEAP. therefore, there is great flexibility in allocating storage in the HEAP. When you need to create an object, you only need new to write a simple line of code, and when you execute this line of code, it will be automatically stored in the heap Allocation. of course, for this flexibility you have to pay the code ACCORDINGLY. Storage allocation with heaps takes more time than storage storage with the STACK.

------to Store all new Objects.

4. Static Storage (storage). "static" here means "in a fixed position". The data stored in the static store that persists while the program is Running. You can use the keyword static to identify a particular element of an object that is static, but the Java object itself never resides in a static storage space.

------to store static members (statically Defined)

5. Constant Storage (constant storage). Constant values are usually stored directly inside the program code, and it is safe to do so because they are never changed. sometimes, in an embedded system, the constants themselves are separated from the rest, so in this case, you can choose to place them in ROM

------storing string constants and basic type constants (public static Final)

6. Non-ram Storage. If the data is completely outside of the program, it can be free from any control of the program and can exist when the program is not running.

------hard disk, such as permanent storage space for speed, has the following relationship:

Registers > Stacks > Heaps > Other

Here we are primarily concerned with stacks, heaps, and constant pools, which can be shared for objects in stacks and constant pools, and not for objects in the HEAP.

The data size and life cycle in the stack can be determined, and the data disappears when no references point to the Data. The garbage collector is responsible for the collection of objects in the heap, so the size and life cycle do not need to be determined and have great Flexibility.

For strings: references to their objects are stored in the stack, and if the compilation period has been created (defined directly in double quotes), it is stored in a constant pool, which is stored in the heap if the Run-time (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.

Code like this: 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");

Explain here that for a string to be generated by new (assuming "china"), The constant pool is first searched for whether the "china" object is already in place, and if none is created in the constant pool, then a copy object of this "china" object in the constant pool is then created in the HEAP.

That's the way to interview: string s = new string ("xyz"), How many objects are produced?

one or two of Them. If there is no "xyz" in a constant pool, it is two. If there is "xyz" in the original constant pool, it is one.

For variables and constants of the underlying type: variables and references are stored in the stack, and constants are stored in the constant Pool.

Code like this: 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: member variables are external to the method, variables defined internally by the class;

A local variable is a variable 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.

Code like this: 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 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.

Java heap, stack, and constant pool details (ii.)

1. Stacks and heaps (heap) are places that Java uses to store data in Ram. Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set up stacks or heaps.

2. The advantage of the stack is that the access speed is faster than the heap, second only to the registers directly in the CPU. however, The disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. In addition, the stack data can be shared, see 3rd.

The advantage of the heap is that the memory size can be allocated dynamically, and the lifetime does not have to tell the compiler beforehand that the Java garbage collector automatically collects the data that is no longer in Use. however, The disadvantage is that the access speed is slower due to the dynamic allocation of memory at run Time.

3. There are two kinds of data types in Java.

One is the basic type (primitive types), a total of 8 kinds, namely int, short, long, byte, float, double, boolean, char (note, and no basic type of string). The definition of this type is through such as int a = 3; Long B = 255L; the form to be defined, called an automatic variable. It is worth noting that the automatic variable is a literal value, not an instance of a class, that is not a reference to a class, there is no class Here. such as int a = 3; Here A is a reference to the int type, pointing to the literal value of 3. The data of these literals, due to the size of the known, the lifetime of the known (these values are fixed in a program block, the program block exits, the field value disappears), for the sake of speed, it exists in the STACK. In addition, the stack has a very important particularity, is that there is data in the stack can be shared.   Suppose we define int a = 3 at the same time; int b = 3; the compiler processes int a = 3 first, it creates a reference to a variable in the stack, and then looks for an address with a literal value of 3, and then finds an address that holds the literal value of 3, and then points A to the address of 3. then the int b = 3 is processed, and after the reference variable of B is created, B is pointed directly to the address of 3 because there are already 3 literals in the STACK. In this case, A and B both point to 3. It is particularly important to note that the reference to this literal is different from the reference to the class Object. Assuming that a reference to two class objects points to an object at the same time, if an object reference variable modifies the internal state of the object, then another object reference variable will immediately reflect that Change. conversely, modifying its value by a reference to a literal value does not result in another case where a reference to that literal is Changed. As in the example above, we define the value of a and B and then make a=4; then B will not be equal to 4 or equal to 3. Inside the compiler, when it encounters a=4, it will re-search the stack for a literal value of 4, and if not, re-open the value of the address 4, and if so, point a directly at the Address. Therefore the change of a value does not affect the value of B.

The other is the wrapper class data, such as integer, String, double, and so on, the corresponding basic data types are wrapped up class. These classes of data all exist in the heap, and Java uses the new () statement to tell the compiler that it is dynamically created as needed at run time, so it is more flexible, but the disadvantage is that it takes more time.

Here are some examples: Java code

public class Test {

public static void main (string[] Args)

{

int a1=1;

int b1=1;

int c1=2;

int d1=a1+b1;

Integer A = 1;

Integer B = 2;

Integer C = 3;

Integer d = 3;

Integer e = 321;

Integer f = 321;

Long g = 3L;

System.out.println (a1==b1); True result 1

System.out.println (c1==d1); True result 2

System.out.println (c==d); True result 3

System.out.println (e==f); False result 4

}

}

Analysis:

The result 1:a1==b1 as described above, will open up storage space in the stack to store Data.

Result 2: First it will create a reference in the stack with a variable of c1, and then find an address with a literal value of 2, not found, open a 2 of the value of the address, and then the C1 point to 2 address, D1 for two literal added also 2, because in the stack has 2 this literal value, The D1 points directly to the address of 2.           In this way, the C1 and D1 both point to 3. Before analyzing the results below let's take a look at the Java auto-unpacking and boxing: when auto-boxing, when int becomes integer, there are rules, when the value of your int is -128-integercache.high (127), Instead of returning an integer object that is new, it is an integer object that has been cached in the heap (we can understand that the system has cached an integer from 128 to 127 into an integer array, If you want to turn an int into an integer object, first go to the cache, find the words to return the reference directly to you, no need to new one, if not -128-integercache.high (127) Returns an integer object that is New.

Result 3: since 3 is within range so the data is taken from the cache, and C and D point to the same object, the result is true;

Result 4: because 321 is not in the range so the data is not taken from the cache but there is a new object alone, E and F do not point to the same object, the result is false;

4. String is a special wrapper class Data. That is, it can be created in the form of string str = new string ("abc"), or in the form of string str = "abc" (in contrast, before JDK 5.0, you have never seen an expression of integer i = 3; because class and literal literals are not can be generic except for string. In JDK 5.0, this expression is possible! Because the compiler is converting the integer i = new integer (3) in the Background. The former is the process of creating a canonical class, that is, in java, everything is an object, and the object is an instance of the class, all created in the form of new (). Some classes in Java, such as the DateFormat class, can return a newly created class through the Class's getinstance () method, which seems to violate this Principle. actually otherwise The class uses a singleton pattern to return an instance of the class, except that the instance is created inside the class through new (), and getinstance () hides this detail from the Outside. So why is the case in string str = "abc", not created by new (), a violation of the above principle? Not really.

4 (1) String str = "abc" procedure for creating an object 1 First look in the constant pool for the existence of the content as "abc" string Object 2 If it does not exist, create "abc" in the constant pool and let Str refer to the object 3 if it exists, let str refer to the object directly

As to how "abc" is saved, where is it stored? The constant pool is part of the class information, and the class information is reflected in the JVM memory model that corresponds to the method area that exists in the JVM memory model, meaning that the constant pool concept in this class information exists in the method area, and the method area is allocated by the JVM in the heap in the JVM memory model, so "abc" Can be said to exist in the heap (and some data, in order to distinguish the heap of the method area from the JVM heap, the method area is called the stack). In this case, "abc" is written to the bytecode at compile time, so when class is loaded, the JVM allocates memory for "abc" in the constant pool, so it is similar to the static ZONE.

4 (2) String str = new String ("abc") creates an instance of the procedure 1 first in the heap (not the constant Pool) to create a specified object "abc" and to have the STR reference point to the object 2 is viewed in the string constant pool, if there is a content of "abc" string Object 3 if present , the new string object is associated with the object in the string constant pool 4 if it does not exist, a string object with the content "abc" is created in the string constant pool, and the object in the heap is associated with the Intern method can return a reference to the object of the string in the constant Pool. You can test the Java code simply by following the code

Class Stringtest {

public static void main (string[] Args) {

String str1 = "abc";

String str2 = new string ("abc"). intern ();

System.out.println (str1==str2);

}

}

An initially empty string pool, which is privately maintained by the class String. When the Intern method is called, if the pool already contains a string equal to this string object (as determined by the equals (Object) method), the string in the pool is Returned. otherwise, This string object is added to the pool and a reference to this string object is Returned.   It follows the following rules: for any two strings s and t, s.intern () = = T.intern () is true if and only if S.equals (t) is True. So string str1 = "abc", str1 refers to the object of the constant pool (method area), and string str2 = new string ("abc"), str2 refers to the object in the heap, so the memory address is different, but the content is the same, so = = is false, and Equals is True

4 (3) String str1 = "abc"; String str2 = "ab" + "c"; STR1==STR2 is ture because string str2 = "ab" + "c" will look in the constant pool when there is content as "abc" string object, if there is a direct let str2 refer to the object, obviously string str1 = "abc" when it says, An "abc" object is created in the constant pool, so str1 references the object, str2 also references the object, so STR1==STR2

4 (4) String str1 = "abc"; String str2 = "ab"; String STR3 = str2 + "c"; STR1==STR3 is false because string str3 = str2 + "c" involves the addition of variables (not all constants), So a new object is generated, and its internal implementation is the first new one stringbuilder, then append (str2), Append ("c"); then let STR3 refer to the object returned by ToString () if you want to know more details, you can view the Anti-compilation code yourself, and see that the Anti-compilation code can be used with javap.

That is, javap-c-verbose the class file (. Class) to view

Example of the above code

Javac stringtest.java//compiling

Javap-c-verbose Stringtest//decompile

4 (5) String str1 = "abc";

String str2 = "abc";

System.out.println (str1==str2); True Note that

We do not use str1.equals (str2) in this way, as this will compare the values of two strings for Equality. = = number, as described in the jdk, returns true only if two references point to the same object. And what we're looking at here is whether str1 and str2 all point to the same object. The result shows that the JVM created two references str1 and str2, but only one object was created, and two references pointed to the Object.

4 (6) String str1 = "abc";

String str2 = "abc";

STR1 = "bcd";

System.out.println (str1 + "," + str2); bcd, ABC

System.out.println (str1==str2); False This means that changes in the assignment result in a change in the class object reference, and str1 points to another new object! And str2 still points to the original Object. In the example above, when we change the value of str1 to "bcd", the JVM discovers that there is no address for that value in the constant pool, opens up this address and creates a new object whose string value points to the Address. In fact, the String class is designed to be immutable (immutable) Classes. If you want to change its value, yes, but the JVM silently creates a new object at run time based on the new value, and then returns the address of the object to the reference of the original class. This creation process is entirely automatic, but it takes up more time. In the environment that is more sensitive to time requirements, it will have some adverse effects.

4 (7)

String str1 = "abc";

String str2 = "abc";

STR1 = "bcd";

String STR3 = str1;

System.out.println (str3); Bcd

String STR4 = "bcd";

System.out.println (STR1 = = str4); True STR3 the Object's reference points directly to the object pointed to by str1 (note that STR3 does not create a new object). When STR1 changes its value, it creates a reference str4 of string and points to the new object created by str1 modifying the Value. It can be found that this time STR4 also did not create a new object, thereby re-sharing the data in the STACK.

4 (8)

Let's look at the following code Again.

String str1 = new String ("abc");

String str2 = "abc";

System.out.println (str1==str2); False creates two References. Two objects were created.   Two references point to a different two objects, respectively.   String str1 = "abc";   String str2 = new String ("abc");  System.out.println (str1==str2); False creates two References. Two objects were created. Two references point to a different two objects, respectively. The above two code shows that as long as new () is used to create the object, it is created in the heap, and its string is stored separately, even if the data in the stack is the same, it is not shared with the data in the STACK.

5. The value of the data type wrapper class cannot be Modified. Not only the value of the string class cannot be modified, but all data type wrapper classes cannot change their internal values.

6. Conclusions and Recommendations:

(1) when we use a format definition class such as String str = "abc", We always want to think of course that we created the object str of the string class. Worry about traps! The object may not have been created! The only certainty is that a reference to the string class was Created. As to whether the reference is pointing to a new object, it must be considered in terms of context, unless you create a new object with a prominent way through the new () Method. therefore, It is more accurate to say that we have created a reference variable to the object of the string class str, which refers to a variable that points to a string class with the value "abc". Being aware of this is helpful in troubleshooting bugs that are difficult to find in a Program.

(2) the use of string str = "abc", in a way that can improve the speed of the program to a certain extent, because the JVM will automatically based on the actual data in the stack to determine whether it is necessary to create a new object. 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. This idea should be the idea of the meta-mode, but it is not known whether the internal JDK implements this Pattern.

(3) Use the Equals () method when comparing the values in the wrapper class, and use the = = when testing whether the references to the two wrapper classes point to the same object.

(4) because of the immutable nature of the string class, you should consider using the StringBuffer class when a string variable needs to change its value frequently to improve program efficiency

Z reprint!

Detailed explanations of Java heap, Stack and Chang, and related string

Related Article

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.