Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extreme Memory Conditions Testing : How to saturate RAM?

Tags:

performance

c#

I would like to write a small piece of program that launches threads, consumes available RAM memory in a linear fashion, until a certain level, and stops (ideally, pauses until "enough" memory is freed and continues creating threads after that, and so on.)

I tried the following, but the list.Add(new byte[]) requires contiguous RAM space and drops an OutOfMemoryException, which is NOT what I am trying to simulate.

EDIT :
I have a multi-threaded memory-hungry application that eats up a whole bunch of RAM GB's. All I want is to isolate/reproduce that situation in "Lab conditions" to tackle it, i.e write an adaptive mem-monitoring / thread-limiter draft. I am using x64 OS an x64 Platform. To make it clear : The result I want to see is the Task Manager Memory Monitor going up straight due to the program.

    static void Main(string[] args)
    {            
        ComputerInfo ci = new ComputerInfo();
        D("TOTAL PHYSICAL MEMORY : " + Math.Round(ci.TotalPhysicalMemory / Math.Pow(10,9),3) +" GB");

        //########### Fill Memory ###############
        var list = new List<byte[]>();

        Thread FillMem= new Thread(delegate()
        {
            while (Process.GetCurrentProcess().PrivateMemorySize64 < MAX_MEM_LEVEL)
            {
                list.Add(new byte[1024 * 10000]); //<- I Need to change this
                Thread.Sleep(100); 
            }
        });

        FillMem.Start();

        //########### Show used Memory ###############
        Thread MonitorMem = new Thread(delegate()
        {
            while (true)
            {
                D("PROCESS MEMORY : " + Math.Round(Process.GetCurrentProcess().PrivateMemorySize64 / Math.Pow(10, 6), 3) + " MB");
                Thread.Sleep(1000);
            }
        });

        MonitorMem.Start();

        Console.Read();
    }
like image 484
Mehdi LAMRANI Avatar asked Jan 30 '12 20:01

Mehdi LAMRANI


1 Answers

The question is still quite confusing; it is not clear to me what you are trying to do here and why.

If you genuinely want to be consuming physical memory -- that is, telling the operating system no, really do not use this portion of the physical RAM chip that is installed in the machine for anything other than what I say -- then I would probably use the aptly-named AllocateUserPhysicalPages function from unmanaged code.

That will then reduce the amount of physical memory that is available for other uses, forcing more virtual memory pages to go out to the page file.

Other than making all the programs running on your machine a whole lot slower, I'm not sure what you intend to accomplish by this. Can you clarify?

like image 144
Eric Lippert Avatar answered Nov 15 '22 16:11

Eric Lippert