简体   繁体   English

C#GC.Collect()和内存

[英]C# GC.Collect() and Memory

I am receiving a very large list as a method argument and would like to remove it from memory after it is used. 我收到一个很大的列表作为方法参数,并希望在使用后将其从内存中删除。 Normally I would let the GC do its thing, but I need to be very careful of memory usage in this app. 通常,我会让GC做它的事情,但是我需要非常小心此应用程序中的内存使用情况。

Will this code accomplish my goal? 这段代码能实现我的目标吗? I've read a lot of differing opinions and am confused. 我读了很多不同的意见并且感到困惑。

public void Save(IList<Employee> employees)
    {
        // I've mapped the passed-in list
        var data = Mapper<Employee, EmployeeDTO>.MapList(employees);

        // ?????????????
        employees = null;
        GC.Collect();

        // Continues to process very long running methods....
        // I don't want this large list to stay in memory

   }

Maybe I should use another technique that I'm not aware of? 也许我应该使用另一种我不知道的技术?

If the list is not used anymore the GC will automatically collect it when available memory is an issue . 如果不再使用该列表,则当可用内存出现问题时 ,GC会自动收集该列表。

However, if the caller uses the list after passing it to your function then the GC won't collect it even if you set it to null (all you have is a reference to the list - you can't do anything about other objects that hold references as wel). 但是,如果调用者在将列表传递给函数后使用了列表,则即使其设置为null,GC 也不会收集它(您所拥有的只是对列表的引用-您无法对其他对象执行任何操作保留引用为wel)。

Unless you have a measurable problem don't try and outsmart the GC. 除非您有可衡量的问题,否则请勿尝试使GC失灵。

This is not a direct answer to the question but some ideas how to handle the low-memory situations the poster (@Big Daddy) referred to. 这不是问题的直接答案,而是一些如何处理海报(@Big Daddy)所提到的低内存情况的想法。

If you're running into low memory situations on an x64 platform with 8 GB of memory, you should determine if your application is responsible for it. 如果在内存为8 GB的x64平台上遇到内存不足的情况,则应确定应用程序是否对此负责。 If it is, then run a memory profiler (CLR Profiler or something else or even get a full user dump and run WinDbg on it) to see what allocates the memory. 如果是这样,则运行内存分析器(CLR Profiler或其他工具,或者甚至获得完整的用户转储并在其上运行WinDbg)以查看分配内存的内容。 It's possible that you have some objects that are not used anymore but are still referenced somewhere - this is not a true memory leak but it's memory that's not freed up in your application - most decent profilers will identify big objects (or objects with a lot of instances) along with their types. 您可能有一些不再使用但仍在某处引用的对象-这不是真正的内存泄漏,但是您的应用程序中未释放的内存-大部分不错的探查器将识别大对象(或具有很多对象的对象)实例)及其类型。

I find it hard to believe that the list passed to this Save function would stress a server with 8 GB of memory but we don't know how much free memory is available to the process and what kind of a process it is (IIS, desktop, etc.) 我发现很难相信传递给此Save函数的列表会给具有8 GB内存的服务器带来压力,但是我们不知道该进程有多少可用内存以及该进程是哪种类型(IIS,桌面等)

If Save is called on several threads with huge inputs, it can potentially lead to memory stress but even then, it's not very likely and I'd check various counters and profile data to see when and why memory stress happens. 如果在具有大量输入的多个线程上调用Save ,则可能会导致内存紧张,但是即使那样,它也不太可能发生,我将检查各种计数器和配置文件数据以查看何时以及为什么发生内存紧张。

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

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