Introduction to Java Memory Architecture in the Java Virtual machine family

Source: Internet
Author: User

In this article we will explain the roles of each region and region in a Java virtual machine.

One. Program counter
What is a program counter and what is the function?
Program technology is a small area of memory, mainly as a thread in the execution of the byte code indicator, the bytecode interpreter works by changing the value of this counter to select the next execution of the bytecode command, branch, loop, jump and other basic functions are dependent on this program counter to complete.

What are the characteristics?
The multithreading in a Java Virtual machine is implemented by changing the execution time of the processor by thread rotation, so in order for the thread to switch back to the correct execution location, each thread needs a separate program counter, the counters between the threads do not affect each other, independent storage, so the thread is private.

Attention:
If the thread executes a Java method, this counter records the address of the virtual machine bytecode instruction being executed, and if the native method is being executed, the value of this counter is null. This memory area is the only area in the Java Virtual Machine specification where no outofmemory is specified

Two. Java Virtual machine stack
What does the Java virtual machine stack do?
The Java Virtual machine stack describes the Java memory model, in which each method creates a stack frame to store information such as the local variable table, the operand stack, the dynamic link, the method return address, and so on. Each method from the call to the end of execution corresponds to a stack frame in the virtual machine stack into the stack of the process.

What are the characteristics?
The Java Virtual machine stack is thread-private and has the same life cycle as the thread.

Attention:
If the thread requests a stack that is deeper than the virtual machine allows, the STACKOVERFLOWERROR exception will be thrown, and if the virtual machine stack can be dynamically extended, the OutOfMemoryError exception will be thrown if the extension cannot request enough memory.

Three. Local method stack
Similar to the functionality of the virtual machine stack, the difference is that the virtual machine stack executes Java methods for the virtual machine, and the local method stack serves the native method used by the virtual machine. The local method stack also throws Stackoverflowerror and OutOfMemoryError exceptions.

Four. Java heap
What is a Java heap and what does it do?
The Java heap is the largest managed memory in a Java virtual machine, a piece of memory that is shared by all threads, created when the virtual machine is started, to hold object instances.

What are the characteristics?
Almost all object instances allocate memory here, so this is also the main area of garbage collector management, and the Java heap can be in a physical discontinuous memory space, as long as the logical continuous

1, the Java heap also subdivides the Cenozoic and the old age, then divides into the new generation in the Eden region, from Survivor space, to survivor space and so on.

2, the Java heap garbage collection algorithm is used in the generational collection algorithm, that is, the new generation using the "copy" algorithm, and the old age using the "mark-clean" algorithm or "mark-collation" algorithm.

Attention:
Current mainstream virtual machines are extensible (controlled by-XMS and-xmx) and throw a OutOfMemoryError exception if there are no memory-complete instances in the heap and the heap can no longer be expanded.

Five. Method area
What is the function of the method area?
A method area is a region of memory shared by each thread that stores the class information, constants, static variables, and so on, even after the compiler compiles the data, that have been loaded by the virtual machine.

Why say the method area is not equivalent to perpetual area?
The method area is not equivalent to Yuyongjude, but the design team of the hotspot virtual machine chooses to extend the GC generational collection to the method area, so that the hotspot garbage collector can manage this part of the memory like the Java heap, eliminating the effort to write memory management code specifically for the method area. However, it is also more likely to cause memory overflow problems (the permanent generation can use-xx:maxpermsize to set the upper limit), so in JDK1.7 's hotspot has moved the permanent string constant pool out, moved to the Java heap, can be in the deep parsing string# Intern "Understand

What is the function of running a constant-volume pool?
The run-time-constant pool is also part of the method area, which holds the various literal and symbolic references generated during the compilation period, which are stored at runtime of the class load backward into the method area.

What are the characteristics of running a constant-rate pool?
The Java Virtual machine has a strict format for each part of the class file, and each byte is used to store which data must conform to the requirements of the specification to be recognized, loaded, and executed by the virtual machine, however, there is no requirement to run the constant pool.
The running constant is dynamic, and you can put new constants into the pool during run time, such as The Intern () method of String
Six. Direct Memory
is direct memory the memory of a Java virtual machine?
Direct memory is not part of the data area when the virtual machine is running, nor is it a memory area defined by the Java VM Specification, which uses native memory and is also known as out-of-heap memory.

How do I use out-of-heap memory in Java?
In JDK1.4, the NIO class introduces the I/O method based on channel and buffer, which can be used to directly allocate out-of-heap memory using the native function library. The Directbytebuffer object is then used in the Java heap to act as a reference to this block of memory.

Attention:
While out-of-heap memory is not managed by the Java heap size, it is limited by the native total memory and processor addressing space. When the server administrator configures the virtual machine parameters, the maximum memory of the Java Virtual machine can be configured through-XMX and so on, but the direct memory is often ignored, causing the sum of each memory region to be greater than the physical memory limit, causing the outofmemoryerror exception.

If there is any question or error in this article, please point out the message and learn from each other.

Introduction to Java Memory Architecture for the Java Virtual Machine family

Related Article

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.