Java memory allocation (1)

Source: Internet
Author: User

Java memory allocation involves the following areas:

 

1. Register: unable to control in the program


2. STACK: stores basic data and object references, but the object itself is not stored in the stack, but stored in the heap.

 

3. Heap: store data generated with new


4. static fields: static members defined by static in objects


5. Constant pool: stores constants.


6. 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.

When a variable is defined in a code block, Java allocates memory space for the variable in the stack. When the variable exits the scope

 

Java will automatically release the memory space allocated for the variable, and 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. The memory allocated in the heap is managed by the Java Virtual Machine's automatic garbage collector.

 

.

 

After an array or object is generated in the heap, you can define a special variable in the stack so that the value of the variable in the stack is equal to the value of the array.

 

Or the first address of the 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 the number

 

A group or object name. Later, you can use the reference variable in the stack in the program.
Access
Array or object in the heap. When you reference a variable

 

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. Array and

 

Objects are allocated in the heap. Even if the program runs outside the code block where the new statement is used to generate arrays or objects

 

The memory occupied by the image itself will not be released. arrays and objects become junk only when no referenced variable points to it, and cannot be used,

 

But it still occupies the memory space and is removed (released) by the garbage collector at an uncertain time ). This is also a Java comparison

 

Memory usage.

  

In fact, the variables in the stack point to the variables in the heap memory, which is the pointer in Java.


 

Constant pool ):




A constant pool refers to some data that is determined during the compilation period and stored in the compiled. Class file. Except for

 

Yi
And the constant value (final) of the object type (such as string and array) also contains some

 

Shape
Symbol reference, 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. The constant pool is an ordered set of constants used for this type, including

 

Direct constants (string, integer, and floating point constants) and
Others
Type, field, and method symbol reference.

  

For a String constant, its value is in the constant pool. The constant pool in JVM exists in the memory as a table.

 

String type. A fixed-length constant_string_info table is used to store text string values. Note: This table only stores

 

Character string value, which is not used for symbol reference. Here, there should be a clear explanation of the storage location of string values in the constant pool.

 

Solved.

During program execution, the constant pool is stored in the method area rather than in the heap.

 

 

Stack and stack:



The Java heap is a runtime data zone and class (the object allocates space from it. These objects use new, newarray,

 

Commands such as anewarray and multianewarray are created, and they do not require program code to be explicitly released. Garbage collection is responsible for the heap.

 

The advantage of heap is that the memory size can be dynamically allocated, and the lifetime does not need to be told to the compiler in advance because it is dynamically allocated during runtime.

 

Java Garbage Collector automatically collects the data that is no longer in use. However, the disadvantage is that the memory needs to be dynamically allocated at runtime,

 

Slow access speed.

  

 

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 in the stack exists.

 

And the survival time must be determined and lacks flexibility. The stack mainly stores some basic types of variable data (INT, short, long, byte,

 

Float, double, Boolean, char) and object handle (reference ).

  

A very important feature of stacks is that data in stacks can be shared. Suppose we define both:




  1. Int

    A = 3

    ;

  2. 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 exists in the stack,

 

If not, store 3 and point A to 3. Then process int B = 3. After the referenced variable of B is created

 

And B is directed to 3. In this way, both A and B point to 3 at the same time.

  

At this time, if a = 4 again, the compiler will re-search whether there are 4 values in the stack. If not, it will store 4 and make

 

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 such data is different from the sharing of two objects pointing to one object at the same time because a's repair

 

The change does not affect B. It is completed by the compiler and is conducive to space saving. A variable referenced by an object modifies the internal structure of this object.

 

Status, which affects the variables referenced by another object.

 


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.