垃圾收集相关 [英] Garbage collection related

查看:64
本文介绍了垃圾收集相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System; 
 
class MyClass { 
  int a, b; // private 
 
  // Create a class factory for MyClass. 
  public MyClass Factory(int i, int j) { 
    MyClass t = new MyClass(); 
    
    t.a = i; 
    t.b = j; 
 
    return t; // return an object 
  }  
  

public void Show() { 
    Console.WriteLine("a and b: " + a + " " + b); 
  } 
 
} 
  
class MakeObjects { 
  static void Main() {   
    MyClass ob = new MyClass(); 
    int i, j; 
 
    // Generate objects using the factory. 
    for(i=0, j=10; i < 10; i++, j--) { 
      MyClass anotherOb = ob.Factory(i, j); // make an object 
      anotherOb.Show(); 
    }        
    Console.WriteLine();    
  } 
}




有2个问题,但相互关联:

1.当返回一个对象时,就像上面的代码中返回t一样,它应该通过引用来实现.因此,我可以说对了,在for循环的那个实例中,anotherOb和t都引用了一个新对象?

2.如果是这样,则在每次for循环迭代中anotherOb超出范围时,将不会有任何垃圾回收,因为t仍然是对该对象的引用.




There are 2 questions but inter-linked:

1.when an object is returned, like when t is returned in the above code, it should be by reference. Hence, will I be right to say that at that instance of the for loop, anotherOb and t are both reference to a new object?

2. If so, when the anotherOb goes out of scope for every for loop iteration, there will not be any garbage collection since t is still a reference to that object.Is it true since garbage collection takes place when there is no reference to the object?

推荐答案

1)Factory的代码中,我们可以看到已经创建了一个新对象并将其分配给t .该引用被返回并且超出范围,但是当它被分配给anotherObj时,anotherObj引用了相同的实例.在每次循环迭代中,此变量都超出范围(因为在内部循环中声明了该变量);引用的实例将释放所有引用,并计划将其删除.在循环的每次迭代中,都会创建一个新实例,将其显示并计划将其删除.

2)更确切地说,GC根据其自身的策略执行对象的销毁并回收内存.您不能假设发生任何特定类型的瞬间(并且任何基于这种假设的代码都是不正确的).通过创建析构函数并记录其调用,可以观察到此行为.另外,可以使用System.GC.Collect修改此行为,请参见 http://msdn.microsoft. com/en-us/library/system.gc.aspx [ ^ ].除非您有真正的狡猾计划 :-),否则不建议这样做.

注释:

由于这种GC行为,编写析构函数的活动意义不大.借助.NET,它们很少使用.

失去对该对象的所有引用并不是一件容易的事.考虑对象A持有对对象B的引用,对象B持有对对象C的引用,而对象C持有对对象A的引用.请考虑对这些对象中的任何一个对象释放所有其他引用,除了那些循环引用.这三个物体会被破坏吗?尽管他们似乎保护彼此免受破坏,但不要担心,意志.它们将被销毁,因为无法从可能在以后运行的任何实际代码中访问它们.因此,GC机制一点也不微不足道.

—SA
1) From the code of Factory we can see that a new object is created and assigned to t. This reference is returned and goes out of scope, but as it is assigned to anotherObj, anotherObj references the same instance. On each loop iteration, this variable goes out of scope (as it is declared insider loop); the referenced instance looses all references and is scheduled for removal. In each iteration of the loop a brand new instance is created, is shown and is scheduled for removal.

2) More exactly, GC performs destruction of object and reclaiming the memory according to its own strategy. You cannot assume any particular moment of type when it happens (and any code based on such assumption is incorrect). This behavior can be observed by creation a destructor and logging its calls. Also, this behavior can be modified by using System.GC.Collect, see http://msdn.microsoft.com/en-us/library/system.gc.aspx[^]. It is not recommended to do so unless you have a really Cunning Plan :-).

Notes:

Due to this GC behavior, the activity of writing of destructors makes little sense. With .NET, they are used pretty rarely.

Loosing all references to the object is not a trivial condition. Consider object A holds a reference to object B, object B holds a reference to object C, and object C holds a reference to object A. Consider you loose all other references to any of these objects except those circular references. Will these three object destroyed? Don''t worry, the will, despite the fact they seemingly protect each other from destruction. They will be destroyed because there is no a way to access them from any actual code which could possibly be run later. So, GC mechanism is not trivial at all.

—SA


这篇关于垃圾收集相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