7

What does [Garbage collection] mean in this pic? And the "20 calls" thing?

I mean, how can I figure out why GC took so long? Was it collecting a lot of small objects? A single big one? Any hints as to how to optimize this at all?

The code in question is:

private void DeserializeFrom(SerializationInfo info)
{
    Width = info.GetInt32("width");
    Height = info.GetInt32("height");
    var data = (List<byte>)info.GetValue("cells", typeof(List<byte>));
    cells = new Cell[physicalSize.Width, physicalSize.Height];
    int pos = 0;
    for (int x = 0; x < physicalSize.Width; x++)
    {
        for (int y = 0; y < physicalSize.Height; y++)
        {
            cells[x, y] = new Cell();
            if (x < Width && y < Height)
            {
                cells[x, y].HasCar = data[pos];
                pos++;
            }
        }
    }
}

Nothing too fancy. I suspect the culprit is the big List<byte> object, but I thought collecting a single, big object is supposed to be instant (as opposed to collecting a bunch of small objects).

Stefan Monov
  • 11,332
  • 10
  • 63
  • 120

2 Answers2

2

If you want to find out what is causing GCs, what objects are being allocated and collected, you can do it via dotMemory. Here is a tutorial that explains how to optimize memory traffic: https://confluence.jetbrains.com/display/NETCOM/Tutorial+3+-+How+to+Optimize+Memory+Traffic+with+dotMemory

Maria
  • 526
  • 4
  • 7
1

A little late to the party but if you are using .Net then you are using managed code, which basically means that the .Net runtime disposes your objects accordingly so you don't have memory leaks as opposed to C or C++.

Garbage Collection is whenever the runtime takes a time to manage the allocation and release of memory for the application. In this case that is what's taking place.

Please take a look at this filter that can be used with doTrace (I have version 6) so that you can analyze garbage collection and determine when it might be blocking your execution. https://www.jetbrains.com/profiler/help/CLR_Activity.html

xmorera
  • 1,933
  • 3
  • 20
  • 35
  • I realize this is a few months old, but I feel it is worth mentioning that you absolutely can have memory leaks in .net code. Static events are a very common cause of this. If you subscribe to a static event with a transient and fail to unsubscribe from the event before releasing all known references to the transient, the transient will be kept alive by the reference from the static event; the garbage collector will never collect it. – Kelsie Aug 28 '15 at 16:54
  • @kelsie you are right and I did not express myself correctly. You do not have memory leaks in regards to how you used to program with unmanaged code where it was easy to simply not deallocate an object properly and have a memory leak. Also as you rightly point out, events is one way in which you can keep a reference to an object so that it does not get disposed. – xmorera Aug 30 '15 at 04:24
  • Other possibility was in pre 4.5 with the large object heap when your application was very intensive in the use of arrays that were allocated in the LOH and given that before you could not compact the LOH, you could get out of memory exceptions. – xmorera Aug 30 '15 at 04:26