Java core memory allocation principle

Source: Internet
Author: User

Java core memory allocation principle

Reprinted: http://write.blog.csdn.net/postedit

Java memory allocation and management is one of the core technologies of Java. We have previously introduced Java memory management and Memory leakage and Java garbage collection knowledge. Today we go deep into Java core again, this section describes Java's knowledge about memory allocation in detail.

Java generally involves the following areas in memory allocation:

Register: We areProgramCannot be controlled

Stack:Stores basic types of data and object references, but the object itself is not stored in the stack, but stored in the heap

Heap:Store data generated with new

Static domain: Static members stored in objects defined using static

Constant pool: Stores constants.

Non-ram Storage: Permanent storage space such as hard disk

Stack in Java Memory Allocation

Variable data of some basic types defined in functions and referenced variables of objects are allocated in the function stack memory. WhenCodeWhen a block defines a variable,Java allocates memory space for this variable in the stack.,After the variable exits the scope,Java Automatically releases the memory space allocated for the variable., The memory space can be used for another use immediately.

Heap in Java Memory Allocation

Heap memory is used to store objects and arrays created by new.Memory allocated in heap,Managed by the automatic garbage collector of the Java Virtual Machine.

After an array or object is generated in the heap, you can define a special variable in the stack so that the value of this variable in the stack is equal to the first address of the array or object in the heap memory, the variable in the stack becomes the referenced variable of the array or object. The referenced variable is equivalent to an array or an object name. Later, you can use the referenced variable in the stack in the program to access the array or object in the heap. The referenced variable is equivalent to an array or object name.

The referenced variable is a common variable. It is assigned to the stack during definition. The referenced variable is released after the program runs out of its scope. Arrays and objects are allocated in the heap. Even if the program runs beyond the code block where new statements are used to generate arrays or objects, the memory occupied by arrays and objects is not released, arrays and objects become junk only when no referenced variable points to it. They cannot be used, but still occupy the memory space, the garbage collector collects (releases) the garbage collector at an uncertain time ). This is also the reason why Java accounts for memory usage.
In fact, the variables in the stack point to the variables in the heap memory. This is the pointer in Java!

Constant pool(Constant pool)

A constant pool refers to some data that is determined during the compilation period and stored in the compiled. Class file. Besides containing the basic types (such as int and long) defined in the Code and the constant values (final) of the object type (such as string and array) it also contains some symbolic references that appear in the form of text, such:

Full qualified names of classes and interfaces;

Field name and descriptor;

Method and name and descriptor.

The virtual machine must maintain a constant pool for each mounted type. A constant pool is an ordered set of constants used for this type, including direct constants (string, integer, and floating point constants) and symbolic references to other types, fields, and methods.

Stack memory and heap memory comparison

 Both stacks and stacks are places where Java is used to store data in the memory. Unlike C ++,Java automatic stack management and heapProgrammers cannot directly set stacks or stacks.

The Java heap is a runtime data zone from which objects are allocated space.The advantage of heap is that the memory size can be dynamically allocated.,The compiler does not have to be informed of the lifetime.,Because it dynamically allocates memory at runtime. The Java Garbage Collector automatically collects the unused data. However, the disadvantage is that the memory needs to be dynamically allocated at runtime,Slow access.

Stack advantages:,Faster access than heapSecond only to registers, 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 handle (that is, object reference ).

Stack has a very important particularity,That is, data in the stack 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,
Because the value 3 already exists in the stack, B is directed to 3. 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 point A 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.In this case, the modification of a does not affect B. It is completed by the compiler and is conducive to saving space. A variable referenced by an object modifies the internal state of the object, which affects the variable referenced by another object.

String Memory Allocation 

String is a special packaging data. Available:
String STR = new string ("ABC ");
String STR = "ABC ";
The first method is to use new () to create an object, which is stored in the heap. Each call creates a new object.
 The second is to first create a string class object in the stack to reference the variable STR, and then find whether the stack contains "ABC". If not, store "ABC" into the stack and point STR to "ABC". If "ABC" already exists, direct STR to "ABC ".

Use the equals () method to compare the values in a class. Use the = method to test whether the references of the two classes point to the same object. The example below illustrates the above theory.
String str1 = "ABC ";
String str2 = "ABC ";
System. Out. println (str1 = str2); // true
It can be seen that str1 and str2 point to the same object.
String str1 = new string ("ABC ");
String str2 = new string ("ABC ");
System. Out. println (str1 = str2); // false
The new method is used to generate different objects. Each time one is generated.
Therefore, the first method is used to create multiple "ABC" strings. Only one object exists in the memory. This method can save memory space. At the same time, it can improve the program running speed to a certain extent, because the JVM will automatically decide whether to create a new object based on the actual situation of the data in the stack. For the code of string STR = new string ("ABC");, a new object is created in the heap, regardless of whether the string value is equal, whether it is necessary to create a new object, this increases the burden on the program.

On the other hand, NOTE: When we use a format such as string STR = "ABC";, we always take it for granted that the STR object of the string class is created. Worry trap! The object may not be created! Instead, it may only point to a previously created object.
Only by using the new () method can a new object be created every time. Because the value of the string class is immutable, when the string variable needs to change its value frequently, you should consider using the stringbuffer or stringbuilder class to improve program efficiency.

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.