简体   繁体   English

如何在C#应用程序(DataTable dispose)中保持内存清晰?

[英]How keep the memory clear in the C# applications (DataTable dispose)?

What's the best way to dispose a DataTable and clear all RAM memory related in the C# application? 处理DataTable并清除C#应用程序中相关的所有RAM内存的最佳方法是什么?

I have a simple code: 我有一个简单的代码:

private void Form1_Load(object sender, EventArgs e)
{
    using (DataTable vDT = new DataTable())
    using (DataTable vDTsec = new DataTable())
    {
        vDT.Columns.Add("A");
        vDT.Columns.Add("B");
        vDT.Columns.Add("B_1");

        vDTsec.Columns.Add("A");
        vDTsec.Columns.Add("B");
        vDTsec.Columns.Add("E");

        for (int x = 1; x <= 1000000; x++)
        {
            vDT.Rows.Add(new object[] { x, "B" + x.ToString(), "C" + x.ToString() });
            vDTsec.Rows.Add(new object[] { x, "B" + x.ToString(), "E" + x.ToString() });
        }

        vDT.Dispose();
        vDTsec.Dispose();
    }

    GC.Collect();
    GC.WaitForFullGCApproach(100);
    GC.WaitForPendingFinalizers();
}

If I put a breakpoint before to create the million of rows the application has a size of ~4MB: 如果我之前设置一个断点来创建数百万行,那么应用程序的大小约为4MB: 1AA

After the creation of all rows, the memory has a huge increase (~428MB): 在创建所有行之后,内存有了大幅增加(~428MB): 2AA

Even after to dispose the both tables, the memory continues in ~428MB: 即使在处理了两个表之后,内存仍在~428MB: 3AA

How I can free this memory from the program? 我如何从程序中释放这些记忆?

First, you shouldn't necessarily be concerned about that number you see in Task Manager. 首先,您不必担心在任务管理器中看到的数字。 All that means is that the Garbage Collector hasn't collected the memory yet (or the OS is caching the memory), and if the OS doesn't need it, the GC may wait awhile to collect it. 所有这些意味着垃圾收集器尚未收集内存(或操作系统正在缓存内存),如果操作系统不需要它,GC可能会等待一段时间来收集它。

The only compelling reason to better control the memory usage (beyond sensible object management) in your application is to improve your program's performance characteristics. 在应用程序中更好地控制内存使用(超出合理对象管理)的唯一令人信服的理由是改善程序的性能特征。 The way you do that is by using Object Pooling , which basically means that, instead of destroying and recreating objects, you create them once, and then reuse them, so the GC never collects. 你这样做的方法是使用对象池 ,这基本上意味着,不是破坏和重新创建对象,而是创建它们一次,然后重复使用它们,这样GC就不会收集。 But only use this if you've exhausted all other techniques for improving performance. 但是,如果您已经用尽所有其他技术来提高性能,那么只能使用它。

You should not normally invoke garbage collection yourself. 您通常不应该自己调用垃圾收集。 Call Dispose() and let GC do it's thing. 调用Dispose()并让GC做它的事情。 GC.Collect() tries to collect all generations. GC.Collect()尝试收集所有代。 GC is or can be expensive. GC是或可能很昂贵。 Most of the time it handles itself well and we do not need to get directly involved. 大部分时间它处理得很好,我们不需要直接参与。

Make sure unmanaged objects are explicitly closed and/or disposed of (like DB connections). 确保显式关闭和/或处理非托管对象(如DB连接)。 If you are loading that much data into a datatable in memory that you feel the need to force a collection, you might want rethink your approach. 如果要将大量数据加载到内存中的数据表中,您认为需要强制收集,则可能需要重新考虑您的方法。 I'd be interested in understanding what you are doing that would require a GC. 我有兴趣了解你在做什么需要GC。

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

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