Home > Java > javaTutorial > JVM garbage collection algorithm and three areas in the heap memory of jvm

JVM garbage collection algorithm and three areas in the heap memory of jvm

php是最好的语言
Release: 2018-07-30 11:24:37
Original
2027 people have browsed it

JVM Common Garbage Collection Algorithms

jdk1.7.0_79

As we all know, Java is a language that does not require programmers to manually manage memory. It relies entirely on the JVM to automatically manage memory. Since it is automatically managed, there must be a garbage memory recycling mechanism or algorithm. This article will introduce several common garbage collection (hereinafter referred to as GC) algorithms.

When a memory is allocated to an instance object on the Java heap, the reference variable on the virtual machine stack will store the starting address of the instance object.

Object obj = new Object();
Copy after login

Now if we assign the variable to null.

obj = null;
Copy after login

At this point you can see that the instance object on the Java heap cannot reference it again, then it is an object that has been GCed. We call it the object "has been die". What about the obj variable on the virtual machine stack? As mentioned above in "Introduction to JVM - Runtime Data Area", the virtual machine stack is exclusive to threads, which means that it is initialized and dies as the thread is initialized. When the thread is destroyed, the memory on the virtual machine stack It will naturally be recycled, which means that this memory space on the virtual machine stack is not within the scope of the virtual machine GC . The following figure shows the memory range of garbage collection:

1. Whether the object is "dead" algorithm - reference counter algorithm

Add a reference counter to the object. If the reference counter is 0, it means that no other place is referencing it. If there is a reference, it will be 1, and if the reference is invalid, it will be -1. It seems like a funny and simple algorithm, but in fact this algorithm is not used in most Java virtual machines because it will bring about a fatal problem - object circular reference. Object A points to B, and object B in turn points to A. At this time, their reference counters are not 0, but both of them are actually meaningless because nothing points to them. So the following algorithm is introduced.

  2. Whether the object is "dead" algorithm - Reachability analysis algorithm

This algorithm can Effectively avoid the situation of object circular reference. The entire object instance is presented as a tree. The root node is an object called "GC Roots". Starting from this object, search downward and mark it. After traversing the tree, it is not The marked object will be judged as "dead", that is, it can be recycled.

GC algorithm

1.Mark-clear algorithm

Waiting to be recycled The "marking" process of objects has been mentioned above. If the object is cleared directly after being marked, another new problem will arise - memory fragmentation. If a larger object instance needs to allocate a larger memory space on the heap next time, it may not be possible to find enough contiguous memory and the garbage collection will have to be triggered again.

 2.Copy algorithm (Garbage collection algorithm for the new generation in Java heap)

This GC algorithm actually solves the mark-sweep algorithm The "memory fragmentation" problem caused by. First, mark the memory to be recycled and the memory that does not need to be recycled. The next step is to copy the memory that does not need to be recycled to the new memory area, so that the old memory area can be fully recycled, and the new memory area is continuous. Its disadvantage is that it will lose some system memory, because you always have to free up some memory for copying.

As mentioned above in "Introduction to JVM - Runtime Data Area", the Java heap is divided into the new generation and the old generation. This division is to facilitate GC. The new generation in the Java heap uses the GC copy algorithm. The new generation is divided into three areas: Eden space, To Survivor space, and From Survivor space. From survivor and to survivor are the same size, and one is guaranteed to be empty. You might as well return your attention to the new generation part on the left side of this picture:

# When a new object instance is created, it is usually in the Eden space, and what happens in the Eden space GC is called Minor GC. When a GC occurs in the new generation, will copy the memory of Eden and one of the Survivor spaces to another one In Survivor, if an object survives several times, the memory object will be moved to the old generation. It can be seen that Eden accounts for the majority of the new generation, while the two Survivors actually account for a very small part. This is because most objects will be GCed soon after they are created (the 80/20 principle may be used here).

  3.标记-压缩算法(或称为标记-整理算法,Java堆中老年代的垃圾回收算法)

  对于新生代,大部分对象都不会存活,所以在新生代中使用复制算法较为高效,而对于老年代来讲,大部分对象可能会继续存活下去,如果此时还是利用复制算法,效率则会降低。标记-压缩算法首先还是“标记”,标记过后,将不用回收的内存对象压缩到内存一端,此时即可直接清除边界处的内存,这样就能避免复制算法带来的效率问题,同时也能避免内存碎片化的问题。老年代的垃圾回收称为“Major GC”。

