12

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();
    }
Mehdi LAMRANI
  • 11,289
  • 14
  • 88
  • 130
  • 19
    Just start visual studio :) – rerun Jan 30 '12 at 20:43
  • 6
    You are completely conflating *virtual memory* with *physical memory* -- as their names imply, they are very different. Which do you care about saturating -- virtual address space, or physical space on RAM chips? – Eric Lippert Jan 30 '12 at 20:46
  • @Eric Lippert : Ok, I have read this : http://blogs.msdn.com/b/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx But I am not very familiar with this yet. To put it straight, I want my program to skyrock the Task Manager Memory monitor, so I guess I am targeting the Physical Memory – Mehdi LAMRANI Jan 30 '12 at 20:49

3 Answers3

7

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?

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Thank you for your interest. As I stated in an other comment : 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 a mem-monitoring / thread-limiter draft. – Mehdi LAMRANI Jan 30 '12 at 21:05
  • @MikaJacobi: you get `OutOfMemoryException`, so you actually simulated the case when your .NET application allocates too much memory. If this is not the case you want to check, do not allocate too much memory and check what you want. – Tigran Jan 30 '12 at 21:14
  • @Tigran : You can have an OutOfMemoryException while there is STILL plenty of RAM available as displayed in the Task Manager. This is NOT what I want to simulate. I just want to see the Memory Consumption as displayed in the TM go up. I really do not know how to put this any clearer. Article here : http://blogs.msdn.com/b/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx – Mehdi LAMRANI Jan 30 '12 at 21:19
  • @MikaJacobi: ok, so just guess. A scenario: you want understand how your application behaves/works on PC where most of the memory is occupied by **other** applications. Does this scenario matche your needs? – Tigran Jan 30 '12 at 21:24
  • @Tgran LOL. No. Not at all. I Just want the app to have its private working set grow, grow, GROW, and eat up a whole bunch of RAM. Just Like firefox when you keep opening dozens of tabs on and on. This was I can watch it grow, and programatically tell it to stop creating thread when a certain level is reached – Mehdi LAMRANI Jan 30 '12 at 21:35
  • @MikaJacobi: you can not acieve that with `C#` if the memory consumption limits applied to `CLR Process` are too restrictive for you. Choose other *non* .NET technology. – Tigran Jan 30 '12 at 21:49
  • 4
    @Mika Jacobi: Firefox does not necessarily eat up RAM. It eats up *virtual memory*, and the *memory manager* chooses to keep that virtual memory mapped to *physical memory* because Firefox is the application in the foreground and hence gets priority. If you minimize it and start up some other memory-intensive application, then sure enough, the operating system will dump Firefox's least-used pages out of RAM, into disk, and let some other process map its virtual memory into RAM. – Eric Lippert Jan 30 '12 at 21:54
  • 2
    @Mika Jacobi: Now, if what you want to do is allocate *virtual memory* that is *never swapped to the page file* then you can do that by calling VirtualAlloc and passing PAGE_NOCACHE. Of course, doing so is dangerous, as I'm sure you can imagine; once you've committed it, that page is going to be in RAM until it is decomitted. Using this technique obviously you cannot allocate *more RAM* than you have *virtual memory* in a process. – Eric Lippert Jan 30 '12 at 22:01
  • @EricLippert : Yes you are totally right. I was not precise enough indeed. The problem I am having is that my problematic application is not freeing memory in some way, while threads are being created and there are too many heavy objects being created and this causes a steep slope in the Task Ram Monitor. So for some reason, there is no (or not enough) swapping occurring, hence the memory saturation occurring. I was trying to reproduce the same behaviour, I thought it was more straightforward than that. – Mehdi LAMRANI Jan 30 '12 at 22:08
  • @EricLippert : Your insights puzzled me. The question is now : Why am I having this steep slope occurring in my original App ? If memory is nor freed nor cached, as you say, then I guess it is because the data is ALL being used often enough for it not to get swapped. Is that the exact behaviour I should reproduce ? – Mehdi LAMRANI Jan 30 '12 at 22:11
  • @Mika Jacobi: Or perhaps you have a reference/memory leak. – Greg D Jan 31 '12 at 13:35
1

The thing is that with C# you can not grow more then approximately 1.2 GB of RAM on 32 bit .NET framework. You can have even 8GB of RAM on your 64 bit machine, but if the process you run was compiled for 32bit architecture, it will lead to OutOfMemoryException as soon as it reaches approx 1.2GB.

For this kind of testing I would suggest choosing other type of languages/frameworks.

EDIT

A good link on subject:

is-there-a-memory-limit-for-a-single-net-process

Community
  • 1
  • 1
Tigran
  • 61,654
  • 8
  • 86
  • 123
  • you are right : I am using x64 OS and x64 Compiler. What I have described happens actually with my application. I am just trying to isolate the mem management before implementing it in the App once I have tested it successfully – Mehdi LAMRANI Jan 30 '12 at 20:52
  • @MikaJacobi: you can *try*, if it's possible, compile your application targeting 64bit platform, so you will have a much bigger space to grow in. – Tigran Jan 30 '12 at 20:57
  • Ok but this does not answer the question : How ? :) I AM compiling on an x64 Platform as stated in my first comment. I already have a hungry application that eats up a whole bunch of GB's, I just want to isolate/reproduce the situation in "Lab conditions" to tackle it – Mehdi LAMRANI Jan 30 '12 at 21:03
  • @MikaJacobi: what you want to simulate actually? can you provide a scenario of the behaviour/state/condition you want to simulate? – Tigran Jan 30 '12 at 21:04
-1

If the problem that you're running into is that your process is running out of virtual memory space before the hardware is running out of physical memory space then you could just spin up a number (5 maybe?) processing with your code (and something to stop them at say 1-2 GB so they don't OOM themselves). It's probably not as good of a solution as a unmanaged call to allocate memory, but it would be easy enough to do.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • What do you exactly mean by "spin up a number processing with your code" ? – Mehdi LAMRANI Jan 30 '12 at 21:14
  • Easy way, double click the .exe 5 times. If you want to be more reproducible or formal, have another program that just starts up several instances of a program like the one you have in the OP. – Servy Jan 30 '12 at 21:26