<分区>
我用C#写了一个长TCP连接套接字服务器。我的服务器内存中出现峰值。我使用 dotNet Memory Profiler(一种工具)来检测内存泄漏的位置。 Memory Profiler 显示private heap 很大,内存如下图(数字不是真实的,我想展示的是GC0 和GC2 的Holes 非常非常大,数据大小正常):
Managed heaps - 1,500,000KB
Normal heap - 1400,000KB
Generation #0 - 600,000KB
Data - 100,000KB
"Holes" - 500,000KB
Generation #1 - xxKB
Data - 0KB
"Holes" - xKB
Generation #2 - xxxxxxxxxxxxxKB
Data - 100,000KB
"Holes" - 700,000KB
Large heap - 131072KB
Large heap - 83KB
Overhead/unused - 130989KB
Overhead - 0KB
然而,什么是GC hole? dotNet Memory Profiler 的文档确定了“漏洞”:
“Holes” represent memory that is unused between two allocated instances. “Holes” appear when the heap is not fully compacted, due to pinned instances or optimizations in the garbage collector.
我想知道的是:
- 哪种类型的两个分配实例之间出现“洞”?
- 固定了哪些实例?
- 如何压缩堆?
希望有人能解释一下。