如何释放程序存储器 [英] How to release program memory
问题描述
嗨古鲁兹,
我正在研究一个非常简单的应用程序,该应用程序在数据表中填充了4000000条记录. 使用for循环将这些记录进一步切片成一个哈希表.当我的哈希表达到400000时,我将消耗该哈希表并清除它
I am working on a very simple application which populate 4000000 Records in data table. These records are further sliced into a hash table one by one using a for loop. When my hash table reaches up to 400000 , i consume that hash table and clear it
hshObject.Clear()
hshObject =没什么
hshObject.Clear()
hshObject = Nothing
平行
parallel when i populate data from data table to hash table , i removed copied row from data table. At my progam end i also make
dt.Clear()
dt.Dispose()
dt.Clear()
dt.Dispose()
我的程序启动时是 20,200 Mem Usage. 程序执行此工作时,它的内存使用量达到 157308 并保持不变.应该向后或至少 当我的程序完成所有任务时,状态就会下降.
My program startup with 20,200 Mem Usage. When program perform this job , its memory usage reaches up to 157308 and remain on same figure. It should be reach back or at least goes down when my program complete all tasks.
我想使用GC.Collect(),调用gc Collect可以吗?那是最佳实践吗?
I want to use GC.Collect(), does it fine to call gc Collect ? is that best practice ?
请帮助.
注意
Ali
阿里·穆罕默德(Ali Muhammad)
Ali Muhammad
推荐答案
不,调用GC强制收集不是一个好习惯.问题在于,GC很可能会使情况变得更糟.根本问题是您正在尝试处理DataTable中的大量数据. DataTable并非旨在存储大型数据 数据量.您最好创建简单的业务对象,使用DataReader将数据从源读取到您的自定义对象中,然后直接使用它们.这减少了DataTable的开销,并且可能消除了对 一个哈希表,因为您可能只使用LINQ.清除内存仍然需要您清空列表.
No, calling GC to force a collection isn't considered good practice. The issue is that most likely GC will make things worse. The fundamental problem is that you're trying to process a lot of data in a DataTable. DataTable weren't designed to store large quantities of data. You'd do better to create simple business objects, use a DataReader to read the data from the source into your custom objects and then work with them directly. This reduces the overhead of DataTable and probably eliminates the need for a hash table altogether as you could probably just use LINQ. Clearing the memory still requires that you empty out your list.
还请注意,只要您低于.NET使用的内存阈值,内存就会增加.一旦分配了内存,.NET将在一段时间内不再释放它,以防您需要更多的内存.因此,使用157 MB(似乎有点低)并不大 一笔交易,因为它会回落.还应注意,工作集和虚拟存储器是两件不同的事情,因此它取决于哪个存储器".你在看.
Also note that memory will go up as long as you are below the memory threshold that .NET uses. Once you have allocated memory .NET will not release it for a while in case you need more memory. So using up 157 MB (which seems a little low) isn't that big of a deal as it'll go back down. Also be aware that working set and virtual memory are 2 different things so it depends upon which "memory" you were looking at.
迈克尔·泰勒
http://www.michaeltaylorp3.net
Michael Taylor
http://www.michaeltaylorp3.net
这篇关于如何释放程序存储器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!