方法结束后,内存将保持分配状态 [英] Memory keeps allocated after method ends

查看:54
本文介绍了方法结束后,内存将保持分配状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#和.NET Framework 4.6.2和Entity Framework 6.1.3开发Windows服务.

我在该服务上有一个Web API接口,并以一种Controller的方法创建了一个字典,该字典包含一百万个以上的字符串:

I'm developing a Windows Service with C# and .NET Framework 4.6.2 and Entity Framework 6.1.3.

I have a Web API interface on that service and in one Controller's method I create a Dictionary with more than one million strings:   

Dictionary<string, List<string>> codes;

该词典是在另一种方法中创建的:

This Dictionary is created inside another method:   

codes = GetCodesAsDictionary(connectionString, productionId);

我将此变量从静态类传递给静态方法.

一个Visual Studio 2015的内存分析器,当我调用此方法时,我看到内存如何增长到1,1Gb,但是当该方法结束时,内存保持为1,1Gb.

如果再次调用该方法,内存将再次增长到2Gb,并且在方法完成后,内存将再次达到1,1Gb.

我试图通过`codes`变量作为参考,但是我得到了相同的内存分配.我必须停止该服务,然后再次运行以释放1,1Gb.顺便说一句,我通过引用传递了它,因为我不希望静态方法复制 它.

您知道为什么方法结束后它不释放这么多的内存吗?

更新:

我已经用实例类更改了静态类(从静态类复制方法),并且得到了相同的结果(分配了1,1Gb内存).

第二次更新:

我还更改了字典的创建方式:

I pass this variable to a static method from a static class.

One Visual Studio 2015's memory profiler I see how memory grows to 1,1Gb when I call this method, but when the method ends, the memory keep as 1,1Gb.

If I call the method again, memory grows again to 2Gb, and after method finishes, memory goes to 1,1Gb again.

I have tried to pass the `codes` variable as reference, but I get the same memory allocation. I have to stop the service and re-run it again to release that 1,1Gb. By the way, I pass it by reference because I don't want that the static method makes a copy of it.

Do you know why it doesn't release such amount of memory after method ends?

UPDATE:

I have changed the static class with an instance class (copying the method from static class) and I get the same result (1,1Gb memory allocated).

SECOND UPDATE:

I have also changed how the dictionary is created:

Dictionary<string, List<string>> codes = null;

GetCodesAsDictionary(connectionString,productionId,out codes);

GetCodesAsDictionary(connectionString, productionId, out codes);

而且我得到相同的结果:分配了1,1Gb内存.

And I get the same result: 1,1Gb memory allocated.

推荐答案

在我看来这并不奇怪.当您分配大量内存时,托管堆将会增长. GC运行后,堆可能不会缩小.假设您将再次需要那么多的内存,则它会保持空白.  

That does not sound unusual to me.  When you allocate a lot of memory, the managed heap will grow.  After GC runs the heap may not shrink.  It stays large and empty on the assumption that you will need that much memory again.  

请参见

"为保留内存,垃圾收集器将调用Win32 VirtualAlloc 函数,并一次为托管应用程序保留一块内存.垃圾收集器还会根据需要保留段,并将段释放回操作系统(清除它们后) 调用Win32 VirtualFree 函数."

"To reserve memory, the garbage collector calls the Win32 VirtualAlloc function, and reserves one segment of memory at a time for managed applications. The garbage collector also reserves segments as needed, and releases segments back to the operating system (after clearing them of any objects) by calling the Win32 VirtualFree function."

垃圾收集基础

David


这篇关于方法结束后,内存将保持分配状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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