Java Virtual Runtime data area
The runtime data area mainly includes: Method area, heap, virtual machine stack, local method stack, program counter.
Where the method area and the stack are thread-shared areas, the other three blocks are private areas of each thread. The functions of each data area are briefly described below:
Program counter: The line number indicator of the byte code executed by the current thread.
Virtual Machine Stacks: Describes the memory models that Java methods perform-each method creates a stack frame to store information such as local variable tables, operand stacks, dynamic links, method exits, and so on. Each method from the call until the completion of the process, corresponding to a stack frame in the virtual machine stack into the stack of the process. if the depth of the stack is greater than the depth allowed by the virtual machine, the Stackoverflowerror exception is thrown. If sufficient memory cannot be requested when the stack is extended, the OutOfMemoryError exception is thrown (the local method stack, as described below).
Local method Stack: similar to the virtual machine stack. The virtual machine stack is a Java method service, and the local method stack serves the native method.
Java heap: Created when a virtual machine is launched to hold an object instance. Control the size of the heap through-xmx and-XMS. A OutOfMemoryError exception is thrown if there is no memory in the heap to complete the instance assignment, and the heap can no longer be expanded.
Method Area: The function is to store class information, constants, static variables, etc. that have been loaded by the virtual machine (this area is called permanent Generation in the hotspot).
Creation of objects
Object creation consists of four steps: class load checking, allocating memory for new objects, initializing to 0 values, setting object headers.
First, class load check
Virtual opportunity to a new directive, the recipient checks whether the parameters of the directive can be Chang (the run-time-constant pool is part of the method area.) class files In addition to the class version, fields, methods, interfaces, and other descriptive information, there is a constant pool, for the compilation period generated by a variety of literal and symbolic references, which will be loaded in the class load into the method area of the run constant pool. ), and check to see if the class represented by the symbol reference has been loaded, parsed, and initialized. If not, you must first perform the appropriate class loading process.
Ii. allocating memory for new objects
The memory size required for an object is fully determined after the class is loaded, and allocating memory is equivalent to dividing a certain size of memory from the Java heap. There are two situations in which the memory in the Java heap is absolutely regular (called A pointer collision, Bump the pointer), and the memory is not structured (called the Free list). In a regular Java heap, used memory is put aside, free memory is put on one side, and a pointer in the middle is used as the dividing point. In an irregular Java heap, a virtual machine maintains a list of records which blocks are available. The choice of that allocation method is determined by whether the Java heap is structured or not, and whether the Java heap is structured by the garbage collector with the compression finishing function. For example, when using a collector with compact processes such as serial and parnew, the allocation algorithm used is a pointer collision, whereas a free list is used when using a CMS based on the mark-sweep algorithm.
Iii. allocating memory procedures to ensure thread safety
Synchronous processing of the action of allocating memory space, or the action of allocating memory to the thread in different spaces (i.e. local thread allocation buffer thread local Allocation buffer).
Four, the object header settings
Describes which class is an instance, how to find the metadata information for the class, the hash code for the object, and the GC age of the object.
Access positioning of objects
The Java program is reference on the stack to get the concrete object on the data manipulation heap. There are two ways to access it: using a handle and a direct pointer.
First, use the handle
Reference stores a stable handle address that changes the instance data pointer in the handle only when the object instance data is moved (garbage collection), and the reference itself does not need to be changed.
Ii. use of direct pointers
Fast because it saves time overhead for pointer positioning.
Description: The content of this article refers to the book "in- depth understanding of Java Virtual Machine (2nd edition)"
Java Memory Area--JVM series < one >