none
Out of Memory Exception RRS feed

  • Question

  • Hello,

    I created a console app, switched configuration to x64 to avoid the 2GB limit of 32bit system. My machine has 16GB of RAM with only 3.5 GB in use.

    When I try to create a list with 1 billion integers, I get an Out Of Memory Exception:

    int len = 1000000000;
    List<int> data = new List<int>(len);
    

    Why would I get this exception, if I run in x64 environment?

    Thanks.


    Evgueni Tsygankov
    Monday, February 21, 2011 5:31 PM

Answers

  • .NET has a fixed 2GB maximum array size for a single array (even on x64).  If you want to have a larger, single chunk of memory, you have to segment it into pieces.  Your array allocation above is trying to allocate 4GB, which will always fail.

     

    This can be done quite easily - for example, see this MSDN post on a BigArray<T> implementation.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Popsovy Monday, February 21, 2011 8:42 PM
    Monday, February 21, 2011 6:34 PM
    Moderator