Topic: 请问我是错了还是答案错了

  Print this page

1.请问我是错了还是答案错了 Copy to clipboard
Posted by: aleel_008
Posted on: 2004-10-06 19:44

1.Object aobj = new Object ( ) ;
2.Object bobj = new Object ( ) ;
3.Object cobj = new Object ( ) ;
4.aobj = bobj; //第一个对象满足垃圾收集
5.aobj = cobj;
6.cobj = null;
7.aobj = null; //第三个对象满足垃圾收集
题目问在第几行的时候开始有对象满足垃圾收集
在4行的时候我认为第一个对象已经满足垃圾收集要求了,而答案是第
7行才开始有对象满足垃圾收集,请问我是错了还是答案错了

2.Re:请问我是错了还是答案错了 [Re: aleel_008] Copy to clipboard
Posted by: COWCOW
Posted on: 2004-10-06 19:49

答案对了.

3.Re:请问我是错了还是答案错了 [Re: aleel_008] Copy to clipboard
Posted by: aleel_008
Posted on: 2004-10-06 23:33

给个理由啊

4.Re:请问我是错了还是答案错了 [Re: aleel_008] Copy to clipboard
Posted by: dorrenchen
Posted on: 2004-10-06 23:48

on line 4, instance "aobj" has no other var pointing to it, and thus becomes dangling and inaccessible, cause of memory leak. But there's a distinction of inaccessible and no longer in use. No longer in use means the reference is either is set to null, out of scope and reference is not passed to anyone else. I think JVM will only do a garbage collection (GC) when obj is no longer in use.

Back in C/C++, dangling object is the cause of memory leak. But since in C, you can do pointer arithmatics, so it's possible that you can have one reference created, then make it point to something else, and later you can make it to point it back to the original object instance by some pointer calculation.

I guess java inherit this, even if instance is inaccessible but still in scope, JVM won't GC it.

5.Re:请问我是错了还是答案错了 [Re: aleel_008] Copy to clipboard
Posted by: aleel_008
Posted on: 2004-10-07 03:01

I understand your meaning,but if some object lost its reference in java,it must be collected by system, this is different from C or C++.such as:
Obeject a=new Object();
a=new Object();
the obejct created in Line 1 is eligible for garbage collection ,I am sure of it.
so I think the answer to last question is incorrect if the aobj indeed lost its reference.

6.Re:请问我是错了还是答案错了 [Re: aleel_008] Copy to clipboard
Posted by: dorrenchen
Posted on: 2004-10-07 05:39

I apologize, I was wrong, and you are right. instance "objA" is GCed at line4, and instance "objC" is GCed at line 7.

my program proves it.


public class Garbage{
  static Runtime rt;
  static long total; // total available memory
  static long used; // memory used before the code to be tested
  static long used2; // memory used after the code.
  static char space = ' ';
  static int N = 200; // to run gc() N times, 200 seems to be minimum

  public static void pre_gc(){
    gc(); // clear all other objects that may interfere
    used = total - rt.freeMemory();
  }

  public static void gc(){
    int counter = N;
    do{
      System.gc();
      Thread.yield();      
    }while(counter-- > 0); // call gc() N times
    used2 = total - rt.freeMemory();
  }
  
  public static void run() {
    rt = Runtime.getRuntime();
    total = rt.totalMemory();
    
    Object a = new Object(); // each object takes 8 bytes
    Object b = new Object();
    Object c = new Object();
    
    pre_gc();
    a = b;
    gc();
    System.out.print(used); System.out.print(space); System.out.println(used2);
    
    pre_gc();
    a = c;
    gc();
    System.out.print(used); System.out.print(space); System.out.println(used2);
    
    pre_gc();
    c = null;
    gc();
    System.out.print(used); System.out.print(space); System.out.println(used2);
    
    pre_gc();
    a = null;
    gc();
    System.out.print(used); System.out.print(space); System.out.println(used2);
  }
  
  public static void main(String args[]){
    Garbage.run();
  }
}


7.Re:请问我是错了还是答案错了 [Re: aleel_008] Copy to clipboard
Posted by: aleel_008
Posted on: 2004-10-07 15:17

Thank you for your so careful and splendid explanation.I learned a right measure to deal with these problems like this.

8.Re:请问我是错了还是答案错了 [Re: aleel_008] Copy to clipboard
Posted by: arthas1982
Posted on: 2004-10-09 09:47

只有在当前线程消亡了时候,此线程的类变量才被JVM收集。dorrenchen仁兄的功底好深啊,小弟一时没有看懂。小弟写了个自己的,大家看看
import java.lang.ref.WeakReference;

public class Garbage{
public Object a;
public Object b;
public Object c;
public WeakReference aR; //WeakReference 不会影响JVM对他所引用的对象的收集
public WeakReference bR;
public WeakReference cR;

public Garbage(){
a = new Object();
b = new Object();
c = new Object();
aR = new WeakReference(a);
bR = new WeakReference(b);
cR = new WeakReference(c);
}

public void run(){
System.out.println("run() Begin!");
print();
a = b;
System.out.println("execute a = b");
print();
a = c;
System.out.println("execute a = c");
print();
c = null;
System.out.println("execute c = null");
print();
a = null;
System.out.println("execute a = null");
print();
System.out.println("run() End!");
}

public void print(){
System.out.println("a :"+aR.get());//如果a:后是null,则说明他被回收了
System.out.println("b :"+bR.get());
System.out.println("c :"+cR.get());
}
}
public class Test {

  public static void main(String[] args) {
    Garbage g = new Garbage();
    g.run();
  }
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923