Stress Testing Memory in C #

I want to create a tool to simulate memory limitations for testing memory stress in other applications. After performing some Google searches, the following code appeared, but when it starts, the task manager or resource monitor does not show a 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();
            }
        }
    }
}
+5
source share
2 answers

. , , , , , . , . , , , , . hog , (.. ), .

, SetProcessWorkingSetSizeEx, , . , , , SetProcessWorkingSetSizeEx, .

+3

, ...

, , . , / .

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*)Marshal.AllocHGlobal(blockSize);
            //write to the memory
            for (int off = 0; off < blockSize; off++)
                *(handles[i] + off) = 1;
        }
    }
    finally
    {
        //create a thread to ensure the memory continues to be accessed
        ManualResetEvent mreStop = new ManualResetEvent(false);
        Thread memoryThrash = new Thread(
            () =>
                {
                    int ihandle = 0;
                    while (!mreStop.WaitOne(0, false))
                    {
                        for (int off = 0; off < blockSize; off++)
                            if (*(handles[ihandle++ % handles.Length] + off) != 1)
                                throw new InvalidOperationException();
                    }
                }
            );
        memoryThrash.IsBackground = true;
        memoryThrash.Start();

        Console.WriteLine("Memory after  : " + (Process.GetCurrentProcess().PrivateMemorySize64 / 1024)/1024);
        Console.WriteLine("Finished allocating 1024MB memory....Press Enter to free up.");
        Console.ReadLine();

        mreStop.Set();
        memoryThrash.Join();
    }

    try
    {
        for(int i=0; i<1024; i++)
        {
            Marshal.FreeHGlobal(new IntPtr(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();
    }
}
+3

All Articles