One, virtual machine memory partition
A Java Virtual machine runs on a physical machine that is manipulated by a different operating system, and different operating systems use different underlying methods to perform different operations, which are called local methods: Native method, the local methods are generally performed in the lower level of operations, such as IO, thread management, etc. Java methods perform generally relatively advanced operations, such as a number of logical operations, or call the underlying local method to accomplish the underlying task. The runtime data region of a Java virtual machine divides the memory into different parts to coordinate the memory data interaction of the Java Virtual machine.
Data structures can be broadly categorized as follows:
Stack area:
-
Virtual machine stack: Java Virtual machine run Java Method (Java bytecode method) composed of the stack space, this space at run time to store these methods of local variable table, Operation Stack, dynamic link and method export;
-
Local method Stack: The local method stores the stack area produced by the data at run time.
Heap Area:
-
Java heap: An instance of an object is stored in this shared heap space, due to the possession of the largest and most meaningful space, the GC process of this space is the focus of the virtual machine running.
-
Method Area: Store Virtual run-time load of class information, constants, static variables and immediately compiled code, so you can consider this part as a relatively fixed data to save the part, constants and static variables at compile time to determine down into this part of memory, runtime class information will be directly loaded into this part of memory, So it's all relatively early into memory.
-
Run a constant pool: Not all constants are determined at compile time to enter the memory, there will still be runtime to enter the memory of the constant, which is generally generated at compile time some fixed information, such as translation of the reference, and so on, directly in the class loading in the run-time pool can help improve performance. The data interaction of all memory areas is guided by program counters to complete the complex logical steps of the virtual machine.
How to find an instance of an object:
Select AllCopyput it in your notes .
Object obj = new Object();
In this process, creating an obj reference in the local variable table of the virtual machine stack, creating an instance of the object class in the heap memory, and finally associating the obj reference with this object instance, and what we need to know is that not all instances have completely saved all of the class information. The data for common or static classes is stored in the method area, and the unique instance data is actually saved in the Java heap, so each reference must find both the instance data and the class data associated with it. There are two ways to do this:
I. The reference store is only the handle of the instance, and the handle is stored in the handle pool of the heap, the address of the real instance in the heap and the address of the class data in the method area, so that these addresses can be found through this handle.
II. The reference store is the address of the instance in the heap, and the instance contains the address where the class data can be located, that is, by finding the instance address to find its corresponding class data.
Two and memory overflow-related exceptions:
-
Stackoverflowerror: The stack depth of the thread request is greater than the specified value of the virtual machine;
-
OutOfMemoryError: Increased memory for thread expansion is greater than the requirements of virtual machines;
Second, memory recovery mechanism
Virtual machine stacks, local method stacks, and counters are mostly memory allocations determined at compile time and are cleaned up after the thread executes, and memory recovery is relatively easy. So, most of the memory recycling we mention refers to the recycling of heap memory. We illustrate the memory recovery mechanism by following several questions:
1. What kind of heap memory can be recycled?
What kind of heap memory can be recycled? In short, those "useless" memory, then what kind of memory is "useless"? That is, those memory objects that are no longer accessible through existing pointers (or "references"). So there is an algorithm to describe the invalid reference: (reference counting algorithm) each object has a reference counter, is referenced once the counter plus 1, the reference is set empty minus 1, the value of the reference counter is 0 is "useless" memory object, it occupies memory can be recycled. (This algorithm does not seem to be a problem, but there is a problem when it encounters a circular reference: if both sides of the circular reference are empty, the memory is not accessed even if the referenced counter is not 0).
This process is a flaw in this algorithm, which reflects the potential problems of circular referencing, which are still referenced but cannot be accessed.
(Root search algorithm) will be used by the stack memory or method area referenced by the object as gcroots to build the reference chain, if the object can be found that the object can access its memory can not be recycled, but the reference chain cannot find this object is already deprecated objects, Its memory is supposed to be recycled. (The above example of circular reference can be solved, because this problem, although the value of the reference counter is not 0, but there is no gcroots can find these memory, the problem of GC roots is the stack memory of Obja and OBJB, the two stacks in memory of the reference is empty, So there is no way to find the object in the reference chain. )
2. Determine what memory is being recycled after the GC mechanism is directly reclaiming memory?
Determines what memory should be recycled after the GC mechanism is directly reclaiming memory? The GC will give some of these in-memory objects One chance, which is the object of the class that rewrote the Finalize method, and the GC executes the Finalize method that the object has rewritten, and if in this method the object re-links itself to a reference so that the area of memory can be accessed again, Then the GC will not recycle it this time, however, this process can only be performed once, the next time the GC is encountered will not take into account the Finalize method is directly recycled, so be aware that the overridden Finalize method can only be executed once. This is the collection of objects in the heap memory, in the method area to save the class information and the memory of the constant pool also need to be recycled, the process is relatively slow and not so efficient, because a period of time the use of the class and constant pool is more stable, only to seriously confirm that there are classes no longer used and not reflected when the class is unloaded It is true that constants that are not used in a constant pool are released when no constants are in use.
See also: http://segmentfault.com/a/1190000003834613
Java Virtual machine