[Reserved]-depth understanding of Java garbage collection

Depth understanding of Java garbage collection

Original: http://www.linuxidc.com/Linux/2015-06/118829.htm

 

Meaning a garbage collection mechanism

  Java language in a notable feature is the introduction of garbage collection mechanism to enable c ++ programmers the most headache solved the problem of memory management, which allows Java programmers in the preparation of the program no longer need to consider memory management. Because there is a garbage collection mechanism, Java objects are no longer in the "scope" concept, and only the object reference have no "scope." Garbage collection can effectively prevent memory leaks, effective use of free memory.

  ps: memory leaks means not recovered after the memory space is used up, in the general case does not involve complex data structures, Java memory leaks appear as a memory object life cycle beyond the program needs its length of time, we sometimes It referred to as "the object free."

Second, the garbage collection algorithm in

  Java language specification does not clearly explain what kind of JVM garbage collection algorithm to use, but any kind of garbage collection algorithms generally do two basic things: (1) find useless information objects; (2) recovery is useless objects occupy memory space so that the space can be re-used program.  

1. Reference counting (Reference Counting Collector)

1.1 Analysis of Algorithms 

  Reference counting garbage collector is an early strategy of. In this method, each heap object instance has a reference count. When an object is created, and the object instance assigned to a variable, the variable count is set to 1. When any other variable is assigned to the object's reference count (Counter +1 a = b, then the referenced object instance b) plus 1, but a reference to an object instance or more than the life-cycle is set to when a new value object references an instance of the counter is decremented. Any reference to an object instance counter 0 can be garbage collected. When an object instance is garbage collected, any object instance that references a reference counter by one.

1.2 advantages and disadvantages

advantage:

  Reference counting collectors can quickly performed, interleaving in the program run. More favorable procedures need not be interrupted for a long time real-time environment.

Disadvantages:

  Unable to detect circular references. As a parent there is a sub-object references, in turn, the child object reference to the parent object. In this way, they can never be the reference count is zero.

1 reference count algorithms can not resolve the circular reference problem, for example:

public class Main {
    public static void main(String[] args) {
        MyObject object1 = new MyObject();
        MyObject object2 = new MyObject();
        
        object1.object = object2;
        object2.object = object1;
        
        object1 = null;
        object2 = null;
    }
}

The last two will face object1 and object2 assignment is null, that is to say object1 and object2 point has been the object can no longer be accessed, but because they refer to each other, causing them to reference counter is not 0, then the garbage collector they never recovered.

2.tracing algorithm (Tracing Collector) or mark - sweep algorithm (mark and sweep)

2.1 Search Algorithm

  Root search algorithm is introduced from discrete mathematics graph theory, all references to the program viewed as a relationship diagram from one node GC ROOT start, find the corresponding reference node, find the node after node continue to look for this reference node, when all the reference nodes looking for is completed, the remaining nodes is considered not to be a reference to the node, the node that is useless.

java may have as an object of GC Root

  1. VM stack objects referenced (local variable table)

  2. The method of the object in a static property references

  3. The method of the object in the constant reference

  4. The objects referenced in native method stacks (Native objects)

Schematic 2.2tracing algorithm

2.3 mark - sweep algorithm analysis

  Mark - sweep algorithm scans from the root set of objects surviving object tag, the tag is completed, then the entire scanned object space unlabeled, recovered, as shown in FIG. Mark - sweep algorithm does not require moving object, and the object is not only surviving treated in more live objects situation is extremely efficient, but the mark - sweep algorithm directly recycled objects do not survive, it will cause memory fragmentation .

3.compacting algorithm or mark - Collation Algorithm

  Mark - sorting algorithm mark - sweep algorithm same way mark object, but they differ in the removal, after the object space occupied by the recovery nonviable, will all live objects moves to the left end of the free space, and updates the corresponding pointer. Mark - Collation Algorithm in mark - sweep algorithm based on, has conducted a moving target, and therefore more costly, but it solves the problem of memory fragmentation. Based on the collector Compacting algorithm, in general, increase handle and handle table.

