[Java Basics] -- Memory Structure

Source: Internet
Author: User

There is no uniform statement about the memory structure of Java on the Internet, but the content is roughly the same. The core statement is the same and there is no problem.

Java memory is divided into five zones:

1. Heap (HEAP): the location where the instance is allocated. It is set through-XMS and-xmx.
2. methodarea (method region): class information and static variables. It corresponds to permanet generation and is set through-XX: permsize.
3. javastack (java stack): The Virtual Machine only performs two operations on javastack directly: frame-based pressure stack or outbound stack. Set Through-XSS. If not, stackoverflowerror will be thrown.
4. programcounter: Each thread has its own PC register, which is also created when the thread is started. The content of the PC register always points to the next address that will be executed. The address here can be a local pointer or offset corresponding to the start instruction of the method in the method area.
5. nativemethodstack: the address for saving the native method to enter the Region

In fact, we mainly discussHeap memoryAndStack memory

Heap memory is used to store the objects and arrays created by new, and 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 this variable in the stack is equal to the first address of the array or object in the heap memory, this variable in the stack becomes an array or object reference variable. Later, you can use the reference variable in the stack in the program to access the array or object in the stack, the referenced variable is equivalent to an array or an 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 ).

Some of the basic types of variables defined in the function and the referenced variables of the object are allocated in the function stack memory. When a variable is defined in a block of code, java allocates memory space for this variable in the stack. When the scope of the variable is exceeded, Java Automatically releases the memory space allocated for this variable, the memory space can be used as another memory immediately.

Do some people still do not understand what is heap memory and stack memory?
In fact, it is easy to remember:
The new object is stored in the heap (object) memory.
The basic data type is stored in the stack memory.
========================================================== ==========
PS: This explanation is slightly incorrect. For details about heap memory and stack memory allocation, see [Java basics-array]

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.