I use Marshal.AllocHGlobal
to allocate several huge chunks (100MB each) of unmanaged memory in my C# application (in Windows). I use these to allocate smaller pieces of unmanaged memory. After benchmarking these smaller allocations, I recognized that while over 99% of them cost 0-10 C# StopWatch
ticks, some cost 30ms to 120ms and I could chase the cost down to individual memset
calls (of 150 Bytes). I replaced the memset
call with two memory writes to addresses in the memory area that memset
would touch and observed the same cost. This means I have memory accesses that cost several 1000 times more than the cost of a main memory access. As page faults seem to have a similar cost, I was wondering if page faults could cause this behavior, but looking at the windows task manager, I am far from my main memory capacity when the problem occurs.
I tried to reproduce this in microbenchmarks without any success. Does anyone have any clue what could cause this behavior or what I could do to chase it down?