Chapter 1. runtime data area

[TOC

Outline

For application developers in C, C ++ is, in the areas of memory management, responsible for every life objects start to the end of responsibility.

For Java programmers, with the help of automatic memory management of virtual machines, do not need to for each new write operation to delete / free pairing codes, memory leaks and memory overflow problem does not occur easily, the virtual machine is managed by the memory it all looks very good. However, it is precisely because Java programmers the right to control the memory to the Java virtual machine, once the memory leaks and overflow problem occurs if the virtual machine is not know how to manage memory, then the error will become an anomaly investigation hard work.

Runtime data area profile

Java virtual machine during the execution of the Java program in which it will manage the memory area is divided into a number of different data regions, which have their own uses and the creation and destruction of time.

Runtime data area includes the following regions:

  • Program Counter
  • Java Virtual Machine stack
  • Native method stacks
  • stack
  • Methods district
  • Runtime constant region

Program Counter

Program Counter (Program Counter Register) is a small memory space, its role can be seen as an indicator byte-code line number of the current thread executed in the conceptual model of a virtual machine's (just a conceptual model, each species may be a virtual machine to achieve a more efficient manner through a number), it is to choose the next bytecode instruction to be executed by changing the value of the counter when the bytecode interpreter work, branched, cyclic, jump, exception handling thread resume and other basic functions need to rely on the counter to complete.

Since the multi-threaded Java Virtual Machine is in turn switched mode execution time and processor allocation by threads to achieve, at any one time is determined, a processor (for multi-core processor is a memory) will only execute one thread instructions. Therefore, in order to restore the thread switch to the correct execution position, each thread requires a separate program counter, between the threads independently of each other, independent store, we call this type of memory area for the private threads RAM.

If the program is a Java method is executed, the counter records the address of the virtual machine bytecode instruction being executed; if the method is being performed Native, this counter value is null (Undefined). This memory area is the only one not provide for any situation in the region OutOfMemoryError Java Virtual Machine Specification.

Java Virtual Machine stack

And program counter, like, Java Virtual Machine stack (Java Virtual Machine Stacks) is also a thread private , its life cycle and the same thread . Is a virtual machine stack Java memory model described method of execution: when each method will be executed at the same time create a stack frame (Stack Frame) for storing local variable table, operation information stack, dynamic linking, method exports. Each method is called until the completion of the execution process, it is a stack frame corresponds to the process in a virtual machine stack from the stack to the station.

Has often been divided into the Java memory heap (Heap) and stack memory (Stack) , the method is relatively rough, divide Java memory area is actually complicated than this. Popular this division can only explain these two most programmers are most concerned about, the relationship with the object closest memory allocation memory area. Which referred to "stack" will be devoted below, while referring to "stack" is now talking about Java virtual machine stack, or a local variable table portion of the virtual machine stack.

All the basic local variable table stored in the compiler known data type (boolean, byte, short, int , long, float, double), object reference (reference type, it is not equivalent to the object province, implemented according to different virtual machines, it may be a start address to an object refers to the address, it may point to a representative of the handle or other object associated with this position of the object) and returnAddress (address pointing to a byte code instruction).

Wherein the long and double-length 64-bit data occupies two local variable space (Slot), the remaining data occupies only one type. 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 .

In the Java Virtual Machine Specification, this area provides two anomalies: 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 can dynamically expand the stack (most of the current Java virtual machines can be dynamically extended, but the Java virtual machine specification also allows virtual machine stack fixed length), when the expansion can not apply enough memory will throw OutOfMemoryError exception.

Native method stacks

Native method stacks (Native Method Stacks) and the role played by the virtual machine stack is very similar, but the difference is the virtual machine execution stack for the Java Virtual Machine method (ie bytecode) service, and is for the native method stacks virtual machine to use a method of Native service. Virtual Machine Specification language native method stack method used, use a data structure is not mandatory, so the specific implementation of the virtual machine it may be free. Some set up a virtual machine (such as HotSpot VM) directly put into one native method stacks and stacks virtual machine. Like the virtual machine stack, the stack area native method can also throw StackOverflowError and an OutOfMemoryError.

stack

For most applications, Java heap (Java Heap) is the largest piece of memory in the Java virtual machine management. Java heap is shared by all threads in a memory area is created when the virtual machine starts . The sole purpose of sub-memory region is stored object instance , almost all object instances are here to allocate memory. This is described in the Java Virtual Machine Specification is: all object instances and arrays to be allocated on the heap, but with the development of JIT compiler and escape analysis technology matures, distribution on the stack, scalar replacement optimization techniques cause some subtle changes, all objects are allocated on the heap gradually becomes not so "absolutely" the.

Java heap is the main area of ​​memory managed by the garbage collector, and therefore often also referred to as "GC heap," If you look from the perspective of memory recovery, due to the generational collection algorithm is now basically using a collector, so Java heap It can also be broken down into: the old and the new generation's; and then there is little detailed Eden space, From Survivor space, to Survivor space. If you look from the perspective of memory allocation, threads share the Java heap can be divided into multiple threads private allocate a buffer (Thread Local Allocation Buffer, TLAB). Anyway division, has nothing to do with the storage of content, regardless of which area, are still stored object instances, further divided the purpose is to better recovery of memory, or faster memory allocation.

Under the Java Virtual Machine specification, Java heap may be in discontinuous physical memory space, as long as logically contiguous to, just like our disk space. When implemented, may be implemented as a fixed size, it can be expanded, but the current mainstream is an extensible virtual machine implemented (by -Xms -Xmx and control). If there is no complete examples in the heap memory allocation, and the stack can no longer expand, it will throw an OutOfMemoryError.

Methods district

District method (Method Area) and the Java heap, as each thread is a shared memory area, which is used to store class information has been loaded in the virtual machine, constants, static variables, the time compiler to compile the code and other data. Although Java Virtual Machine Specification as described in the method area is a logical part of the heap, but it has an alias called Non-Heap (non-stack), the Java heap object should be distinguished.

For habit development and deployment for developers to program in the HotSpot virtual machine, a lot of people willing to method area called "permanent generation", in essence, the two are not equivalent, simply because HotSpot virtual machine's design team chose to GC generational collection area extends to methods, or permanent generations to implement the method area only. For other virtual machines (e.g., BEA Jrockit, IBM J9, etc.) is that there is not a permanent generation. Even HotSpot itself after JDK8, has been used element space (Metaspace) substituted permanent generations. About dimensional space can own Baidu understand.

Java Virtual Machine Specification limit of this region is very loose, in addition to and do not require continuous as the Java heap memory can be selected and fixed size or may be expanded, but also can choose not to implement garbage collection. In contrast, garbage collection behavior in this area is relatively small appearance, but not the data into the area just as a permanent method of generation of the same name "permanent" exists. Garbage collection target this area is mainly for recycling constant pool and unloading of the type of general recovery in this region, "achievement" relatively unsatisfactory, especially the type of unloading, conditions can be quite harsh, but this part of the region recycling is really necessary.

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

Runtime constant pool

Runtime constant pool (Runtime Constant Pool) is part of the zone method. Class file versions in addition to the classes, fields, methods, and interface description information, the information is also a constant pool (Constant Pool Table), and for storing various literal reference symbol generated by the compiler, this part stored in the method area after loading class runtime constant pool.

Java virtual machine formats for each part of the Class file (naturally including the constant pool) has strict rules, each byte is used to store data which must meet the requirements of the specification, this will be recognized by the virtual machine, loading and execution. But for the runtime constant pool, Java Virtual Machine Specification do not require any details of the different providers realize the virtual machine can be implemented in accordance with their needs in this area. However, in general, in addition to saving Class symbol described in the document cited, it will also translate out of the direct reference is also stored in the runtime constant pool.

Runtime constant pool of cash to another important feature of the Class file with the constant pool is dynamic, Java language does not require constant must only be produced in the compiler, which is not preset the contents of Class file constant pool to enter the method run-time constant pool area during the operation may be the new constants into the pool, the more is the method String # intern who take advantage of this feature is developed.

Since the runtime constant pool area is part of the method, the natural method is limited by the memory area, when the constant pool can no longer apply to the memory will throw an OutOfMemoryError.

Direct Memory

Direct memory (Direct Memory) is not part of the data area of ​​the virtual machine is running, nor is it Java virtual machine memory area defined in the specification, but this memory is also frequent use, but may also lead to an OutOfMemoryError occurs, so put say it together here.

In JDK1.4 newly added NIO (New Input / Output) type, based on the introduction of a channel (Channel) and a buffer (Buffer) in the I / O mode, he can use Native libraries directly outside the heap memory allocated, then the stack inside the object as a reference DirectByteBuffer this memory is operated by a stored Java. This can significantly improve performance in some scenarios as to avoid the replicated data back and forth in the Java heap heap and Native.

to sum up

Recognize that Java memory zoning for storage objects each region to understand, and each area may be what kind of exception is thrown.

Different for each region of the virtual machine providers have different implementations, we mainly refer to the mainstream of Sun's HotSpot virtual machine, and the HotSpot virtual machine memory zoning of Java, there will be some changes in different versions. The most obvious is JDK6 when employing a permanent generations, JDK7 when excessive, JDK8 when the permanent generation of canceled and replaced by a dollar space.

Quote: Zhou Zhiming of "in-depth understanding of the Java Virtual Machine: JVM advanced features and best practices", Second Edition

Guess you like

Origin www.cnblogs.com/wuqinglong/p/11105223.html
Recommended