不积跬步,无以至千里;不积小流,无以成江海。

  • 1.JVM的堆栈

栈:在jvm中栈用来存储一些对象的引用、局部变量以及计算过程的中间数据,在方法退出后那么这些变量也会被销毁。它的存储比堆快得多,只比CPU里的寄存器慢

堆:用来存储程序中的一些对象,比如你用new关键字创建的对象,它就会被存储在堆内存中,但是这个对象在堆内存中的首地址会存储在栈中。

栈内存在JVM中默认是1M,可以通过下面的参数进行设置

-Xss
Copy after login
  • 1

最小堆内存在JVM中默认物理内存的64分之1,最大堆内存在JVM中默认物理内存4分之一,且建议最大堆内存不大于4G,并且设置-Xms=-Xmx避免每次GC后,调整堆的大小,减少系统内存分配开销

-Xms

-Xmx
Copy after login
  • 1

  • 2

  • 3

在jvm的堆内存中有三个区域:

1.年轻代:用于存放新产生的对象。

2.老年代:用于存放被长期引用的对象。

3.持久带:用于存放Class,method元信息。

如图:

JVM garbage collection algorithm and three areas in the heap memory of jvm

  • 一.年轻代

年轻代中包含两个区:Eden 和survivor,并且用于存储新产生的对象,其中有两个survivor区如图:

JVM garbage collection algorithm and three areas in the heap memory of jvm

可以使用参数配置年轻代的大小,如果配置它为100M那么就相当于2*survivor+Eden = 100M

-Xmn
Copy after login
  • 1

可以配置Eden 和survivor区的大小,这里配置的是比值,jvm中默认为8,意思就是Eden区的内存比上survivor的内存等于8,如果年轻代的Xmn配置的100M,那么Eden就会被分配80M内存,每个survivor分配10M内存

-XX:SurvivorRatio
Copy after login
  • 1

还可以配置年轻代和老年代的比值,这里需要注意:老年代的内存就是通过这个比值设置,jvm没有给你直接设置老年代内存大小的参数;如果整个堆内存设为100M并且在这里设置年轻代和老年代的比值为7,如果持久代占用了10M,那么100M-10M=90M这里的90M就是老年代和年轻代的内存总和,且年轻代占用(90/(7+1)*7)的内存,老年代就占用(90/(7+1)*1)的内存。

-XX:NewRatio
Copy after login
  • 1

  • 二.老年代

年轻代在垃圾回收多次都没有被GC回收的时候就会被放到老年代,以及一些大的对象(比如缓存,这里的缓存是弱引用),这些大对象可以不进入年轻代就直接进入老年代(1.防止新生代有大量剩余的空间,而大对象创建导致提前发生GC;2.防止在eden区和survivor区的大对象复制造成性能问题),这个可以通过如下参数设置,表示单个对象超过了这个值就会直接到老年带(默认为0):

-XX:PretenureSizeThreshold
Copy after login
  • 1

并且大的数组对象也会直接放到老年代,比如array和arrayList(底层用数组实现),因为数组需要连续的空间存储数据。

  • 三.持久代

持久代用来存储class,method元信息,大小配置和项目规模,类和方法的数量有关,一般配置128M就够了,设置原则是预留30%空间,它可以通过如下参数进行大小配置:

-XX: PermSize 
-XX: MaxPermSize
Copy after login
  • 1

  • 2

持久代也可能会被GC回收,如果持久代理的常量池没有被引用以及一些无用的类信息和类的Class对象也会被回收。

相关文章:

jvm garbage collection algorithm

Java detailed explanation of garbage collection and object life cycle

Related videos:

javascriptprimary tutorial

The above is the detailed content of JVM garbage collection algorithm and three areas in the heap memory of jvm. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template