0

I have out of memory when I want create big array for example.

 var size = 20000;
 var matrix = new decimal?[size, size];

I have set 64 bit project and uncheck "Prefer 32 bit".

Screen1

I added in App.config

<runtime>
    <gcAllowVeryLargeObjects enabled="true"/>
</runtime>

Screen2

I have 16 GB RAM. What did I forget about?

11
  • Possible duplicate of Create big Two-Dimensional Array Commented Jan 12, 2019 at 21:58
  • 2
    How much memory do you have available? Note that an array of 20.000 * 20.000 nullable Decimal values would require around 9.6GB of free, continuous, memory to be available to your program. Commented Jan 12, 2019 at 21:59
  • 2
    Note that the size of bytes of Decimal is 16. So you are trying to allocate an array that would require 16*20000*20000 bytes (roughly 6 GB). Note that you could get that exception even if you have enough free RAM available. The required 6 GB would need to be available as a continuous memory block within the process address space of your program; if that is not the case the same exception occurs... Commented Jan 12, 2019 at 21:59
  • I have 16 GB ram Commented Jan 12, 2019 at 22:01
  • 2
    @elgonzo, you forgot to calculate the Nullable footprint Commented Jan 12, 2019 at 22:03

1 Answer 1

7

10Gb in one object (24 bytes (size of nullable decimal) * 20000 * 20000) exceeds the default size of a single object in .NET (i.e., the CLR) - see this MSDN documentation "gcAllowVeryLargeObjects Element" on how to exceed that by changing a configuration variable. That article also lists other limits on arrays that you may be interested in, e.g., you can only have 2^32 elements in an array - in case 400M elements isn't enough and you're thinking of taking it to the next level.

A better solution would be a "jagged" array, notated [][] in C# - each row will be a separate object (a 1-D array []) and thus you won't hit any of these limits.

You might want to consider other options. E.g., perhaps your array is sparse? Perhaps it is a matrix with usable attributes e.g., upper triangular? Perhaps you can rethink what you're doing so you don't need a 10Gb array of anything - instead preferring another data structure or even better, if that amount of data really needs to be processed, an external algorithm that stores the data on disk and accesses it in a predictable (e.g., sequential, or multiple-sequential) way?

And finally, I question why you need a giant array of nullable decimal: sounds like you really have a sparse array and should be looking at solutions for sparse arrays (plenty on StackOverflow and Google about those).

Sign up to request clarification or add additional context in comments.

6 Comments

The size of a nullable decimal is 24 bytes though.
I need decimal to me problem. But for test i set int?[20000,20000] and I still have the same problem - out of memory
@Lucyper - int? takes 8 bytes so that is a 3Gb array that still exceeds 2Gb. Maybe start with a smaller problem? Another solution is to use a two dimensional "jagged" array [][] where each row will be a separate object. (Instead of being a single object.) It is probably a better solution in any case. Try that, if you insist on going down this route.
@davidbak I created an algorithm for an array [,]. The array [] [] may be an emergency solution that requires a lot of code changes from my site.
@Lucyper, you could perhaps create a class featuring an [int, int] indexer (learn.microsoft.com/en-us/dotnet/csharp/programming-guide/…) as a replacement for your [ , ] array, which could perhaps avoid too many code changes. The class would internally manage a collection of smaller arrays (or jagged array, as suggested), with the indexer implementation managing the access to the elements within the individual arrays "hidden" inside this class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.