I want to create a tool to simulate memory restrictions to memory stress test other applications. I came up with the following code after going through some google searches, but when running this, the task manager or the resource monitor does not show any difference in memory usage. Just a flat line.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Win32Tute
{
unsafe class Program
{
// Heap API flags
const int HEAP_ZERO_MEMORY = 0x00000008;
// Heap API functions
[DllImport("kernel32")]
static extern int GetProcessHeap();
[DllImport("kernel32")]
static extern void* HeapAlloc(int hHeap, int flags, int size);
[DllImport("kernel32")]
static extern bool HeapFree(int hHeap, int flags, void* block);
private static int ph = GetProcessHeap();
public static void* Alloc(int size)
{
void* result = HeapAlloc(ph, HEAP_ZERO_MEMORY, size);
if(result == null) throw new OutOfMemoryException("Couldn't execute HeapAlloc");
return result;
}
public static void Free(void* block)
{
if(!HeapFree(ph, 0, block)) throw new InvalidOperationException("Couldn't free memory");
}
public static void Main(string[] args)
{
int blockSize = 1024*1024; //1mb
byte*[] handles = new byte*[1024];
Console.WriteLine("Memory before : " + (Process.GetCurrentProcess().PrivateMemorySize64/1024)/1024); // get value in Megabytes
try
{
for(int i=0; i<1024; i++)
{
handles[i] = (byte*)Alloc(blockSize);
}
}
finally
{
Console.WriteLine("Memory after : " + (Process.GetCurrentProcess().PrivateMemorySize64 / 1024)/1024);
Console.WriteLine("Finished allocating 1024MB memory....Press Enter to free up.");
Console.ReadLine();
}
try
{
for(int i=0; i<1024; i++)
{
Free(handles[i]);
}
}
finally
{
Console.WriteLine("Memory at the end : " + (Process.GetCurrentProcess().PrivateMemorySize64 / 1024)/1024);
Console.WriteLine("All allocated memory freed. Press Enter to quit..");
Console.ReadLine();
}
}
}
}
This kind of thing is almost always a bad idea. If you succeed in creating a program that chews up memory, you’ll likely find that doing so doesn’t stop the other program from responding. The virtual memory manager will go to heroic efforts to keep the other program running. It will, for example, page your memory hog’s data to disk so as to keep the working program’s data in memory where it belongs. And if you modify your memory hog so that it locks pages in memory (i.e. doesn’t let the pages get swapped out), then the computer will start thrashing.
You’re much better off writing diagnostic code in the program you’re testing that will let you call SetProcessWorkingSetSizeEx to test how the program responds under varying memory conditions. If you can’t modify the program you’re testing, you can write a program that gets the test program’s handle and calls
SetProcessWorkingSetSizeEx, passing that handle.