Jonathan Kehayias
Jonathan Kehayias

Reputation: 3412

Creating a Memory Consuming Application

I want to write an application that consumes a lot of memory on a server to be able to show problems associated with memory pressure on a server. I know C# fairly well, but I am curious what the most efficient method of causing an application to consume excessive amounts of memory in a controllable manner. For example, I'd like to be able to pass a parameter that says to consume x MB of memory and have it consume somewhere close to that value. Any thoughts on how I might do this would be greatly appreciated.

Upvotes: 3

Views: 1926

Answers (3)

Conrad Frix
Conrad Frix

Reputation: 52675

Wouldn't the easiest way is just to create a byte array of the size you're interested in. To get very large allocations you may need to use more than one array using this technique.

Also if you're so inclinded you could p/invoke to VirtualAlloc

If you just need to an app you could also use Testlimit from sysinternals. This utility was used in Mark Russinovich's awesome Pushing the Limits of Windows series.

Upvotes: 3

Fernando
Fernando

Reputation: 4028

One way of doing it could be creating an array of bytes. Convert your input value from MB to bytes and then allocate the array. If you want to try the stack allocation directly, you could use stackalloc.

Upvotes: 2

Alexei Levenkov
Alexei Levenkov

Reputation: 100610

Depending on what "memory pressure" you are looking for:

  • boot.ini burnmemory option - http://support.microsoft.com/kb/833721 to really restrict amount of available memory.
  • create a program that simply allocates large amount of memory and actively touches it. This way you can see how your other program reacts to slowness in memory allocation/usage.
  • allocate memory in the process you are interested to create pressure on address space (32-bit mostly).

Upvotes: 2

Related Questions