4.copying算法(Compacting Collector)

  该算法的提出是为了克服句柄的开销和解决堆碎片的垃圾回收。它开始时把堆分成 一个对象 面和多个空闲面, 程序从对象面为对象分配空间,当对象满了,基于copying算法的垃圾 收集就从根集中扫描活动对象,并将每个 活动对象复制到空闲面(使得活动对象所占的内存之间没有空闲洞),这样空闲面变成了对象面,原来的对象面变成了空闲面,程序会在新的对象面中分配内存。一种典型的基于coping算法的垃圾回收是stop-and-copy算法,它将堆分成对象面和空闲区域面,在对象面与空闲区域面的切换过程中,程序暂停执行。

5.generation算法(Generational Collector)

  分代的垃圾回收策略,是基于这样一个事实:不同的对象的生命周期是不一样的。因此,不同生命周期的对象可以采取不同的回收算法,以便提高回收效率。

年轻代(Young Generation)

  1.所有新生成的对象首先都是放在年轻代的。年轻代的目标就是尽可能快速的收集掉那些生命周期短的对象。

  2.新生代内存按照8:1:1的比例分为一个eden区和两个survivor(survivor0,survivor1)区。一个Eden区,两个 Survivor区(一般而言)。大部分对象在Eden区中生成。回收时先将eden区存活对象复制到一个survivor0区,然后清空eden区,当这个survivor0区也存放满了时,则将eden区和survivor0区存活对象复制到另一个survivor1区,然后清空eden和这个survivor0区,此时survivor0区是空的,然后将survivor0区和survivor1区交换,即保持survivor1区为空, 如此往复。

  3.当survivor1区不足以存放 eden和survivor0的存活对象时,就将存活对象直接存放到老年代。若是老年代也满了就会触发一次Full GC,也就是新生代、老年代都进行回收

  4.新生代发生的GC也叫做Minor GC,MinorGC发生频率比较高(不一定等Eden区满了才触发)

年老点(Old Generation)

  1.在年轻代中经历了N次垃圾回收后仍然存活的对象,就会被放到年老代中。因此,可以认为年老代中存放的都是一些生命周期较长的对象。

  2.内存比新生代也大很多(大概比例是1:2),当老年代内存满时触发Major GC即Full GC,Full GC发生频率比较低,老年代对象存活时间比较长,存活率标记高。

持久代(Permanent Generation)

  用于存放静态文件,如Java类、方法等。持久代对垃圾回收没有显著影响,但是有些应用可能动态生成或者调用一些class,例如Hibernate 等,在这种时候需要设置一个比较大的持久代空间来存放这些运行过程中新增的类。

6. adaptive算法(Adaptive Collector)
  在特定的情况下,一些垃圾收集算法会优于其它算法。基于Adaptive算法的垃圾收集器就是监控当前堆的使用情况,并将选择适当算法的垃圾收集器。

三.GC(垃圾收集器)

新生代收集器使用的收集器:Serial、PraNew、Parallel Scavenge

老年代收集器使用的收集器:Serial Old、Parallel Old、CMS

Serial收集器(复制算法)

  新生代单线程收集器,标记和清理都是单线程,优点是简单高效。

Serial Old收集器(标记-整理算法)

  老年代单线程收集器,Serial收集器的老年代版本。

ParNew收集器(停止-复制算法) 

  新生代收集器,可以认为是Serial收集器的多线程版本,在多核CPU环境下有着比Serial更好的表现。

Parallel Scavenge收集器(停止-复制算法)

  并行收集器,追求高吞吐量,高效利用CPU。吞吐量一般为99%, 吞吐量= 用户线程时间/(用户线程时间+GC线程时间)。适合后台应用等对交互相应要求不高的场景。

Parallel Old收集器(停止-复制算法)

  Parallel Scavenge收集器的老年代版本,并行收集器,吞吐量优先

CMS(Concurrent Mark Sweep)收集器(标记-清理算法)

  高并发、低停顿,追求最短GC回收停顿时间,cpu占用比较高,响应时间快,停顿时间短,多核cpu 追求高响应时间的选择

四、GC的执行机制

  由于对象进行了分代处理,因此垃圾回收区域、时间也不一样。GC有两种类型:Scavenge GC和Full GC。

Scavenge GC

一般情况下,当新对象生成,并且在Eden申请空间失败时,就会触发Scavenge GC,对Eden区域进行GC,清除非存活对象,并且把尚且存活的对象移动到Survivor区。然后整理Survivor的两个区。这种方式的GC是对年轻代的Eden区进行,不会影响到年老代。因为大部分对象都是从Eden区开始的,同时Eden区不会分配的很大,所以Eden区的GC会频繁进行。因而,一般在这里需要使用速度快、效率高的算法,使Eden去能尽快空闲出来。

