Parsing a text summary of the Java virtual machine memory area model

Recent taking the time to read the little "in-depth understanding of the Java Virtual Machine," this article to summarize the role of each major area of ​​the Java virtual machine memory, and these regions, clients and the problems which may arise, as we interview Collection.

First we look at the data area of ​​the Java runtime, Java virtual machine during the execution of the Java program in which it will manage the memory is divided into several different data regions, which have their uses, and their creation time destroyed. Some regions with the launch of the virtual machine process exists, and some regions are dependent on start and end users to create and destroy threads while.

We look at the data area of ​​the Java virtual machine runtime

FIG conjunction with this, the following one by one to analyze the characteristics of each data region.

1. Program Counter

The program counter is a smaller memory space, ** can be seen as an indicator byte-code line number of the current thread of execution. **What does that mean? We know that time is calculated by way of the CPU to the slice to each thread (in other words, in fact, the so-called parallel or serial in nature), such as thread A executes in one place, the CPU control to the thread B , then thread a to regain CPU resources, how to restore just the place to perform it? This is the program counter to be doing things! A thread can help find a place just executed, so just continue execution.

After switching to the thread of execution can be restored to the correct position, it requires each thread requires a separate program counter, between the threads independently of each other, independent storage. So the program counter is thread-private.

In addition, the program counter is the only one not provide for any situation in the region OutOfMemoryError Java Virtual Machine Specification.

2. Java Virtual Machine stack

Java virtual machine stacks occupy memory space is what we usually call "stack memory" and is also a thread private , the life cycle of the same thread. Virtual machine stack Java memory model described method performed: while performing each method, and creates a stack frame for storing local variable table (basic data types, object reference and returnAddress type), the operand stack, dynamic link, for export and other information.

Required for completion of the local variable table memory space assigned during compilation, when entering a method, this method requires much space for local variables allocated in the stack frame is fully determined during operation of the method does not change the size of the local variable table .

** Each method is invoked procedure until completion of the execution, it is a stack frame corresponds to the process from the virtual machine to push the stack from the stack. ** For the Java virtual machine stack, there are two cases to taste:

  • If the stack is greater than the depth of the thread requested virtual machine allowable depth, StackOverFlowError will throw an exception.

  • If the virtual machine stack at the time of dynamic expansion, unable to apply enough memory, it will throw an OutOfMemoryError.

3. The native method stacks

Native method stacks and stacks of virtual machine is very similar to the role, the main difference between them is: a virtual machine for Java stack method (ie bytecode) virtual machine execution services, and native method stacks, the virtual machine Native method to use the service.

Similarly with the virtual machine stack, native method stacks and an OutOfMemoryError will be thrown StackOverFlowError.

4. Java heap

Java memory heap is the biggest piece of the Java virtual machine management. Java heap in main memory is shared by all threads of a memory area, which is created with the creation of the JVM, the sole purpose of heap memory is stored object instances and arrays. At the same time Java heap GC is a major area of management.

Java heap need not be physically contiguous memory, as long as continuous logical. If there is no heap memory to complete the instance distribution, and can no longer expand, it will throw an OutOfMemoryError.

The method area

The method area is shared by all threads of a memory area. ** class information is used to store the virtual machine has been loaded, constants, static variables, the time compiler to compile the code and other data. ** The method also has a region not called Non-heap (non-heap), and to distinguish the Java heap. For the HotSpot virtual machine, the method area has become accustomed to "permanent-generation (Permancent Generation)", but this is only for the HotSpot virtual machine is and did not realize this concept on other virtual machines. In contrast, garbage collection behavior appear relatively small in this region, but it is not will not be collected, garbage collection target this area is mainly for the recovery of the constant pool and unloading of types.

Under the Java Virtual Machine specification, when the method of memory allocation area can not meet demand, it will throw an OutOfMemoryError.

6 Run-time constant pool

Runtime constant pool belonging to the method area. Class file versions in addition to the classes, fields, methods, and interface description information, the information is also a constant table for storing various constants and literal compile generated reference symbols, this part will be loaded after the class the method of entering the zone run-time constant pool of storage ** (JDK1.7 start, constant pool has been moved to the heap memory). **

In other words, this part, at compile time just to put a constant pool information, to the load time, will go into the runtime constant pool. Runtime constant pool County attributable to Class file constant pool Another important feature with dynamic, Java language does not require constant necessarily only compile to produce, that is not preset the contents of Class file constant pool to enter the zone method the runtime constant pool, during operation may be new constant into the pool, more is intern String class () method is the use of this property developers.

When the method of memory allocation area can not meet demand, will throw an OutOfMemoryError constant pool area belongs to the method, the same may throw an OutOfMemoryError.

The following model for Java memory area make a brief summary.

Guess you like