1

Looking at this, it appears that Single is the non integer number with the lowest memory requirements in C#. If I create:

Single[,] data = new Single[100000000, 10];

I am getting a:

System.OutOfMemoryException

Does this depend on my machine's available RAM? Could I keep an array like this or larger in memory? Thanks.

10
  • Yes, it depends on your available RAM. You should keep in mind that your array would be the size 40 GB if my calculations are correct. Commented Apr 5, 2017 at 13:04
  • So you also need to look into <gcAllowVeryLargeObjects> Commented Apr 5, 2017 at 13:05
  • 2
    @Crowcoder OP is talking about non integer number Commented Apr 5, 2017 at 13:06
  • Thanks. Just set platform target to x64 .. still the same. Commented Apr 5, 2017 at 13:06
  • @JeroenvanLangen, thx, missed that Commented Apr 5, 2017 at 13:07

1 Answer 1

3

Does this depend on my machine's available RAM?

To an extent, yes. You cannot have an object that is significantly larger than your memory. However:

Could I keep an array like this or larger in memory?

32bit processes can be 2GB in size maximum (3GB with special exceptions). But even on 64bit systems using a 64bit process, you cannot exceed the size of 2 GB for a single .NET object. So you can have two large arrays of 1.5 GB each, but you could not have a single one of 3 GB. And yours seems to be way above that size.


As commenters pointed out, there is a way around this limit with later editions of .NET:

Put this in your App.config file:

<configuration>  
  <runtime>  
    <gcAllowVeryLargeObjects enabled="true" />  
  </runtime>  
</configuration>  
Sign up to request clarification or add additional context in comments.

8 Comments

Note that article you linked is 12 year old. Now there is a way to allocate objects larger than 2GB.
Well, you can use <gcAllowVeryLargeObjects> to allow objects greater than 2GB in size.
@Evk Thanks, added it.
Wow this works for the example. i chose. Bit confused now how, as I definitely do not have 40GB of RAM ... must be magic.
@csetzkorn See Virtual memory. Note that just declaring the array only reserves space for it in virtual memory. Only when you start setting values in it will the memory actually be commited. Try initialising it in a loop and watch your program slow to a crawl... ;)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.