Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
}

}
}
} 
Posted

That while loop looks suspicious to me - It'll just keep allocating more and more memory until you run out, since size will never get below zero if you keep doubling it like that!
 
Share this answer
 
Based on these, you are running out of memory - here[^] and here[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900