Reputation: 3412
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
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
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
Reputation: 100610
Depending on what "memory pressure" you are looking for:
Upvotes: 2