Full GC

对整个堆进行整理,包括Young、Tenured和Perm。Full GC因为需要对整个对进行回收,所以比Scavenge GC要慢,因此应该尽可能减少Full GC的次数。在对JVM调优的过程中,很大一部分工作就是对于FullGC的调节。有如下原因可能导致Full GC:

  1.年老代(Tenured)被写满

  2.持久代(Perm)被写满

  3.System.gc()被显示调用

  4.上一次GC之后Heap的各域分配策略动态变化

System.gc()方法

      命令行参数透视垃圾收集器的运行
  使用System.gc()可以不管JVM使用的是哪一种垃圾回收的算法,都可以请求Java的垃圾回收。在命令行中有一个参数-verbosegc可以查看Java使用的堆内存的情况,它的格式如下:
  java -verbosegc classfile
  可以看个例子:
  
class TestGC  
{  
    public static void main(String[] args)  
    {  
      new TestGC();  
      System.gc();  
      System.runFinalization();  
   }  
}  
      
  在这个例子中,一个新的对象被创建,由于它没有使用,所以该对象迅速地变为不可达,程序编译后,执行命令: java -verbosegc TestGC 后结果为:
  [Full GC 168K->97K(1984K), 0.0253873 secs]
  机器的环境为,Windows 2000 + JDK1.3.1,箭头前后的数据168K和97K分别表示垃圾收集GC前后所有存活对象使用的内存容量,说明有168K-97K=71K的对象容量被回收,括号内的数据1984K为堆内存的总容量,收集所需要的时间是0.0253873秒(这个时间在每次执行的时候会有所不同)。


      需要注意的是,调用System.gc()也仅仅是一个请求(建议)。JVM接受这个消息后,并不是立即做垃圾回收,而只是对几个垃圾回收算法做了加权,使垃圾回收操作容易发生,或提早发生,或回收较多而已。

finalize()方法

      在JVM垃圾回收器收集一个对象之前,一般要求程序调用适当的方法释放资源,但在没有明确释放资源的情况下,Java提供了缺省机制来终止该对象心释放资源,这个方法就是finalize()。它的原型为:
  protected void finalize() throws Throwable
  在finalize()方法返回之后,对象消失,垃圾收集开始执行。原型中的throws Throwable表示它可以抛出任何类型的异常。
  之所以要使用finalize(),是存在着垃圾回收器不能处理的特殊情况。假定你的对象(并非使用new方法)获得了一块“特殊”的内存区域,由于垃圾回收器只知道那些显示地经由new分配的内存空间,所以它不知道该如何释放这块“特殊”的内存区域,那么这个时候java允许在类中定义一个由finalize()方法。


      特殊的区域例如:1)由于在分配内存的时候可能采用了类似 C语言的做法,而非JAVA的通常new做法。这种情况主要发生在native method中,比如native method调用了C/C++方法malloc()函数系列来分配存储空间,但是除非调用free()函数,否则这些内存空间将不会得到释放,那么这个时候就可能造成内存泄漏。但是由于free()方法是在C/C++中的函数,所以finalize()中可以用本地方法来调用它。以释放这些“特殊”的内存空间。2)又或者打开的文件资源,这些资源不属于垃圾回收器的回收范围。
      换言之,finalize()的主要用途是释放一些其他做法开辟的内存空间,以及做一些清理工作。因为在JAVA中并没有提够像“析构”函数或者类似概念的函数,要做一些类似清理工作的时候,必须自己动手创建一个执行清理工作的普通方法,也就是override Object这个类中的finalize()方法。例如,假设某一个对象在创建过程中会将自己绘制到屏幕上,如果不是明确地从屏幕上将其擦出,它可能永远都不会被清理。如果在finalize()加入某一种擦除功能,当GC工作时,finalize()得到了调用,图像就会被擦除。要是GC没有发生,那么这个图像就会被一直保存下来。

      一旦垃圾回收器准备好释放对象占用的存储空间,首先会去调用finalize()方法进行一些必要的清理工作。只有到下一次再进行垃圾回收动作的时候,才会真正释放这个对象所占用的内存空间。
  在普通的清除工作中,为清除一个对象,那个对象的用户必须在希望进行清除的地点调用一个清除方法。这与C++"析构函数"的概念稍有抵触。在C++中,所有对象都会破坏(清除)。或者换句话说,所有对象都"应该"破坏。若将C++对象创建成一个本地对象,比如在堆栈中创建(在Java中是不可能的,Java都在堆中),那么清除或破坏工作就会在"结束花括号"所代表的、创建这个对象的作用域的末尾进行。若对象是用new创建的(类似于Java),那么当程序员调用C++的 delete命令时(Java没有这个命令),就会调用相应的析构函数。若程序员忘记了,那么永远不会调用析构函数,我们最终得到的将是一个内存"漏洞",另外还包括对象的其他部分永远不会得到清除。
  相反,Java不允许我们创建本地(局部)对象--无论如何都要使用new。但在Java中,没有"delete"命令来释放对象,因为垃圾回收器会帮助我们自动释放存储空间。所以如果站在比较简化的立场,我们可以说正是由于存在垃圾回收机制,所以Java没有析构函数。然而,随着以后学习的深入,就会知道垃圾收集器的存在并不能完全消除对析构函数的需要,或者说不能消除对析构函数代表的那种机制的需要(原因见下一段。另外finalize()函数是在垃圾回收器准备释放对象占用的存储空间的时候被调用的,绝对不能直接调用finalize(),所以应尽量避免用它)。若希望执行除释放存储空间之外的其他某种形式的清除工作,仍然必须调用Java中的一个方法。它等价于C++的析构函数,只是没后者方便。
      在C++中所有的对象运用delete()一定会被销毁,而JAVA里的对象并非总会被垃圾回收器回收。In another word, 1 对象可能不被垃圾回收,2 垃圾回收并不等于“析构”,3 垃圾回收只与内存有关。也就是说,并不是如果一个对象不再被使用,是不是要在finalize()中释放这个对象中含有的其它对象呢?不是的。因为无论对象是如何创建的,垃圾回收器都会负责释放那些对象占有的内存。

