Java Memory Management

Source: Internet
Author: User

Why do you want to know about Java memory management?

Although Java is an automatic memory allocation, once there is a memory leak and overflow problem, it is also necessary to understand how the virtual machine uses memory when troubleshooting errors.

1. Run-time data region

1.1 Program Counters

Each thread has a separate program counter (thread-private) that records the address of the virtual machine bytecode that the thread is executing.

The program counter is the only area in the Java virtual machine that does not specify any outofmemoryerror.

1.2 Java Virtual Machine stack

The Java Virtual machine stack is also thread-private, with the same life cycle as the thread.

When each method is executed, a stack frame is created at the same time to store information such as local variable tables, operation stacks, dynamic links, method exits, and so on.

Each method is called until the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the process of the stack.

The local variable table holds the various underlying data types (Boolean, Byte, char, short, int, float, long, double), object reference, and ReturnAddress type (which point to the address of a bytecode directive) that are known during the compilation period.

If the request depth of the thread is greater than the allowed depth of the virtual machine, the Stackoverflowerror exception will be thrown.

If the virtual machine stack can be dynamically extended, an OutOfMemoryError exception is thrown when the extension fails to request enough memory.

1.3 Local Method Stack

The local methods Stack (Native method Stacks) is very similar to the virtual machine stack, where the virtual machine stack serves the Java method and the local method stack serves the Native method.

The local method stack throws Stackoverflowerror and OutOfMemoryError exceptions.

1.4 Java Heap

The Java heap is the largest piece, and the Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started.

The only purpose of this memory area is to hold object instances where almost all of the object instances are allocated memory.

The Java heap is the primary area of garbage collector management and is therefore referred to as the "GC Heap" (garbage collected heap).

The Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous.

A OutOfMemoryError exception will be thrown if there is no memory in the heap to complete the instance assignment and the heap can no longer be expanded.

1.5 Method Area

The method area is also an area of memory shared by each thread that holds 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 GUI method describes the method area as a logical part of the heap, it has an alias called Non-heap.

The OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements.

1.6 Running a constant-rate pool

1.7 Direct Memory

Direct memory is not part of the data area when the virtual machine is running, nor is it an area of memory defined in the Java VM specification.

However, this part of memory is also used frequently and may cause outofmemoryerror anomalies to occur.

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.