当:
   F@0: False
   F@1: True
   F@2: False
结果为:很明显了,看了就知道了。呵呵。
Created:1
i1:1  this=1
Beginning to finalize after 1 Chairs have been created
All 1 finalized
Created:2
Created:3
i3:3  this=3
i2:3  this=2
All 3 finalized
Created:4
Created:5
i5:5  this=5
i4:5  this=4
All 5 finalized
Created:6
Created:7
i7:7  this=7
i6:7  this=6
All 7 finalized
Created:8
Created:9
i9:9  this=9
i8:9  this=8
All 9 finalized
Created:10
Created:11
i11:11  this=11
i10:11  this=10
All 11 finalized
Created:12
Created:13
i13:13  this=13
i12:13  this=12
All 13 finalized
Created:14
Created:15
i15:15  this=15
i14:15  this=14
All 15 finalized
Created:16
Created:17
i17:17  this=17
i16:17  this=16
All 17 finalized
Created:18
Created:19
i19:19  this=19
i18:19  this=18
All 19 finalized
Created:20
Created:21
i21:21  this=21
i20:21  this=20
All 21 finalized
Created:22
Created:23
i23:23  this=23
i22:23  this=22
All 23 finalized
Created:24
Created:25
i25:25  this=25
i24:25  this=24
All 25 finalized
Created:26
Created:27
i27:27  this=27
i26:27  this=26
All 27 finalized
Created:28
Created:29
i29:29  this=29
i28:29  this=28
All 29 finalized
Created:30
Created:31
i31:31  this=31
i30:31  this=30
All 31 finalized
Created:32
Created:33
i33:33  this=33
i32:33  this=32
All 33 finalized
Created:34
Created:35
i35:35  this=35
i34:35  this=34
All 35 finalized
Created:36
Created:37
i37:37  this=37
i36:37  this=36
All 37 finalized
Created:38
Created:39
i39:39  this=39
i38:39  this=38
All 39 finalized
Created:40
Created:41
i41:41  this=41
i40:41  this=40
All 41 finalized
Created:42
Created:43
i43:43  this=43
i42:43  this=42
All 43 finalized
Created:44
Created:45
i45:45  this=45
i44:45  this=44
All 45 finalized
Created:46
Created:47
i47:47  this=47
Finalizing Chair $47, Setting flag to stop Chair creation
i46:47  this=46
All 47 finalized
After all Chair have been Created:
  Total Created = 47  Total finalized = 47
bye!

