什么是GC孔? [英] What are GC holes?

查看:79
本文介绍了什么是GC孔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C#编写了一个长TCP连接套接字服务器.服务器上的内存出现峰值.我使用dotNet Memory Profiler(一种工具)来检测内存泄漏的位置. Memory Profiler指示私有堆很大,并且内存如下所示(数字不是实数,我要显示的是GC0和GC2的Holes非常非常大,数据大小正常):

I wrote a long TCP connection socket server in C#. Spike in memory in my server happens. I used dotNet Memory Profiler(a tool) to detect where the memory leaks. Memory Profiler indicates the private heap is huge, and the memory is something like below(the number is not real,what I want to show is the GC0 and GC2's Holes are very very huge, the data size is normal):

  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孔? dotNet Memory Profiler的文档确定了孔":

Howerver, what is GC hole? dotNet Memory Profiler's documention definite the "Holes":

孔"表示两次分配之间未使用的内存 实例.当堆没有完全压缩时,会出现孔洞",原因是 在垃圾收集器中固定实例或优化.

"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.

我想知道的是:

  1. 在分配的两种实例之间出现空洞"?
  2. 固定哪种实例?
  3. 如何压缩堆?

我希望有人能解释一下.

I hope somebody can explain it.

推荐答案

一个固定对象是一种不允许在内存中移动的内存.当使用非托管代码时,通常需要这样做,这需要您传递指向内存中某种结构的指针-默认情况下,垃圾收集器可以自由移动该结构以最佳地管理内存,但是如果在您执行此操作时这样做给一些非托管代码一个指向该结构的指针,那么如果非托管代码移动了该非托管代码,它将不再指向正确的结构,从而导致意外的行为.

A pinned object is one that is not allowed to move in memory. This is often needed when working with unmanaged code which requires that you pass in a pointer to some structure in memory - by default the garbage collector is free to move that structure around in order to best manage memory, however if it does this when you've given some unmanaged code a pointer to that structure then if its moved that unmanaged code won't be pointing to the correct structure any more, leading to unexpected behaviour.

解决方案是固定"该对象以告知GC不应移动该对象.

The solution is to "pin" that object to tell the GC that it shouldn't move it.

您不能显式地压缩堆,GC在执行完整或部分收集时应该这样做(垃圾收集器的基本知识和性能提示

You can't explicitly compact the heap, the GC should so this itself when performing either a full or partial collect (with the exception of the LOH) - pinning lots of objects will make it harder for it to successfully manage this however. For more detail on the GC see Garbage Collector Basics and Performance Hints

这篇关于什么是GC孔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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