vrwim
vrwim

Reputation: 14300

How can I hog memory to test another application?

So I want to test my windows application under low memory conditions, and I have found that the easiest way to do this is to create another app (a Console application) that just hogs memory.

I have created this monster:

while (true)
{
    try
    {
        Marshal.AllocHGlobal(1024);
    }
    catch{}
}

But it only goes to 3.7 GB. I then open another instance of this application and it goes back down.

How can I keep the garbage collector from collecting my allocations?

Or: how can I test low-memory conditions on my universal windows application?

Upvotes: 4

Views: 769

Answers (2)

Chris Shain
Chris Shain

Reputation: 51329

You are probably running into the limit that you see because you are running your memory hog as a 32-bit process, which can only address ~4GB of memory.

Try running as a 64-bit process (compile for x64 explicitly), or spawning multiple copies.

That said, there are better ways to limit the memory available to the process under test. One example is given here: Set Windows process (or user) memory limit

Upvotes: 1

bodangly
bodangly

Reputation: 2624

You can try changing the GCSettings latency mode to SustainedLowLatency which will avoid garbage collection at all unless the system will run out.

GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency;

Upvotes: 3

Related Questions