Question

I have (I believe) a very classic problem with memory allocation using "new". Here is the piece of code I use:

float * _normals = NULL;
try{

    _normals = new float[_x*_y*_z*3];
}catch(System::Exception ^ e){

    Windows::Forms::MessageBox::Show("Normals:\n" + e->Message);

    if(e->InnerException != nullptr && e->InnerException->Message != nullptr)
        Windows::Forms::MessageBox::Show("Details:\n" + e->InnerException->Message);

    _file->Close();

return 0;
}

I don't know if you can tell from this piece of code but this is a mixed of managed and unmanaged code program. I don't know if that matters.

Now, when I run this code and try to allocate, say, 256*256*128*3 floats it runs normally. When I go with 492*492*442 floats then it throws a "External component has thrown an exception" exception. This is around 1.2GB, right. My system has 6GB of ram and free around 3GB. Can you tell the problem from this information? Can I handle it? I read somewhere about program memory space. Maybe the program memory is not enough?(I don't know anything around that matter, If you can enlighten me)

Please ask if you need more information.

Thank you in advance

Was it helpful?

Solution

Address space for a 32-bit Windows program (Windows is implied by C++-CLI) running on a 64-bit operating system is either

  • 2 GB by default
  • 4 GB if linked with /LARGEADDRESSAWARE. This flag can also be added later by editbin.

Your problem is address space fragmentation. Just because you've only allocated, say 100MB, doesn't mean that you can allocate another 1.9GB chunk in a 2GB address space. Your new allocation needs to have contiguous addresses.

If, for example, a DLL used by your non-LAA process had a load-address at 0x40000000, then you could allocate a 1GB block below it, or an almost-1GB block above it, but you could never allocate a single block larger than 1GB.

The easiest solution is to compile as 64-bit. Even though the address space will still be fragmented, the open spaces between allocations will be much larger and not cause you problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top