A
A
Alek_dr2015-10-30 00:55:57
.NET
Alek_dr, 2015-10-30 00:55:57

An unhandled exception of type 'System.OutOfMemoryException' in mscorlib.dll. What to do?

There is the following method in C#

public static List<Int64> Time(Printer P, BigInteger T)
    {
        List<Int64> res = new List<Int64>();
        Int64 d;
        Int64 current = 0;
        uint i = 1;
        while (current < T)
        {
            current = P.f * i;
            if (current < T)
                d = (Int64)T - current;
            {
                res.Add(current);          
                i++;
            }            
        }
        return res;
    }

After i = 33554433, an exception System.OutOfMemoryException is thrown.
Its essence is clear, but what to do is not.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dasha Tsiklauri, 2015-10-30
@dasha_programmist

- increase the amount of RAM, install x64 system
- run the application in the cloud with the restriction on RAM removed
- come up with an algorithm that will require less memory

A
Alexey Kulakov, 2015-10-30
@carbon88

1) Are you sure that your elements will be less than int.MaxValue?
2) Throw this List into the furnace, you can leave it only if you know the number of elements in advance and you can initialize maxCapacity right away. If not, then definitely throw it away. The fact is that after the 4th insertion (if my memory serves me) with each subsequent insertion of the Capacity element, the Capacity doubles, respectively, the array of elements inside also doubles and the elements are distilled into a new one. Can you imagine how fast the array is growing? that is, on i = 33554433 + 1 list.Add() will try to allocate a piece under 67108866 elements, and this requires a continuous piece of memory which by that time is probably not located and everything falls with an OutOfMemoryException. By the way, you also need to store an array from which we will copy to the one that we copy. this is a huge waste of memory. tin is simple.
I don’t know what kind of code you have that uses the result of the Time method, but if it’s just an enumeration, then this algorithm of yours is just tinny, and you can probably not use the internal list at all, but give out the calculated value as needed through yield return and calculate on fly.

A
Alek_dr, 2015-10-31
@Alek_dr

Perhaps I'll change the code better)
thanks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question