7

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

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.

What I want to know is:

  1. "holes" appear between what kind of two allocated instances?
  2. what kind of instances are pinned?
  3. How to compact the heap?

I hope somebody can explain it.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
tianyi
  • 109
  • 6
  • 2
    I suspect the article is about a different kind of hole. – CodesInChaos Oct 15 '12 at 09:44
  • 4
    "“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." [From the .NET Memory Profiler Documentation](http://memprofiler.com/onlinedocs/default.htm?turl=managedheaps.htm) – CodesInChaos Oct 15 '12 at 09:46
  • 2
    Why are so many people upvoting this question? The author's question is not clear. Based on the edit it also appears he has no idea what he is talking about. – Security Hound Oct 15 '12 at 12:07
  • 1
    What do you want as an answer? An explanation on the hole or how to resolve your memory issue? Unless you make that clear, this question is of low quality. – Lex Li Oct 15 '12 at 12:08
  • Thanks for the documentation,I got the definition of "Holes" from Memory Profiler Documentation. Then, what I want to know is "holes" appear between what kind of two allocated instances? what kind of instances are pinned? How to compact the heap? – tianyi Oct 15 '12 at 12:32
  • 1
    I don't believe this question should be closed. It's a good question now that it's been edited, and relevant to a lot of developers. – Dan Puzey Oct 15 '12 at 13:15

1 Answers1

6

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.

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

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

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367
  • 1
    It's worth noting that network connections are typically umanaged resources - so if the questioner's "TCP connection socket server" doesn't manage its conections very carefully, and is handling a large number of connections, this could be the cause of a large number of pinned objects. – Dan Puzey Oct 15 '12 at 13:12
  • All sockets are closed after use. I think there are some other unmanaged resources allocated and not disposed. – tianyi Oct 15 '12 at 14:51