BennyO
BennyO

Reputation: 123

unexpected GC behaviour

I'm seeing some strange GC behaviour on the WP8 emulator when working with large byte arrays, i.e. the memory is not released, nor reused.

Example: The memory usage baseline of my application is ~17MB. If I then allocate a 50MB byte array memory usage will as expected increase to 67MB. I reinitialize the byte array to a size of zero and call GC.Collect() which doesn't reduce the total memory usage. This is as expected; the memory aren't release back to windows, merely flagged as reusable. If I then reinitialize the byte array to the same size, 50MB, I would expect the memory to be reused and the total memory usage not to increase. Right? False. Memory is not reused and now the total memory usage is 117 MB. See sample code below..

Can anyone explain this? Since I'm allocating the exact same number of bytes the problem shouldn't be rooted in LOH fragmentation, right?

    // memory usage = 17MB
    var tb = new byte[1024*1024*50];
    // memory usage = 67MB
    tb = new byte[0];
    GC.Collect();
    // memory usage = 67MB
    tb = null;
    GC.Collect();
    // memory usage = 67MB
    tb = new byte[1024*1024*50];
    // memory usage = 117MB
    GC.Collect();
    // memory usage = 117MB;

Upvotes: 0

Views: 214

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273229

I tried this with a button to repeat the allocation, the first click more or less acts like you describe but after that the memory use is flat.

So your observations are most likely disturbed by start-up issues and maybe allocations happening in the background.

The GC, from a broader perspective, behaves as expected.

Upvotes: 1

Related Questions