2

As per this website here,the MaxSize of an array can be more than 2GB in the x64 environments and the actual elements it can hold is UInt32.MaxValue.

So,I've made my app.config like :

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

and my array declaration and initialization is :

int idsCount = 999999999; long[] ids = new long[idsCount]; 

which is much less than the UInt32.MaxValue,but an OutofMemory Exception was thrown.
Am I doing any mistake?
My Framework version is also 4.5

2
  • Do you get the same error with an array in ints or chars? Commented Aug 10, 2014 at 3:43
  • @rhughes : yeah...even with the int[],it is the same :( Commented Aug 10, 2014 at 3:44

2 Answers 2

7

The basic check-list for this failure:

  • Watch out for the Visual Studio Hosting process that's enabled by default when you run your program with the debugger attached. It uses a different .config file, yourapp.vshost.exe.config. Quickly eliminate this kind of mishap by disabling the option, Project + Properties, Debug tab.

  • This can only work when your process runs in 64-bit mode, not the default for a new project. Project + Properties, Build tab, be sure to have the Platform target set to AnyCPU and the "Prefer 32-bit mode" checkbox unticked.

  • While you are there, click the Application tab and double-check the Target framework setting. Must be 4.5 or higher of course.

  • The Windows version matters, different versions have different limits for the maximum VM size of a process. Review this MSDN page and locate your Windows version and edition. You'd run into trouble with Windows 7 Home Basic for example, 8 Gigabytes max.

  • Large allocations in 64-bit mode can fail on a commit failure. Committing is the technical term for reserving space in the operating system's paging file. It acts as the backing-store in case your large array needs to be paged-out to make room for other processes. Pretty much guaranteed to happen if your machine doesn't have well over 8 Gigabytes of RAM available. You might have the size limit set too low or the OS can't grow the paging file quick enough to give you the space you need. Demonstrated well in this blog post by Mark Russinovich.

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

1 Comment

"Prefer 32-bit mode" checkbox unticked did the trick for me.Thanks a Lot
1

A C# long is 64 bits, or 8 bytes. The array ids is dimensioned for one less than one billion entries, at 8 bytes per entry. That array as defined will use around 8GB.

2 Comments

I've declared an integer array also,but still the same. actually,by setting the gcAllowVeryLargeObjects in app.config for Framework 4.5 will increase the limit of an object to more than 2GB.
int is 32 bit, 4 bytes, so you'll still need 4GB for that array. The OutOfMemory exception is just saying the runtime cannot allocate that amount of storage. It could be that there is not enough physical memory available, <del>or I'd have to look this up, but would not be surprising if</del> and since array needs one big contiguous memory chunk, this size block might not be available.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.