The JVM JAVA virtual machine runtime data area

Runtime data area
Here Insert Picture DescriptionHere Insert Picture Description

The program counter
1, a small memory space, thread private. Bytecode interpreter of this work is that by changing the value of the counter to select one of the instructions to be executed bytecode instructions, branching, looping, branching, exception handling, and other basic functions thread recovery relies on complete counter
2, if thread is executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed; Native method is being performed if the value of this counter is compared (Undefined). This memory area is the only one not provide for any situation in the region OutOfMemoryError Java Virtual Machine Specification.
Zhan JAVA virtual machine
1, a memory area to act on java execution
stack calls follow the principles advanced out
2, each method will create a stack frame (Stack Frame) while performing local variable table for storing operand stack, dynamically linked, and other information for export, each method call from a process until the completion of a stack frame will correspond to the virtual machine from the stack to the stack
3, the type of data stored in the local variable table may include:
Boolean, byte, char, short, int, float, long, double , and the type of object reference (reference type) and returnAddress type (address pointing to a byte code instruction) and the like.
4. If the stack is greater than the depth of the thread requested virtual machine allowable depth, will throw an exception StackOverflowError
recursive call, will produce abnormal heap memory, (recursive cause StackOverflowError because each allows a method to create a stack frame, stack frames created too and more can not continue to apply for memory expansion)
Exception in the Thread "main" java.lang.StackOverflowError
Here Insert Picture Description
StackOverflowError: thread request stack depth greater than the depth allowed by the virtual machine.
OutOfMemoryError: If the virtual machine can dynamically expand the stack, and can not apply to enough memory when expanding.

Native method stacks
1, and the same stack java virtual machine, each method will create a stack frame (Stack Frame) to store information local variable table, the operand stack, dynamic link, and the like while performing the method of the outlet, each method from the calling process until the stack frame will correspond to a virtual machine from the stack to the stack
2, when the JAVA virtual machine stack performs Java method will be compiled into bytecode, native method stacks native method is executed (call when other languages)
3, distinct from the Java virtual machine stack is, the Java virtual machine stacks for Java virtual machine to perform method (ie bytecode) services, native method stacks, the virtual machine to use a method of native service. StackOverflowError and there will be an OutOfMemoryError.
JAVA stack
1, which is a JAVA memory area used to store object instance area, almost all of the object instances are partitioned here.
2, JAVA heap is the largest piece of memory JAVA virtual machine management, and JAVA stack is shared by all threads in an area of memory
3, JAVA stack can be divided into the old and the new generation's new generation can be divided into Eden, From Space , the to Space
. 4, the JAVA heap is the main garbage collector region management therefore often also called "GC heap"
5, the heap memory overflow
Here Insert Picture Description
method area
1 belongs to the shared memory area for storing the virtual machine has been loaded class information, constants, static variables, the time compiler to compile the code and other data.
Runtime data stored content zone JAVA virtual machine
Here Insert Picture Description
runtime constant pool
1, part of the zone method, and for storing various literal symbols compile generated reference. The compiler and runtime (String's intern ()) can be constants into the pool. Limited memory, OutOfMemoryError is thrown when unable to apply for
access to the object
1, handle to access the
Java heap set aside a block of memory pool as a handle, the handle reference address stored in the object, the handle contains the object instance data, the address information of the type of data
references storage handler address is stable, the data only need to change the pointer instance handle pool handle when the object is moved, without the need to change the address reference stack itself (garbage collection object needs to be moved to a different area)
Here Insert Picture Description

2, direct pointer
different from the direct pointer that references stored in the instance data objects, Example address data includes type data
advantage is speed, as compared to the handle to reduce the time cost of a pointer positioned, JVM HotSpot on the use of this way
Here Insert Picture Description

GC的概念
GC:Garbage Collection 垃圾收集。这里所谓的垃圾指的是在系统运行过程当中所产生的一些无用的对象,这些对象占据着一定的内存空间,如果长期不被释放,可能导致OOM(堆溢出)。内存区域中的程序计数器、虚拟机栈、本地方法栈这3个区域随着线程而生,线程而灭;栈中的栈帧随着方法的进入和退出而有条不紊地执行着出栈和入栈的操作,每个栈帧中分配多少内存基本是在类结构确定下来时就已知的。在这几个区域不需要过多考虑回收的问题,因为方法结束或者线程结束时,内存自然就跟着回收了。而Java堆和方法区则不同,一个接口中的多个实现类需要的内存可能不同,一个方法中的多个分支需要的内存也可能不一样,只有在程序处于运行期间时才能知道会创建哪些对象,这部分内存的分配和回收都是动态的,GC关注的也是这部分内存,如果涉及到“内存”分配与回收也仅指着一部分内存
回收算法
引用计数算法:(老牌垃圾回收算法。无法处理循环引用,没有被Java采纳)
1、引用计数算法的概念:
给对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加1;当引用失效时,计数器值就减1;任何时刻计数器为0的对象就是不可能再被使用的。
Here Insert Picture Description

上面的3个图中,对于最右边的那张图而言:循环引用的计数器都不为0,但是他们对于根对象都已经不可达了,但是无法释放。

public class Object {
 
    Object field = null;
    
    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            public void run() {
                Object objectA = new Object();
                Object objectB = new Object();//位置1
                objectA.field = objectB;
                objectB.field = objectA;//位置2
                //to do something
                objectA = null;
                objectB = null;//位置3
            }
        });
        thread.start();
        while (true);
    }

