This article refers to the book "in-depth understanding of Java virtual Machines". Summarize the various areas of Java Virtual machine memory, as well as the role of these areas, the service objects, and the issues that may arise.
1. Run-time data region
Java Virtual machine in the process of executing a Java program, it says that the managed memory is divided into a number of different data regions, which have their own purpose, as well as the creation and destruction of the time, some regions as the virtual machine process started, some regions rely on the user thread start and end to build and destroy. As shown in the following examples:
In conjunction with this diagram, each of these data regions is analyzed individually.
1.1 Procedure Counter (program Counter Register)
A program counter is a small amount of memory space that can be thought of as the line number indicator of the byte code executed by the current thread. What do you mean? We know that CPU computing time is given to each thread in the form of shards (in other words, so-called parallelism is essentially serial), such as when thread a executes to a place where the CPU gives control to thread B, and when thread a recovers the CPU's resources, how can it revert to where it was just executed? This is what the program counter is going to do! It can help thread A to find the place that it just executed, thus continuing just execution.
In order for the thread to switch back to the correct execution location, each thread needs to have a separate program counter, and the counters between the threads do not affect each other and are stored independently. So the program counter is thread-private .
In addition, the program counter is the only area in the Java Virtual Machine specification that does not specify any outofmemoryerror conditions.
1.2 Java Virtual machine stack
The memory space occupied by the Java Virtual Machine stack is what we normally call "stack memory" and is thread-private with the same life cycle as the thread. The virtual machine stack describes the memory model that is executed by the Java method: Each method executes at the same time, creating a stack frame for storing local variable tables (basic data types, object references and returnaddress types), operand stacks, dynamic links, method exits, and other information.
The memory space required for a local variable table is allocated during compilation, and when entering a method, this method needs to allocate much of the local variable space in the stack frame, which is fully deterministic and does not change the size of the local variable table while the method is running.
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. For the Java Virtual machine stack, there are two exceptions:
1) If the thread requests a stack depth greater than the virtual machine allows, the STACKOVERFLOWERROR exception will be thrown;
2) If the virtual machine stack dynamically expands, unable to request enough memory, it will throw outofmemoryerror;
1.3 Local Method Stack
The role of the local method stack and the virtual machine stack is very similar, the difference is that the virtual machine stack is the Java method (that is, bytecode) service for the virtual machine, and the local method stack is the native method service used by the virtual machine.
Similar to the virtual machine stack, the local method stack throws Stackoverflowerror and OutOfMemoryError exceptions.
1.4 Java Heap
The Java heap is the largest piece of memory managed by a Java virtual machine. The Java heap is in main memory, a chunk of memory shared by all threads , created with the creation of the JVM, and the only purpose of heap memory is to hold object instances and Arrays . The Java heap is also the main area of GC management.
The Java heap does not physically require contiguous memory, as long as it is logically contiguous. A OutOfMemoryError exception is thrown if there is no memory in the heap to complete the instance assignment and can no longer be extended.
1.5 Method Area
method to be a memory area shared by all threads . Used to store data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine. The method area also has an alias called Non-heap (Not a heap), which is used to differentiate it from the Java heap. For a hotspot virtual machine, the method area is also used to be called the "permanent Generation" (permancent Generation), but this is only for the hotspot virtual machine, the other virtual machine implementation does not have this concept. The garbage collection behavior is relatively rare in this area, but it is not collected, and the memory recovery target of this area is mainly for the recovery of the constant pool and the unloading of the type.
According to the Java Virtual Machine specification, a OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements.
1.6 Running a constant-rate pool
The run-time-constant pool belongs to the method area. In addition to the class file with the description of the version, field, method, interface, and other descriptive information, there is also a constant table for the compilation period generated by a variety of literal constants and symbol references, which will be loaded in the class into the method area In the run-time pool (JDK1.7 start, the constant pool has been moved to heap memory). In other words, this part of the content, at compile time just put into the constant pool information, to the load, will be placed in the run constant pool. Running a constant pool county attributed to the class file is also an important feature of the regular pool of dynamic, the Java language does not require constants must only compile time to produce, that is, not pre-placed in the class file of the constant pool of content to enter the method area of the running of the frequent pool, It is also possible to put new constants into the pool during run time, which is more than the Intern () method of the string class used by developers.
When the method area fails to meet the memory allocation requirements, the OutOfMemoryError exception is thrown, and the constant pool belongs to the method area, which can also throw a OutOfMemoryError exception.
Here is a summary of the memory area model:
2. Summary of the Java memory Area model
| memory area |
thread private |
main function |
overflow exception |
| program counter |
Yes |
record where the current thread is executing |
no exception |
| virtual machine stack |
Yes |
object references and returnaddress types), operand stacks, dynamic links, method exits, and other information (Java methods) |
Stackoverflowerror and OutOfMemoryError |
| local method stack |
Yes |
|
stackoverflowerror and OutOfMemoryError |
|
no |
storing object instances and arrays |
outofmemoryerror |
| method zone |
no |
|
outofmemoryerror |
-Willing to share and progress together!
--My Blog home: http://blog.csdn.net/eson_15
Java Virtual machine Java memory area and memory overflow exception