Soldalma
Soldalma

Reputation: 4758

Why am I getting OutOfMemory exceptions when I have plenty of memory?

I have a program (Fractal10) that executes a loop where the number of iterations depends on a parameter I set manually. When the number of iterations is small the program runs fine. When the number of iterations is large I get the following error:

Unhandled Exception: System.TypeInitializationException: The type initializer for '<StartupCode$Fractal10>.$Program' threw an exception. ---> System.AggregateException: One or more errors occurred. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. 

I am running Windows 10 Professional with 16 GB of memory and 25600 MB of virtual memory on an Intel Haswell processor and the latest version of Microsoft Visual Studio 2017. After the exception is thrown, the Task Manager shows the following:

Memory: 64%
Fractal10 (32 bit)L 1,719.2 MB # this is the culprit
Microsoft Visual Studio 2017 (32 bit): 822.7 MB
... # other apps

Why am I getting this error when only 64% of memory has been used? Is there anything I can do about it?

Upvotes: 0

Views: 1455

Answers (2)

Julius H&#246;rger
Julius H&#246;rger

Reputation: 381

You should use 64 bit versions of your software if you intend to use more than ~1,8 GB of memory.

Upvotes: 4

Adam Thomason
Adam Thomason

Reputation: 1166

First of all, is your OS 32 or 64bit? If it's the former, you won't actually have access to all of the installed memory.

Additionally, (and I'm assuming you're using the .Net framework, correct me if I'm wrong) you'll need to set your platform target to x64 inside your build configuration. On top of this, you can add the following config to your app.config file:

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

This will allow traversal of objects greater than 2GB.

Upvotes: 3

Related Questions