Java Automatic memory management mechanism

Source: Internet
Author: User

Java programmers hand over the memory management to the virtual machine, and if there is a memory leak or overflow problem, if you do not know how the memory works, then troubleshooting will be an extremely difficult task.

Java memory area and memory overflow exception

Java Runtime data region partitioning:

Thread-Isolated

1. Procedure counter (program Counter Register)

The line number indicator of the current thread execution code, when the thread switches and allocates the processor execution time, in order to ensure that the thread recovers to the correct execution location, each thread has a separate counter

2. Virtual machine stack (VM stack)

The virtual machine stack describes the memory model that the Java method executes: Each execution creates a stack frame that stores information such as the local variable table, the operand stack, the dynamic link , the method exit, and so on.

The local variable table holds the basic data type, reference type, and ReturnAddress type, which are known at compile time.

where long and double occupy two slots, others occupy one, the local variable table is required to have the compilation fully determined, and the method runs without the table local variable table size.

Two anomalies are regulated in this area:

The number of virtual machine stacks exceeds the allowed number of virtual machines, Stackoverflowerror

Virtual machine Memory extension cannot request enough memory space, throw OutOfMemoryError

3. Local methods Stack (Native method Stacks)

The local method stack acts like a virtual machine stack, where the virtual machine executes the Java Program service for the virtual machine, and the local method stack executes the local method service for the virtual machine.

The local method stack also throws a Stackoverflowerror,outofmemoryerror exception.

Thread-Shared

4. Heaps (heap)

The Java heap is a piece of memory that is shared by all threads and is described in the Java Virtual Machine specification as: The heap is the runtime data region from which memory for all class instances and array S is allocated.

But with the development of the JIT compiler and the maturity of the escape analysis technique, the stack is allocated, and the scalar substitution makes the object not necessarily stored in the heap.

The Java heap is the main administrative area of garbage collection, so it becomes a GC heap, which divides the memory into several areas:

Cenozoic

Eden, from Survivor, to Survivor

Old age

If there is no memory complete allocation in the heap and the heap cannot be expanded, the OutOfMemoryError is thrown

5. Method area

Method area is a zone shared by all threads, storage class information, constant information, static variable information, compiled code, and so on.

Spothot virtual machines Extend GC generational collection to the method area, which facilitates managing the method area like a heap, so that some people prefer to be a "permanent generation" (Permanent Generation)

Java automatic memory management mechanism

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.