简体   繁体   English

高效的垃圾收集

[英]Efficient Garbage Collection

Consider the following object 考虑以下对象

class someObject;
{
   public objectA A{get;set;}
   public objectB B{get;set;}
   public objectC C{get;set;}
   public objectD D{get;set;}
   //...
}

at some point ObjectD is not needed anymore but the Instance of someObject still exist and will do so for couple hours, objectD takes up alot of memory so after its role is done i did set it up to null. 在某些时候,不再需要ObjectD ,但someObject的实例仍然存在,并且将持续几个小时, objectD占用了大量内存,因此在完成其角色之后,我确实将其设置为null。

  • does that mean the memory will get freed from ObjectD as soon as i set it to null? 这是否意味着只要我将其设置为null,内存就会从ObjectD释放出来?
  • if an object sets for so long unused in some scope what does .NET do about it? 如果一个对象在某个范围内设置了很长时间,那么.NET会怎么做呢?

does that mean the memory will get freed from ObjectD as soon as I set it to null? 这是否意味着一旦我将其设置为null,内存将从ObjectD中释放出来?

It's not object sets to null - its reference to object in memory sets to null . 它不是对象集为null - 它对内存中对象的引用设置为null And object will exist until Garbage Collector will collect it (if you don't have any other references to this object). 并且对象将存在,直到垃圾收集器将收集它(如果您没有对此对象的任何其他引用)。

Take a look on Fundamentals of Garbage Collection 看看垃圾收集的基本原理

The GC will collect items that are not referenced anymore. GC将收集不再引用的项目。 When it does that is up to itself, though. 但是,当它确实如此时,这取决于它自己。

So: Yes, setting the reference to ObjectD to null will enable the GC to collect it. 所以:是的,将ObjectD的引用设置为null将使GC能够收集它。 No, it will (most likely) not happen immediately. 不,它(很可能)不会立即发生。

You can set object as null followed by a Collect method Gc.Collect() . 您可以将object设置为null,然后是Collect方法Gc.Collect() It performs a blocking garbage collection of all generations. 它执行所有代的阻塞垃圾收集。 All objects, regardless of how long they have been in memory, are considered for collection. 所有对象,无论它们在内存中存在多长时间,都被考虑收集。

If you set D to null, it can be deleted from memory when GC will be called. 如果将D设置为null,则可以在调用GC时从内存中删除它。 But it is not a good solution to have null fields in your object. 但是在对象中使用空字段不是一个好的解决方案。 If it needed temporary, maybe it should be a local variable!? 如果它需要临时的,也许它应该是一个局部变量!?

if an object sets for so long unused in some scope what does .NET do about it?

To reclaim memory the garbage collector collects and destroys objects which are no longer available which is when there are either no references to it, all the references are set to null, or else all references to it are from other objects that can be collected. 为了回收内存,垃圾收集器收集并销毁不再可用的对象,当没有对它的引用时,所有引用都设置为null,否则对它的所有引用都来自可以收集的其他对象。 The process of a collection involves moving available objects into the memory and reclaiming the memory used by objects which are no longer used. 集合的过程涉及将可用对象移动到存储器中并回收不再使用的对象所使用的存储器。 An object Which survives a collection is automatically promoted to the next generation 存在于集合中的对象会自动升级到下一代

  1. The memory will NOT be freed immediately by setting it to NULL. 通过将内存设置为NULL,不会立即释放内存。 The memory will be marked for garbage collection. 内存将被标记为垃圾回收。 The .Net GC scheme is somewhat efficient. .Net GC方案有点高效。 If you are concerned, you can force aa GC cycle: See this stack overflow article on the yin/yang of this. 如果您担心,可以强制执行GC循环: 请参阅此yin / yang上的此堆栈溢出文章。

  2. If it sets unused, .Net will not do anything. 如果它未使用,.Net将不会做任何事情。 That is to say, if you don't specifically "nullify" the reference, .Net has no reason to expect the object won't be needed (immediately) at some point. 也就是说,如果你没有特别“废除”引用,.Net没有理由期望在某个时刻(立即)不需要该对象。 If you need it and .Net "temporarily removed it from memory", it will need to recreate it somehow, using information about the state of your class...the obvious choice for the restoration information is the class itself. 如果你需要它并且.Net“暂时从内存中删除它”,它将需要以某种方式重新创建它,使用有关类的状态的信息......恢复信息的明显选择是类本身。 So removing it is not really an option. 所以删除它不是一个真正的选择。

Offloading it through various stages of slower-but-larger memory access is usually the strategy used in modern computers: (1) Store memory in local on chip memory cache(s) for most recent data/program used. 通过较慢但更大的存储器访问的各个阶段卸载它通常是现代计算机中使用的策略:(1)将存储器存储在本地片上存储器高速缓存中以用于最近使用的数据/程序。 (2) Store memory in off chip memory chips. (2)将存储器存储在片外存储器芯片中。 (3) Store memory in virtualized media (eg hard drive). (3)将内存存储在虚拟化介质(例如硬盘驱动器)中。

At the end of the day, you probably know best when to nullify the reference and the framework knows best about when/where to store the memory for your objects. 在一天结束时,您可能最清楚何时使引用无效,并且框架最了解何时/何处存储对象的内存。 There are exceptions, such as game programming, where the GC mechanism can cause hiccups. 有一些例外,例如游戏编程,GC机制可能会导致打嗝。 But in general, the system does well as long as you do. 但总的来说,只要你这样做,系统就能做得很好。

Was this helpful? 这个有帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM