"Java" Memory Area-"heap and Stack"

Source: Internet
Author: User

  This is my reading of the Zhou Zhiming Teacher's "deep understanding of Java Virtual Machine-JVM advanced features and Best practices" chapter 2.2 of the study notes. For more information, please refer to the original book.

A Java Virtual machine executes a Java program by dividing the memory it manages into several different data regions that have their own purpose and life cycle. Specifically, the following areas are included.

1. Program counter

A small amount of memory space that can be seen as the line number indicator of the byte code executed by the current thread. The main purpose is to pick the next byte-code directive that the thread needs to execute.

Each thread has a separate program counter, and each thread's counters are independent of each other and stored independently. Such a memory area is also known as "thread-private" memory.

If the thread is executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed, and if the native method is being executed, the counter is "Undefined".

2. Virtual Machine stack

The thread is also private and its lifecycle is synchronized with the thread. The virtual machine stack describes the memory models that are executed by the Java method, each of which creates a stack Frame that stores information such as local variable tables, operand stacks, dynamic links, method exits, and so on. Each method, from invocation to completion, corresponds to the process of a stack frame from the stack to the stack in the virtual machine stack.

The local variable table holds the various basic data types known to the compiler, object references, and return adress types. The memory space required for a local variable table is allocated during compilation, that is, when a method is entered, the local variable space in its stack frame is deterministic and will not change during runtime.

"Stackoverflowerror" is thrown when the thread requests a stack depth greater than the virtual machine allows, or "OutOfMemoryError" if the virtual machine can be dynamically extended (most of the virtual machines are currently extensible), and if sufficient memory cannot be requested when the extension is extended.

3. Local Method Stack

The native method service, which is primarily used by virtual machines, acts like a virtual machine stack.

4. Java Heap

The Java heap is a piece of memory that is shared by all threads, created when the virtual machine is started, and is the largest piece of memory managed by the virtual machine. Basically, all object instances and arrays are allocated memory here. The amount of space allocated by an object is another problem.

The Java heap is the main area of garbage collector management, so it is often referred to as the "GC Heap" (Garbage Collection heap). The allocation and recycling of various areas on the heap is a GC problem.

The Java heap needs to be logically contiguous, without having to be physically contiguous. The current mainstream virtual machines have implemented the heap as extensible (-XMX,-XMS, which is the maximum heap and initial heap size). "Outofmemory-error" is also thrown when the heap needs to be extended and cannot be expanded.

5. Method area

The Method area is also a region of memory shared by various threads, used to store data such as class information, constants, static variables, and the compiled code of the instant compiler that have been loaded by the virtual machine. Logically part of the heap.

Please refer to the original book for the relevant contents of the method area, running constant pool and direct memory, etc.

"Java" Memory Area-"heap and Stack"

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.