C# - 垃圾收集 [英] C# - Garbage Collection

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

问题描述

好了,所以我了解栈和堆(价值住在堆栈上堆参考资料)。



当我声明一个类的新实例这个住在堆,栈上的内存的引用了这一点。 (也就是说,它决定当一个实例化类不再使用和回收内存)。我也知道C#做它自己的垃圾回收



我有2个问题:




  1. 是我的垃圾收集的理解是否正确?

  2. 我可以做我自己?如果是的话是没有任何实际的好处这样做我自己或我应该离开它。



我问,因为我有一个方法For循环。每次我经过一个循环,我创建类的新实例。在我的脑子里,我想象所有这些类在堆躺在身边,没有做任何事情,但占用的内存,我希望得到尽可能迅速地摆脱他们,我可以让事情变得整洁!



我是正确认识这还是我失去了一些东西?


解决方案

好了,所以我了解的栈和堆(价值住在堆栈上堆


引用

我不认为你了解有关栈和堆。如果数值住在堆栈上再哪里整数数组活?整数是值的。你告诉我,一个整数数组保存在栈中的整数?当您返回一个整数数组从它的方法,比方说,用一万整数,你告诉我,那些10000整数被复制到堆栈?



价值观住在栈上时,他们住在堆栈中,并在堆中生活时,他们住在堆上。该键入的的事情与做的想法的其存储的寿命的完全是无稽之谈。那些的短暂的进入堆叠的存储位置;那些的长寿命的去堆,那就是独立的类型的存储位置。长寿命INT已去堆,同一个类的长寿命的实例。




当我宣布一个新的一个类的实例,这个住在堆,栈上的内存的引用了这一点。




为什么引用必须去在堆栈上?同样,引用的存储生命周期无关,其类型的。如果参考的存储是长寿命则参考那张堆。




我也知道C#做它自己的垃圾回收(即,它决定当一个实例化类不再使用和回收内存)。




C#语言没有这样做;在CLR这样做。




是我的垃圾收集的理解是否正确?




您似乎相信了很多关于栈和堆谎言,所以赔率是好的,不,不是这样的。




我可以做我自己?




在C#中,没有。




我问,因为我有一个方法for循环。每次我经过一个循环,我创建类的新实例。在我的脑子里,我想象所有这些类在堆躺在身边,没有做任何事情,但占用的内存,我希望得到尽可能迅速地摆脱他们,我可以让事情变得整洁!




垃圾收集整点是可以不用担心整理中解脱出来。这就是为什么它被称为自动垃圾收集。它收拾你。



如果你担心你的循环正在创建的集气管压力的,并且你希望避免出于性能的考虑集气管压力则我建议你​​追求的汇聚的策略。这将是明智的开始用的明确的统筹战略;那就是:

 同时(任何)
{
FROB F = FrobPool.FetchFromPool();
f.Blah();
FrobPool.ReturnToPool(F);
}



而不是试图用复活终结做自动池。我建议对阵双方终结和对象的全面复活,除非你是最后确定的语义方面的专家。



当然池分配一个新FROB如果没有一个在泳池。如果有一个池,然后将它交给它,直到它在放回池中删除。(如果你忘了把一个FR​​OB放回池中,GC将得到它最终)通过追求你造成GC汇集战略,所有的Frobs最终移动到2代堆,而不是在0代堆上创建大量收集的压力。然后收集的压力消失,因为没有新的Frobs分配。如果别的东西是生产采集的压力,Frobs都安全地在它们很少参观了第2代堆。



这当然是战略完全相反的你描述;统筹战略的要点是的事业对象流连永远的。对象游逛永远是一个的的事情,如果你打算使用它们。



当然,你不通过,你有一个性能问题,由于集气管压力分析才知道做出这些种类的变化!这是罕见的对桌面CLR这样的问题;这是较为常见的紧凑CLR。



更​​一般地,如果你是什么样的人谁觉得不舒服有一个内存管理器清理对你的日程安排,那么C#是不是适合你的语言。考虑℃,而不是。


Ok so I understand about the stack and the heap (values live on the Stack, references on the Heap).

When I declare a new instance of a Class, this lives on the heap, with a reference to this point in memory on the stack. I also know that C# does it's own Garbage Collection (ie. It determines when an instanciated class is no longer in use and reclaims the memory).

I have 2 questions:

  1. Is my understanding of Garbage Collection correct?
  2. Can I do my own? If so is there any real benefit to doing this myself or should I just leave it.

I ask because I have a method in a For loop. Every time I go through a loop, I create a new instance of my Class. In my head I visualise all of these classes lying around in a heap, not doing anything but taking up memory and I want to get rid of them as quickly as I can to keep things neat and tidy!

Am I understanding this correctly or am I missing something?

解决方案

Ok so I understand about the stack and the heap (values live on the Stack, references on the Heap

I don't think you understand about the stack and the heap. If values live on the stack then where does an array of integers live? Integers are values. Are you telling me that an array of integers keeps its integers on the stack? When you return an array of integers from a method, say, with ten thousand integers in it, are you telling me that those ten thousand integers are copied onto the stack?

Values live on the stack when they live on the stack, and live on the heap when they live on the heap. The idea that the type of a thing has to do with the lifetime of its storage is nonsense. Storage locations that are short lived go on the stack; storage locations that are long lived go on the heap, and that is independent of their type. A long-lived int has to go on the heap, same as a long-lived instance of a class.

When I declare a new instance of a Class, this lives on the heap, with a reference to this point in memory on the stack.

Why does the reference have to go on the stack? Again, the lifetime of the storage of the reference has nothing to do with its type. If the storage of the reference is long-lived then the reference goes on the heap.

I also know that C# does it's own Garbage Collection (ie. It determines when an instanciated class is no longer in use and reclaims the memory).

The C# language does not do so; the CLR does so.

Is my understanding of Garbage Collection correct?

You seem to believe a lot of lies about the stack and the heap, so odds are good no, it's not.

Can I do my own?

Not in C#, no.

I ask because I have a method in a For loop. Every time I go through a loop, I create a new instance of my Class. In my head I visualise all of these classes lying around in a heap, not doing anything but taking up memory and I want to get rid of them as quickly as I can to keep things neat and tidy!

The whole point of garbage collection is to free you from worrying about tidying up. That's why its called "automatic garbage collection". It tidies for you.

If you are worried that your loops are creating collection pressure, and you wish to avoid collection pressure for performance reasons then I advise that you pursue a pooling strategy. It would be wise to start with an explicit pooling strategy; that is:

while(whatever)
{
    Frob f = FrobPool.FetchFromPool();
    f.Blah();
    FrobPool.ReturnToPool(f);
}

rather than attempting to do automatic pooling using a resurrecting finalizer. I advise against both finalizers and object resurrection in general unless you are an expert on finalization semantics.

The pool of course allocates a new Frob if there is not one in the pool. If there is one in the pool, then it hands it out and removes it from the pool until it is put back in. (If you forget to put a Frob back in the pool, the GC will get to it eventually.) By pursuing a pooling strategy you cause the GC to eventually move all the Frobs to the generation 2 heap, instead of creating lots of collection pressure in the generation 0 heap. The collection pressure then disappears because no new Frobs are allocated. If something else is producing collection pressure, the Frobs are all safely in the gen 2 heap where they are rarely visited.

This of course is the exact opposite of the strategy you described; the whole point of the pooling strategy is to cause objects to hang around forever. Objects hanging around forever is a good thing if you're going to use them.

Of course, do not make these sorts of changes before you know via profiling that you have a performance problem due to collection pressure! It is rare to have such a problem on the desktop CLR; it is rather more common on the compact CLR.

More generally, if you are the kind of person who feels uncomfortable having a memory manager clean up for you on its schedule, then C# is not the right language for you. Consider C instead.

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

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