C #: use almost all memory

My goal is to reach the point where I filled all the available memory (note that I want to do this gradually, so I have a minimum amount of memory left until my last allocation). How can i do this?

+3
source share
8 answers

One option is to create a list of some kind and simply add an unwanted file to it in an endless loop.

Depending on the required accuracy, however, this may take some time - one way to speed this up would be to add large structures to the list items at the initial stage, and then capture OutOfMemoryError and try again with less materials until you get the accuracy. which you need.

+4
source
for (object[] o = null;; o = new[] { o });
+9
source
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;
    }
}
+2

, , , .

, /. LoadRunner - , . " #", , , . , , , , - , OutOfMemory.

, , , : http://msdn.microsoft.com/en-us/magazine/cc163613.aspx

+1

Maybe something like this?

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

Continue to add a large buffer to the memory stream inside the while loop.

0
source

LINQ Method:

Enumerable.Range(0, int.MaxValue).ToArray();
0
source
        int size = 1024;
        var bytes = new List<byte[]>();
        while (true)
        {
            try
            {
                bytes.Add(new byte[size]);
            }
            catch
            {                   
                if (size == 1)
                {
                    throw;
                }
                size = size / 2;
            }
        }
0
source

Source: https://habr.com/ru/post/1725790/


All Articles