解决方案 »

  1.   

    当:
       F@0: False
       F@1: False
       F@2: True
    结果为:Created:1 //从1-697,未释放;最后面的10625也没有共有698个未清理
    Created:2
    Created:3
    ... //做++
    Created:697
    Created:698
    i698:698  this=698
    Beginning to finalize after 698 Chairs have been created
    -----end finalized;
    i697:698  this=697
    -----end finalized;
    i696:698  this=696
    -----end finalized;
    Created:699 //F#0
    Created:700
    ... //做++
    Created:1782
    Created:1783
    i1783:1783  this=1783 //1783开始-
    -----end finalized;
    i699:1783  this=699 //F#0 开始+
    -----end finalized;
    i1782:1783  this=1782
    -----end finalized;
    i700:1783  this=700
    -----end finalized;
    i1781:1783  this=1781
    -----end finalized;
    i701:1783  this=701
    -----end finalized;
    i1780:1783  this=1780
    -----end finalized;
    i702:1783  this=702
    ...
    i1239:1783  this=1239
    -----end finalized;
    i1242:1783  this=1242
    -----end finalized;
    i1240:1783  this=1240
    -----end finalized;
    i1241:1783  this=1241
    -----end finalized; //到这里第二组全部释放.698-1783Created:1784 //第三组(次)开始
    Created:1785
    Created:1786
    ...
    Created:1925
    Created:1926
    Created:1927
    i1927:1927  this=1927
    -----end finalized;
    i1784:1927  this=1784
    -----end finalized;
    ...
    i1856:1927  this=1856
    -----end finalized;
    i1855:1927  this=1855
    -----end finalized; //同组二,释放完毕.1784-1927Created:1928 //第四组(次),开始-释放,1928-2907
    ... //忽略同组二,组三;
    i2417:2907  this=2417
    -----end finalized;Created:2908 //第五组(次),开始-释放,2908-3142
    ... //忽略同上
    i3025:3142  this=3025
    -----end finalized;
    ...
    Created:10262
    Created:10263
    Created:10264
    Created:i10264:10265  this=10264 //这10265跑到最后面去了。说明啥问题啊?好象是想拉屎的时候发现厕所没冲,还要先冲冲再拉
    -----end finalized;
    - - - - - - - - - stoped - - - - - =
    i10263:10265  this=10263
    -----end finalized;
    - - - - - - - - - stoped - - - - - =
    i10262:10265  this=10262
    -----end finalized;
    - - - - - - - - - stoped - - - - - =
    ...
    ...
    ...i9776:10265  this=9776
    -----end finalized;
    i9775:10265  this=9775
    -----end finalized;
    i9774:10265  this=9774
    -----end finalized;
    i9773:10265  this=9773
    -----end finalized;
    10265
    After all Chair have been Created:
      Total Created = 10265  Total finalized = 9567
    bye!详细结果在:http://www.86579.cn/result.txt
    ............................................................
    /** 循环     统计如下:
     * -------------------------------------------------------
     *         开始值   结束值    数量
     * 组一    1        697       697
     * 组二    698      1783      1087
     * 组三    1784     1927      144
     * 组四    1928     2907      980
     * 组五    2908     3142      235
     * 组六    3143     4029      887
     * 组七    4030     4344      315
     * 组八    4345     5168      824
     * 组九    5169     5540      372
     * 组○    5541     6316      776
     * 组⒑    6317     6729      412
     * 组⒒    6730     7466      737
     *    . . . . . . . . . . . .
     * 最后    9773     10264     492
     *    释放趋于平和
     *    是不是系统在动态的调整
     */
    ............................................................
      

  2.   

    gc在大部分状况下会将没有任何指向的引用的内存释放掉,除非在特殊情况下,最好不要干涉gc!
    runFinalization()方法在调用时它会尽量去调用这个对象的finalize方法!
    都是强制gc去回收内存,其它大部分相似!
      

  3.   


    路过
    学习ing
    关注ing
    帮您顶研究下也不妨事啊
      

  4.   

    to:onefox
      为什么啦?
      在thinking in java中建议程序员在适当的时候去调用这几个函数。以更好的释放资源啊!to:zhutouzip
      恩。谢谢点拨,很有道理;早说的话我就不用花n小时做这个测试了。to:parol2910
      一起学习。我也是ccnto;007remember
      谢谢你。我代表毛zx向您致敬,并向他老人家发誓给你分。!!!
      

  5.   

    再测试:当:
       F@0: True
       F@1: False
       F@2: FalseD:\MyJava>javac Garbage.java
    D:\MyJava>java  Garbage >> c:\result.txt
    D:\MyJava>结果为:-----------------
    Created:1
    Created:2
    Created:3
    .....
    Created:997
    Created:998
    Created:999
    Created:1000
    After all Chair have been Created:
      Total Created = 1000  Total finalized = 0
    bye!
      

  6.   

    我又做了测试:
      验证了zhutouzip说的:
          runFinalization()方法在调用时它会尽量去调用这个对象的finalize方法!
    当:
       F@0: True
       F@1: False
       F@2: TrueD:\MyJava>javac Garbage.java
    D:\MyJava>java  Garbage >> c:\result1.txt
    D:\MyJava>结果为:-----------------
    Created:1
    Created:2
    Created:3
    ...
    Created:757
    Created:758
    Created:759
    i697:759  this=697
    Beginning to finalize after 759 Chairs have been created
    -----end finalized;
    i695:759  this=695
    -----end finalized;
    i694:759  this=694
    -----end finalized;
    i696:759  this=696
    -----end finalized;
    Created:760
    Created:761
    ...
    Created:998
    Created:999
    Created:1000
    After all Chair have been Created:
      Total Created = 1000  Total finalized = 4
    bye!
      

  7.   

    那就是说调用 runFinalization() 是必要的嘛。
    在执行它时会考虑操作系统是否必要清理系统垃圾。
      

  8.   

    finalize方法是当jvm在得知内存不够时调用,但在何时内存不够没有一个明确的标准,也就是jvm调用这个方法是随机的!
      

  9.   

    哈。。我也发现了这个问题。刚才我一直在调试;就是我不能在回复了。。呵呵。。回复的内容都已经写好了~~看下面的结果:。代码方面我稍做了修改
    ------------------------------------------------------------------
    好象不对。runFinalization()调用runFinalization()是随机的。
    我把源代码中Line46的 new Chair();改成下面的if语句:
    if( new Chair().i==900 ) {
        System.out.println("i====================================900");
        for( int j=0; j<10000; j++ ) {
           System.runFinalization();
           System.out.println("     " + j);
        }
    } F@0: True
     F@1: False
     F@2: False
    D:\MyJava>javac Garbage.java
    D:\MyJava>java  Garbage >> c:\result2.txt
    D:\MyJava>
    结果确是------
    Created:1
    Created:2
    Created:3
    ...
    Created:899
    Created:900
    i===========================900
         0
         1
         2
         ...
         212
         213
         214
    i900:900  this=900
    Beginning to finalize after 900 Chairs have been created
    -----end finalized;
    i899:900  this=899
    -----end finalized;
    i898:900  this=898
    -----end finalized;
         215
         216
         217
         ....
         9997
         9998
         9999
    Created:901
    Created:902
    Created:903
    Created:904
    ....
    Created:968
    Created:969
    Created:970
    i901:970  this=901
    -----end finalized;
    i969:970  this=969
    -----end finalized;
    i902:970  this=902
    -----end finalized;
    .....
    i934:970  this=934
    -----end finalized;
    i936:970  this=936
    -----end finalized;
    i935:970  this=935
    -----end finalized;
    Created:971
    Created:972
    Created:973
    ....
    Created:998
    Created:999
    Created:1000
    After all Chair have been Created:
      Total Created = 1000  Total finalized = 72
    bye!
      

  10.   

    构造到970的时候。并没有执行/调用 runFinalization()函数。不过我还有点疑问。如果真是不需要调用runFinalization();方法finalize是系统按照内存不足的时候再调用执行的话。那为什么我每次运行的结果都是不变的。也就是都在j=214和i=970的时候在执行。按道理应该出现不同的结果啊 ?不应该固定在这两个地方啊。
      

  11.   

    我就是想清楚JVM是在什么情况下执行finalize()方法的。
    感觉有些含糊。不可理解。
      

  12.   

    你看看thinking in java,上面说垃圾回收器会在它判断"可用内存数量不足时"时才开始执行,但究竟何时jvm才认为可用内存不足呢?好像我们人为情况下不知道,所以这种情况是有些含糊,甚至于我们不知道究竟何时finalize方法会执行,所以里面带了一点随机性!
      

  13.   

    to: zhutouzip   谢谢你哦。to:007remember;
       我再次向毛zx发誓给你分。我明天中午结帖。
      

  14.   

    没办法。这年头。被美女甩了。
    极度3心ing...
    不这样做的话。日子都没法过了。
      

  15.   

    哈哈哈。。今天是刚好被她甩了1周纪念日。也是我学java的1周纪念日。好好纪念纪念。也发誓要狂学java。
      

  16.   

    函数 runFinalization() 没起什么效果。
    写不写系统都自动会调用。只是地方不同。我在两台机器上做测试,系统都在两个点(全部都在那两个范围段,见下面)上进行清理;
    其中有一台机器:有一个点是相同的。同时运行大游戏的时候。会发生变化。
    不过都在这两个范围段。结束游戏后。两个点都复位。
    Created:900
    i====================================900
         0
         1
         2
        ...
        200-500 :-> POINT 1
        ...
         9999
    Created:901
    ...
    Created:901
    ...
    901-1000    :->  POINT 2(有一台机器。不管是不是调用了finalize(),都在这里)
    ...
    Created:1000
    After all Chair have been Created:
      Total Created = 1000  Total finalized = 47
    bye!
      

  17.   

    楼主学习java一周就能做这样的试验,真是令我佩服~我学一周的时候连hello world都没调对呢……楼主,生活还得carry on~
      

  18.   

    gc在大部分状况下会将没有任何指向的引用的内存释放掉,除非在特殊情况下,最好不要干涉gc!
    runFinalization()方法在调用时它会尽量去调用这个对象的finalize方法!
    都是强制gc去回收内存,其它大部分相似!
      

  19.   

    TO :watcher_shen
       只是起步点不同。也没什么的,我会C++;还有就是每晚都看到1点。有些拼命而已。
       或许你在我的基础上的话可能比我强多了。
    TO :OnlyFor_love
       你的观点我在哪里看到过。不过按照我的调试结果来看。和你的看法有些不一样哦。
       runFinalization()方法的效果不明显。用不用没多少区别。