五、Java有了GC同样会出现内存泄露问题

1.静态集合类像HashMap、Vector等的使用最容易出现内存泄露,这些静态变量的生命周期和应用程序一致,所有的对象Object也不能被释放,因为他们也将一直被Vector等应用着。

Static Vector v = new Vector(); 
for (int i = 1; i<100; i++) 

    Object o = new Object(); 
    v.add(o); 
    o = null; 
}

在这个例子中,代码栈中存在Vector 对象的引用 v 和 Object 对象的引用 o 。在 For 循环中,我们不断的生成新的对象,然后将其添加到 Vector 对象中,之后将 o 引用置空。问题是当 o 引用被置空后,如果发生 GC,我们创建的 Object 对象是否能够被 GC 回收呢?答案是否定的。因为, GC 在跟踪代码栈中的引用时,会发现 v 引用,而继续往下跟踪,就会发现 v 引用指向的内存空间中又存在指向 Object 对象的引用。也就是说尽管o 引用已经被置空,但是 Object 对象仍然存在其他的引用,是可以被访问到的,所以 GC 无法将其释放掉。如果在此循环之后, Object 对象对程序已经没有任何作用,那么我们就认为此 Java 程序发生了内存泄漏。

2.各种连接,数据库连接,网络连接,IO连接等没有显示调用close关闭,不被GC回收导致内存泄露。

3.监听器的使用,在释放对象的同时没有相应删除监听器的时候也可能导致内存泄露。

