tags:

views:

161

answers:

8

My goal is to reach a point where I have filled up all avalible memory (note that I want to do this incrementaly, so before my last alocation I have a minimal memory remaining). How can I do this?

+3  A: 

One option would be to create a list of some kind, and just keep adding junk to it in an infinite loop.

Depending on the precision needed, though, that could take a while - one option for speeding it up would be adding large structs to the list elements initially, then catching the OutOfMemoryError and trying again with smaller stuff until you've got the precision you need.

Anon.
I feel that there might be a problem with this approach. I believe the List<> class doubles up the internal storage every time it grows - this would mean that in the worst case, you'd have used only slightly more than half the available memory before throwing your Exception. I'm happy to be proven wrong -- or maybe that's good enough for Jack's scenario.
Ant
If a linked list doubles its storage requirements every time it grows (even temporarily), it's the worst linked list implementation ever.
Anon.
A: 

Maybe something like this?

var bytes = new List<byte[]>();
var time = new TimeSpan(100);
while (true)
{
    bytes.Add(new byte[1024]);
    Thread.Sleep(time);
}
ChaosPandion
A: 

Keep adding a large buffer to a memory stream inside a while loop .

REA_ANDREW
+4  A: 
for (object[] o = null;; o = new[] { o });
Pavel Minaev
If that actually works.. it's nasty... :P
Charlie Salts
Yep, it works! In fact, it works so well that it crashed Visual Studio 2008 (while running it under the debugger).
Evgeny
A: 

The LINQ way:

Enumerable.Range(0, int.MaxValue).ToArray();
Jader Dias
Except that it either fails or succeeds, but most likely fail allocating a single array that large.
Cecil Has a Name
Yep, this one throws OutOfMemoryException long before using up all the available RAM.
Evgeny
A: 
        int size = 1024;
        var bytes = new List<byte[]>();
        while (true)
        {
            try
            {
                bytes.Add(new byte[size]);
            }
            catch
            {                   
                if (size == 1)
                {
                    throw;
                }
                size = size / 2;
            }
        }
Jack
+1  A: 

The scenario you are talking about sounds like it might be for testing an application.

You may want to look into load/stress testing tools. LoadRunner is the first one that comes to mind. It's not "in C#", and I am pretty sure it isn't free, but it may be the right tool for the job. Also, you may want to look into fault injection, if you all you want to see is how your program handles an OutOfMemory exception.

For exactly what you asked about, here's an article with an application that consumes RAM: http://msdn.microsoft.com/en-us/magazine/cc163613.aspx

Merlyn Morgan-Graham
+1  A: 
var junk = new LinkedList<byte[]>();
int allocSize = 100 * 1024 * 1024; // 100 MiB
while (allocSize > 0)
{
    try
    {
        junk.AddLast(null);
        junk.Last.Value = new byte[allocSize];
    }
    catch (OutOfMemoryException)
    {
        allocSize = (allocSize - 1) / 2;
    }
}
Cecil Has a Name