本文介绍了为什么在我的情况下.NET不重用/压缩免费对象(内存泄漏问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Server 2008上运行的.NET 3.5多线程应用程序存在内存泄漏问题.运行几天后,该进程消耗了1 GB内存,并且仍在增加.这就是我试图解决的问题:

I have a memory leak problem with my .NET 3.5 multi-threaded application run on Window server 2008. I found the process consumed 1 GB memory after few days running and still increasing. This is what I've tried to address the issue :


-Gen2堆大小随时间不断增加,而Gen0& Gen1保持不变.

- The Gen2 Heap Size is increasing non-stop over time while Gen0&Gen1 stay remain.

-大对象堆大小保持稳定在大约1 MB,这是我为应用程序的生命周期分配的一些对象(这些对象是一些具有指定容量的字典)

- The large object heap size stay stable in about 1 MB, which are some objects that I have allocated for life-time of the application (those objects are a few dictionary with specified capacity)

-!EEHeap显示该应用程序在Gen2中分配了越来越多的内存段:

- !EEHeap shown that the app allocate more and more memory segment in Gen2 :

Loader的总堆大小:0x79000(495616)bytes
=====================================
GC堆数:1
第0代始于0x1a3bddd0
第1代始于0x1a375490
第2代开始于 0x025a1000
临时段分配上下文:无
&segment   开始分配尺寸
025a0000 025a1000  0359f9bc 0x00ffe9bc(16771516)
0bd80000 0bd81000  0c87a5b0 0x00af95b0(11507120)
0dba0000 0dba1000  0e56a2f0 0x009c92f0(10261232)
0f190000 0f191000  0fc9cfd0 0x00b0bfd0(11583440)
10190000 10191000  10998720 0x00807720(8419104)
11190000 11191000  11c380d0 0x00aa70d0(11170000)
12190000 12191000  1295bbf4 0x007cabf4(8170484)
13190000 13191000  13d0e578 0x00b7d578(12047736)
15190000 15191000  158d9534 0x00748534(7636276)
16190000 16191000  16d04c64 0x00b73c64(12008548)
14190000 14191000  14d46794 0x00bb5794(12277652)
17190000 17191000  17d06198 0x00b75198(12013976)
18d60000 18d61000  19942f84 0x00be1f84(12459908)
19d60000 19d61000  1a7dd73c 0x00a7c73c(10995516)
大对象堆从0x035a1000开始
&segment   开始分配尺寸
035a0000 035a1000  036c1aa8 0x00120aa8(1182376)
总大小0x97297b4(158504884)
------------------------------
GC堆大小0x97297b4(158504884)

Total LoaderHeap size: 0x79000(495616)bytes
=======================================
Number of GC Heaps: 1
generation 0 starts at 0x1a3bddd0
generation 1 starts at 0x1a375490
generation 2 starts at 0x025a1000
ephemeral segment allocation context: none
 segment    begin allocated     size
025a0000 025a1000  0359f9bc 0x00ffe9bc(16771516)
0bd80000 0bd81000  0c87a5b0 0x00af95b0(11507120)
0dba0000 0dba1000  0e56a2f0 0x009c92f0(10261232)
0f190000 0f191000  0fc9cfd0 0x00b0bfd0(11583440)
10190000 10191000  10998720 0x00807720(8419104)
11190000 11191000  11c380d0 0x00aa70d0(11170000)
12190000 12191000  1295bbf4 0x007cabf4(8170484)
13190000 13191000  13d0e578 0x00b7d578(12047736)
15190000 15191000  158d9534 0x00748534(7636276)
16190000 16191000  16d04c64 0x00b73c64(12008548)
14190000 14191000  14d46794 0x00bb5794(12277652)
17190000 17191000  17d06198 0x00b75198(12013976)
18d60000 18d61000  19942f84 0x00be1f84(12459908)
19d60000 19d61000  1a7dd73c 0x00a7c73c(10995516)
Large object heap starts at 0x035a1000
 segment    begin allocated     size
035a0000 035a1000  036c1aa8 0x00120aa8(1182376)
Total Size  0x97297b4(158504884)
------------------------------
GC Heap Size  0x97297b4(158504884)

-!dumpheap -stat显示了许多Free对象随着时间的推移而增加,但是应用程序不知何故不会重用这些空闲内存,而是随着时间的推移分配更多的内存:

- !dumpheap -stat shown that a lot of Free object is increasing over time but the application somehow does not reuse these free memory and allocate more memory overtime:

7207da1c    6825      682500 System.Net.Sockets.OverlappedAsyncResult
70ec88c0    92655     4471548 System.String
70ecb330   12959    10870420 System.Byte []
004aefc0    17221   133162956    免费

7207da1c     6825       682500 System.Net.Sockets.OverlappedAsyncResult
70ec88c0    92655      4471548 System.String
70ecb330    12959     10870420 System.Byte[]
004aefc0    17221    133162956      Free

-我试图做一个GC.Collect()来强制所有世代立即进行垃圾收集,但是这些巨大的Free对象仍然留在那里.

- I have tried to do a GC.Collect() to force an immediate garbage collection of all generation but these huge Free object still stay there.

-使用dumpheap -Type Free查看这些空闲对象.这些自由对象的大小各不相同,但我发现有很多连续的大大小的东西可以重复使用:

- Look at these free object with dumpheap -Type Free . The size of these free object is various but i found a lot of contiguous large size which can be reusable :

11af63f4 004aefc0   179468免费
11b221f4 004aefc0   57212免费
11b30284 004aefc0    55508免费
11b3e030 004aefc0   40144免费
11b47e14 004aefc0    33512免费
11b505d0 004aefc0   260464免费
11b9001c 004aefc0    67484免费
11ba0bf8 004aefc0   619492免费
12191000 004aefc0   598516免费
122232f8 004aefc0   166308免费
1224c0dc 004aefc0   172144免费
12276240 004aefc0   288408免费
122bcd18 004aefc0   212100免费
122f0a78 004aefc0   84348免费
12305834 004aefc0    70120免费
12316ef0 004aefc0   112980免费
12332c84 004aefc0   413500免费
12398044 004aefc0   192380免费
123c70ac 004aefc0    62256免费
123d64f0 004aefc0    62172免费
123e58e0 004aefc0   109028免费
12400798 004aefc0    79084免费
12414108 004aefc0   100440免费

11af63f4 004aefc0   179468 Free
11b221f4 004aefc0    57212 Free
11b30284 004aefc0    55508 Free
11b3e030 004aefc0    40144 Free
11b47e14 004aefc0    33512 Free
11b505d0 004aefc0   260464 Free
11b9001c 004aefc0    67484 Free
11ba0bf8 004aefc0   619492 Free
12191000 004aefc0   598516 Free
122232f8 004aefc0   166308 Free
1224c0dc 004aefc0   172144 Free
12276240 004aefc0   288408 Free
122bcd18 004aefc0   212100 Free
122f0a78 004aefc0    84348 Free
12305834 004aefc0    70120 Free
12316ef0 004aefc0   112980 Free
12332c84 004aefc0   413500 Free
12398044 004aefc0   192380 Free
123c70ac 004aefc0    62256 Free
123d64f0 004aefc0    62172 Free
123e58e0 004aefc0   109028 Free
12400798 004aefc0    79084 Free
12414108 004aefc0   100440 Free

...

-我可能认为软件需要分配一些较大的对象,这些对象不适合那些可用内存.所以我做了一个!dumpheap -Type String/System.Byte [] -min 50000来找到大的分配对象,但是我没有找到.

- As I might think the software need to allocate some larger object that does not fit those free memory. So i do a !dumpheap -Type String/System.Byte[] -min 50000 to find large allocated object but i found no one.

.NET为什么不压缩/重复使用这些Free对象,以及如何解决此内存泄漏问题?非常感谢大家!

Why does .NET not compact/reuse these Free object and how to fix this memory leak issue? Thank you all a lot!


推荐答案

.NET中可能会发生内存泄漏.使用时的框架应用程序非托管代码,这是该代码的一部分应用.此非托管代码可以泄漏内存和.NET Framework运行时无法解决该问题.

此外,一个项目可能只能 似乎有内存泄漏.这个如果许多大的情况可能会发生对象(例如DataTable对象)被声明,然后添加到 集合(例如DataSet).的资源 这些对象拥有的可能永远不会被释放,并且资源整个过程都还活着该程序.这似乎是 泄漏,但实际上只是一个记忆方式的症状被分配在 程序.

Additionally, a project may only  appear to have a memory leak. This  condition can occur if many large  objects (such as DataTable objects)  are declared and then added to a  collection (such as a DataSet). The  resources that these objects own may  never be released, and the resources  are left alive for the whole run of  the program. This appears to be a  leak, but actually it is just a  symptom of the way that memory is  being allocated in the program.

有关更多详细信息,您可以参考来自MS com.

For more details, you can refer to https://support.microsoft.com/en-us/help/318263/how-to-identify-memory-leaks-in-the-common-language-runtime from MS com.

用于处理此类问题.记住要全部调用Dispose()IDisposable对象. 使用using 语句.

For dealing with this type of issue,. Remember to call Dispose() on allIDisposable objects. Use the using statement.

最诚挚的问候,

克里斯汀


这篇关于为什么在我的情况下.NET不重用/压缩免费对象(内存泄漏问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:41