六. 减少GC开销的措施

  根据上述GC的机制,程序的运行会直接影响系统环境的变化,从而影响GC的触发。若不针对GC的特点进行设计和编码,就会出现内存驻留等一系列负面影响。为了避免这些影响,基本的原则就是尽可能地减少垃圾和减少GC过程中的开销。具体措施包括以下几个方面:
  (1)不要显式调用System.gc()
  此函数建议JVM进行主GC,虽然只是建议而非一定,但很多情况下它会触发主GC,从而增加主GC的频率,也即增加了间歇性停顿的次数。
  (2)尽量减少临时对象的使用
  临时对象在跳出函数调用后,会成为垃圾,少用临时变量就相当于减少了垃圾的产生,从而延长了出现上述第二个触发条件出现的时间,减少了主GC的机会。

  (3)对象不用时最好显式置为Null
  一般而言,为Null的对象都会被作为垃圾处理,所以将不用的对象显式地设为Null,有利于GC收集器判定垃圾,从而提高了GC的效率。
  (4)尽量使用StringBuffer,而不用String来累加字符串
  由于String是固定长的字符串对象,累加String对象时,并非在一个String对象中扩增,而是重新创建新的String对象,如Str5=Str1+Str2+Str3+Str4,这条语句执行过程中会产生多个垃圾对象,因为对次作“+”操作时都必须创建新的String对象,但这些过渡对象对系统来说是没有实际意义的,只会增加更多的垃圾。避免这种情况可以改用StringBuffer来累加字符串,因StringBuffer是可变长的,它在原有基础上进行扩增,不会产生中间对象。
  (5)能用基本类型如Int,Long,就不用Integer,Long对象
  基本类型变量占用的内存资源比相应对象占用的少得多,如果没有必要,最好使用基本变量。
  (6)尽量少用静态对象变量
  静态变量属于全局变量,不会被GC回收,它们会一直占用内存。
  (7)分散对象创建或删除的时间
  集中在短时间内大量创建新对象,特别是大对象,会导致突然需要大量内存,JVM在面临这种情况时,只能进行主GC,以回收内存或整合内存碎片,从而增加主GC的频率。集中删除对象,道理也是一样的。它使得突然出现了大量的垃圾对象,空闲空间必然减少,从而大大增加了下一次创建新对象时强制主GC的机会。

      下面这个例子向大家展示了垃圾收集所经历的过程,并对前面的陈述进行了总结。

  1.  
    class Chair {
  2.  
       static boolean gcrun = false;
  3.  
       static boolean f = false;
  4.  
       static int created = 0;
  5.  
       static int finalized = 0;
  6.  
       int i;
  7.  
      Chair() {
  8.  
       i = ++created;
  9.  
       if(created == 47)
  10.  
        System.out.println( "Created 47");
  11.  
      }
  12.  
       protected void finalize() {
  13.  
       if(!gcrun) {
  14.  
        gcrun = true;
  15.  
        System.out.println( "Beginning to finalize after " + created + " Chairs have been created");
  16.  
       }
  17.  
       if(i == 47) {
  18.  
        System.out.println( "Finalizing Chair #47, " +"Setting flag to stop Chair creation");
  19.  
        f = true;
  20.  
       }
  21.  
       finalized++;
  22.  
       if(finalized >= created)
  23.  
        System.out.println( "All " + finalized + " finalized");
  24.  
      }
  25.  
    }
  26.  
     
  27.  
    public class Garbage {
  28.  
       public static void main(String[] args) {
  29.  
       if(args.length == 0) {
  30.  
        System.err.println( "Usage: /n" + "java Garbage before/n or:/n" + "java Garbage after");
  31.  
        return;
  32.  
      }
  33.  
       while(!Chair.f) {
  34.  
        new Chair();
  35.  
        new String("To take up space");
  36.  
      }
  37.  
      System.out.println( "After all Chairs have been created:/n" + "total created = " + Chair.created +
  38.  
       ", total finalized = " + Chair.finalized);
  39.  
       if(args[0].equals("before")) {
  40.  
        System.out.println( "gc():");
  41.  
        System.gc();
  42.  
        System.out.println( "runFinalization():");
  43.  
        System.runFinalization();
  44.  
      }
  45.  
      System.out.println( "bye!");
  46.  
       if(args[0].equals("after"))
  47.  
        System.runFinalizersOnExit( true);
  48.  
      }
  49.  
    }

      The above program creates many Chair objects, and at some point after the garbage collector starts running, the program will stop creating Chair. Since the garbage collector may run at any time, so we can not know exactly when it started. Therefore, the program use a tag named gcrun to indicate whether the garbage collector has started running. Using the second mark f, Chair tells main () it should stop generating object. Both marks are in the finalize () internal settings, it calls for during garbage collection. The other two static variables --created and finalized-- are used to track the number of objects that have been created and the number of objects the garbage collector has completed the finishing touches. Finally, each Chair has its own (non-static) int i, so it can keep track of how much the specific numbers. After No. 47 Chair finish finishing touches, will mark set to true, finally ended the process of creating Chair of the object.

 

 

Guess you like

Origin www.cnblogs.com/sanmuqingliang/p/11792134.html
Recommended