A brief analysis of Java memory management

Source: Internet
Author: User

As a Java programmer, because of the automatic memory management of the virtual machine, so there is no need to write to the C and C + + programmers like the disaster area of the delete and the free method, but there is no memory leak in Java, the answer is no, there is a memory leak in Java. So we need to understand how the virtual machine uses memory.

Java virtual machines in the process of executing Java programs will be managed to partition the memory of different areas for different purposes, and some regions as the virtual machine process started, some regions are dependent on the user thread start and end to build and destroy.

Often many people simply divide the memory of Java into a heap, stack, static area, such a division is very inaccurate, the division of Java memory area is far more complex than this. But these two memory areas are often the memory areas that programmers are most concerned about
According to the Java Virtual Machine Specification (2nd edition), the memory managed by the Java Virtual machine will include the following runtime data regions.
1. Method area
2. Heap Area
3. Virtual Machine Stack Area
4. Local Method Stack
5. Program counter

Method Area:
The method area, like the Java heap, is an area of memory shared by each thread that stores data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine. Although the Java Virtual Machine specification describes the method area as a logical part of the heap, it has an alias called Non-heap (Not a heap), which should be distinguished from the Java heap.


Java Virtual machine stack area:
Used to store local variables (eight basic data types in Java, Boolean, Byte, char, short, int, float, long, double), object reference (reference type, which is not equivalent to the object itself, according to different virtual machine implementations, It may be a reference pointer to the starting address of an object, or it may point to a handle to the object or other location related to the object, operation Stacks, dynamic links, method exits and other information, each method is called directly to execute the completion of the process, corresponding to a stack frame in the virtual machine stack from the path of the stack from the process.


Local method Stack:
The local methods Stack (Native method Stacks) is very similar to the virtual machine stack, except that the virtual machine stack executes Java methods (that is, bytecode) services for the virtual machine, while the local method stack serves the Native method used by the virtual machine. The language, usage, and data structure of the methods used in the local method stack in the virtual machine specification are not mandatory, so the virtual machine can implement it freely.


Heap Area (Java heap):
The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. Objects are stored in the stack, and an instance of the object is present in the heap. The description in the Java Virtual Machine specification is that all object instances and arrays are allocated on the heap.
In order to facilitate the Java implementation of different generations to take the garbage collection algorithm, but also for the garbage collection algorithm needs, the heap area memory also has very thin division.
The heap can be divided into three generations (different virtual machines also have different divisions):
Cenozoic younggeneration
Old age Oldgeneration
Permanent Generation Permanentgeneration

The Younggeneration generation consists of an Eden (a new generation zone) and two survivor (which belong to the Cenozoic, in order to replicate the algorithm). The memory of the newly created object is allocated from Eden. Two survivor space always have a piece is free, used as the target space of copying collection. The process of Minor collection is to copy Eden and the live object in survivor space into the free survivor space.

The Oldgeneration generation consists of a tenured, which, like the younggeneration generation, is meant to hold memory objects generated by the Java class. Each object has an object age counter. After the object was collected by Eden into the Survivor area, age +1. After the generation of GC, age +1. In turn, when age >=15 into the old years.

The Permanentgeneration generation consists of a permanent area for storing immutable objects such as classes, methods, strings, and so on.
Note: There are no permgen in java8, such as intern strings, which have been moved to the normal heap in Java 7.
Java garbage Collection sub-level, 0-level for all garbage collection, will be reclaimed in the old field of garbage, Level 1 or above is a partial garbage collection, only the garbage collected in young, memory overflow usually occurs in old or perm segment garbage collection, still no memory space

Program counter:
is a small amount of memory space that can be used as the line number indicator of the bytecode that is being executed by the current thread. In the virtual machine concept model (only the conceptual model, the various virtual machines may be implemented in some more efficient way), the bytecode interpreter works by changing the value of this counter to select the next need to execute the bytecode instruction, branch, loop, jump, exception handling, Basic functions such as thread recovery need to rely on this counter to complete.

A brief analysis of Java memory management

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.