且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

托管内存泄漏

更新时间:2023-12-06 14:04:40

就像这里有一个要点-处理完表单后,对象可能会保留在内存中很长时间.这是因为.NET使用了一个称为垃圾回收过程的过程,该过程将定期出现,并在内存中找到所有不可引用的对象并回收内存.

So if you objects are no longer referencable in you code then eventually they will be recovered.

因此,如果您的对象在代码中不再可引用,那么最终它们将被恢复.

When does this garbage collection process happen ?   This is a interesting point in that it happens when the system is short on resources and needs to try and find some more - so it will run and clean up memory, It would also occur periodically although not really at pre-determined time periods.

此垃圾收集过程何时发生?这是一个有趣的观点,它发生在系统资源短缺并且需要尝试查找更多资源的时候-这样它将运行并清理内存.尽管实际上不是在预定的时间段内,它也会定期发生. /p>

那为什么这样好呢,因为在使用引用计数的COM下,如果发生崩溃并且内存不再具有引用或具有某种形式的循环引用,这可能会导致内存泄漏,这意味着内存将被永久持有.垃圾收集过程不使用引用计数,而是在搜索不再可引用的对象并将回收内存.

So why is this good, because under COM which used reference counting you could get memory leaks if something crashed and the memory not longer had a reference or you had some form of cyclic reference which would mean the memory would be formever held.    The garbage collection process does not use reference counting and is searching for objects that are no longer referencable and will reclaim the memory.

所以您的内存泄漏可能只是尚未被收集.我会猜测,这就是为什么它难以复制的危险.如果您有大量的资源(内存),那么垃圾回收过程的运行频率可能会降低很多,因为它的运行频率没有那么短.如果您有很多正在运行的进程-资源变得越来越重要,并且该进程将更频繁地运行.

So you memory leak is probably just that its not yet been collected.    I would hazard the guess thats why its difficult to reproduce.   If you've got loads of resources (memory) then the garbage collection process will probably run a lot less frequently as it doesnt run short as often.    If you have lots of processes running - resources become more important and the process will run more frequently.

有大量参考资料,只需在垃圾回收+ .NET上进行网络搜索

There is loads of reference material, just do a web search on garbage collections + .NET