High-intensity learning and training on the first day summary: Java memory area

--- --- restore content begins

Program Counter:

Program Counter (Program Counter Register) is a small space, he can be seen as an indicator byte-code line number of the current thread of execution. In the conceptual model of a virtual machine's (only a conceptual model, a variety of virtual machine might go through some achieve more efficient way), when the bytecode interpreter at work is to be selected by changing the value of a counter to be executed bytecode instructions, exception handling thread jump branch loop recovery functions need to rely on the basis of this counter to complete. Since the multi-threaded Java Virtual Machine is in turn switched and allocation of processor execution time by threads to achieve, at any time a determined, a processor (for multi-core processors is just a kernel), will only be executed a thread of instructions. Therefore, in order to restore the thread switch to the correct execution position, each thread requires a separate program counter, independently of each other between the threads, independent store, we call such regions as thread private memory.

If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instructions being executed: Native method is being executed if this counter value is null. This memory area is the only one not provide for any situation in the region OutOfMemoryError Java Virtual Machine Specification.

Java virtual machine stack:

Like the program counter, 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 performed: Each method creates at the same time a stack frame for the implementation of the local variable table information is stored, the operand stack, dynamic linking, method exports. Each method call from execution to completion, to a stack frame corresponds to the process in a virtual machine to push the stack from the stack.

Often it has been put into Java memory heap and stack memory, that the task was a bit rough, divide Java memory area is actually complicated than this. Popular this division can only suggest that most programmers are most concerned about, the relationship with the object memory allocation is the most intimate area of ​​memory and maybe block. The stack is now talking about the virtual machine stack, or a virtual machine stack local variable table section.

Local variable table to store various data types (boolean, byte, char, short, int, float, long, double) compiler known, object reference (reference type, it is not equivalent to the object itself, the object may be a starting point references the start address pointer, a point may be representative of a handle or other object associated with this position of the object) and returnAddress type (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. Desired local variable table in the memory space allocated during compilation is completed, when entering a method, this method requires much space for local variables allocated in the stack frame is fully defined, the method does not change during operation of the local variable table the size of.

In the Java Virtual Machine Specification, this area provides two kinds of 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, you can not apply if the extension enough memory, it will throw an OutOfMemoryError.

 

Native method stacks:

Native method stacks (Native Method Stack) and the role played by the virtual machine stack is very similar, but the difference between them is the implementation of a virtual machine stack is Java bytecode, native method stacks while using the method of Native service. In the virtual machine specification does not dictate language native method stack method used, so the specific virtual machine is free to implement it. Even some virtual machines (such as Sun HotSpot virtual machine), directly to the native method stacks and stack virtual machines combined. Like the virtual machine stack, the local station area method can also throw StackOverflowError and OutOfMeoryError

Java heap:

For most applications, Java heap (Java Heap) is the largest piece of memory in the Java virtual machine management. Java heap is an area shared by all threads, created when the virtual machine starts. The sole purpose of this memory area 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, the distribution station, scalar replacement technology will be optimization cause some subtle changes, all objects are allocated on the heap gradually becomes less categorical.

Methods areas:

The method area and the Java heap, as each thread is a shared memory area for storing virtual machine has been chanting class information loaded, constants, static variables, the time compiler to compile the code and other data. Although the method of the Java Virtual Machine Specification bit heap area described in a logic portion, but it has an alias called Non-Heap, Java object is separated from the heap.

For the habit in the HotSpot virtual machine developers, developers to deploy programs, many people are more willing to method area called permanent generations, essentially two were not equivalent, simply because HotSpot virtual machine design team chose to GC generational collection area extends to methods, or methods to achieve permanent generations area only, so HotSpot garbage collector can be as Java heap management as part of this memory can be omitted specifically written work memory management code for the method area. For other virtual machines (e.g., BEA JRockit, IBM J9, etc.) on behalf of the permanent concept is not present. In principle, the area belongs to the method how to implement the details of the virtual machine, the virtual machine specification without constraint, but generations to achieve a permanent method area now seems not a good idea, because it is more prone to memory overflow problem, but there are very the few ways (String.intern ()) due to this reason there are different manifestations in different virtual machines. Therefore, for the HotSpot virtual machine, according to official information released by the road map, but now they have to give up permanent generations and gradually change to implement the use of Native Memory of the district planning method, HotSpot has now released JDK1.7, he has been put on the original permanent generation string constant pool removed.

Java Virtual Machine Specification limit area very loose stack method, in addition to and do not require continuous as the Java heap memory can be selected and can be fixed size or group fights, can also choose not to implement garbage collection. In contrast, garbage collection behavior in this area is relatively small appearance, but not the method of data entered into the area just as the permanent generation of the same name, memory recovery of this area is the main objective for recycling and constant pool of the reactor type unloading, in general, the area recovered relatively unsatisfactory results, especially the type of unloading, conditions can be quite harsh, but the recovery of this part of the region is indeed necessary. In the Sun's list of BUG, ​​the BUG is a number of serious memory leaks due to the low version of the HotSpot virtual machine is not fully recovered to this area caused there had been.

According to the Java Virtual Machine Specification convention, when the method of memory allocation area can not meet demand, it will throw an OutOfMemoryError.

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, as well as a constant pool information, and for storing various literal compile generated reference symbols, this part will load the class run into the way to the time constant pool of storage.

Java Virtual Machine heap Class file format of each section, there are stringent regulations, each byte is used to store data which must meet the requirements of the virtual machine will be recognized, loaded and executed, but the runtime constant pool , Java virtual machine specification does not make any detailed requirements, different providers implement virtual machines according to their own needs to achieve this memory area. But 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.

Another important feature of the runtime constant pool file relative to Class constant pool is equipped 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 run constant pool to eat during operation can also be the new constants into the pool, which is characteristic of many developers is to intern with the method of the String class.

Since the runtime constant pool area is part of the method, naturally limited program memory, when the constant pool can no longer apply to OutOfMemmoryError memory will throw an exception.

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 OutOfMemoryError exception, so we put together to explain.

In JDK1.4 NIO newly added category, based on the introduction of a channel with buffer (Buffer) in the I / O mode, it can use Native libraries directly outside the heap memory allocated, and then through a stack stored in Java DirectByteBuffer object as a reference to this memory operation, this can significantly improve performance in some instances, it avoids the replicate data in the heap and the heap java Native.

Clearly, the direct memory allocation native Java heap size is not limited, however, since it is the memory definitely will still be the machine include the total memory (RAM and SWAP area or paging file) size and processor address space limitations . When the server administrator virtual machine configuration parameters, based on actual memory -Xmx setting parameter information such as, but often overlooked direct memory, such that each memory region is greater than the sum of the physical memory limitations. And throw OutOfMemoryError.

 

Guess you like

Origin www.cnblogs.com/godoforange/p/11521388.html