OutOfMemoryException when allocation large memory blocks
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: OutOfMemoryException when allocation large memory blocks

  1. #1
    Join Date
    Jan 2010
    Posts
    2

    OutOfMemoryException when allocation large memory blocks

    Dear Softwaredevelopers,

    In my application I have to allocate a huge amount of memory but the following C# console programm creates an OutOfMemoryException when I try to allocate memory >512 MB. and I wonder why.

    My System:
    Intel Core2 Quad CPU 3GB
    Windows XP 32-Bit
    Visual Studio 2008
    available free memory 2.3GB

    Can anybody help?

    Greetings Christian


    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace MemoryAllocation
    {
    class Program
    {
    static void Main(string[] args)
    {
    int size = 2;

    while (size > 0)
    {
    Byte[] b = new Byte[size];

    for (int i = 0; i<size; ++i)
    b[i] = 13;
    size *= 2;
    }

    }
    }
    }
    Attached Images Attached Images  

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: OutOfMemoryException when allocation large memory blocks

    What's your virtual memory set to? I think that can sometimes cause a cap on your available space for a byte array.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,289

    Re: OutOfMemoryException when allocation large memory blocks

    The while( size > 0 ) is causing an infinite loop. That's why you're running out of memory (because size is always greater than 0).

  4. #4
    Join Date
    Jan 2010
    Posts
    2

    Re: OutOfMemoryException when allocation large memory blocks

    the execption occurs when I have 2.3 GB free space and want to allocate more than 512MB...
    in the while loop I only wanted to find out what the limit is.
    It also occurs withot the while loop.
    the memory will be and is cleaned by the garbage collector.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: OutOfMemoryException when allocation large memory blocks

    As Arjay said, you have no terminating condition in your while loop; 'size' is always greater than 0 because you never decrease it (you actually add to it).

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,289

    Re: OutOfMemoryException when allocation large memory blocks

    Quote Originally Posted by guentherc View Post
    the execption occurs when I have 2.3 GB free space and want to allocate more than 512MB...
    in the while loop I only wanted to find out what the limit is.
    It also occurs withot the while loop.
    the memory will be and is cleaned by the garbage collector.
    You might think so, but in this type of tight loop, you'll run out of memory before the cg has a chance to run. Or, even if the cg has a chance to run, it won't be able to keep up.

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    Re: OutOfMemoryException when allocation large memory blocks

    Or it's possible that your operating system does not have 512MB of contiguous free RAM and therefore cannot fulfill your request. Every programming language will hit the exact same issue, though the exact point at which they hit it will vary depending on what your operating system is doing.

    It's very unusal for an application to actually require such large memory buffers. Generally you can use a chunked approach and process 4MB at a time (or less) which can result in a more performant application. Have you considered that approach?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center