上方代码看起来有点刻意为之,但其实在实际编程过程当中,是经常出现的,比如两个一对一关系的数据库对象,各自保持着对方的引用。最后一个无限循环只是为了保持JVM不退出,没什么实际意义。
代码解释:代码中标注了1、2、3三个数字,当位置1的语句执行完以后,两个对象的引用计数全部为1。当位置2的语句执行完以后,两个对象的引用计数就全部变成了2。当位置3的语句执行完以后,也就是将二者全部归为空值以后,二者的引用计数仍然为1。根据引用计数算法的回收规则,引用计数没有归0的时候是不会被回收的。
对于现在使用的GC来说,当thread线程运行结束后,会将objectA和objectB全部作为待回收的对象。而如果我们的GC采用上面所说的引用计数算法,则这两个对象永远不会被回收,即便我们在使用后显示的将对象归为空值也毫无作用。
根搜索算法
1、根搜索算法的概念:
  由于引用计数算法的缺陷,所以JVM一般会采用一种新的算法,叫做根搜索算法。它的处理方式就是,设立若干种根对象,当任何一个根对象到某一个对象均不可达时,则认为这个对象是可以被回收的。
Here Insert Picture Description
如上图所示,ObjectD和ObjectE是互相关联的,但是由于GC roots到这两个对象不可达,所以最终D和E还是会被当做GC的对象,上图若是采用引用计数法,则A-E五个对象都不会被回收。

2, reachability analysis:
the establishment of several root object, when a root object to any one subject not up, then that object can be recycled. We introduce mark in the back - when cleaning algorithm / tags to organize algorithm will always emphasize starting from the root, to make up for all objects first mark, then what is it up to? Here is explained as follows:
reachability analysis:
  from the object root (GC Roots) as a starting point, start searching down, the search path traversed referred to as "reference chain", when an object is not connected to any reference GC Roots chain (use the concept of graph theory terms, is from the GC Roots to this object is unreachable), then it proves that this object is not available.
3, the root (GC Roots):
Speaking of GC roots (GC roots), in the JAVA language, can be used as the object GC roots are the following:
1, the stack (Local Variable Table stack frame) referenced object.
2, the method area in a static member.
3, an object method reference constant region (global variable)
4, in the JNI native method stacks (typically say Native method) referenced object.
NOTE: The local variable table refer to the first and fourth methods, the meaning of the expression of a second relatively clear, mainly referring to the third constant value declared final. On the basis of the root search algorithm based on the realization among the modern virtual machine, garbage collection algorithms there are three, namely, mark - sweep algorithm, replication algorithm, mark - sorting algorithms. Three algorithms are expanded root search algorithm.

Published 10 original articles · won praise 0 · Views 101

Guess you like

Origin blog.csdn.net/xiaobao1352/article/details/104205818