8

I want to render 4 millions triangles in my windows based software which is written in Visual Studio C++ 2010 (Build in Release Mode). When I render 3.9 millions triangles, the total RAM memory consumed by the software is 400MB. But when I try to render 4 millions triangles (just 100K more), the system gives me an error.

For Example:
Point *P = new (std::nothrow) Point[nb_triangles]; //==> "(std::nothrow)" is for catching the run time memory allocation error. (Point is X, Y, Z floats)
If(P == NULL)
   message("System can't allocate this much memory.");  // System gives me this error. It means the system can't reserve huge memory for this operation.

I have to allocate memory for vertices, face normals, vertex normals, etc.

Actually what I am not getting is, I have 8 GB RAM memory, (but in 32 bit XP windows = 3.2GB memory), and the software is just reserved 400MB, the free memory is more than 1 GB, but when I try to render just 100K triangles more, it gives me an error. Why it is giving me an error? because it still has more than 1 GB free RAM memory?

Is there anyway to fix this issue, how can I allocate all the available memory for my application ? Because of this issue, I have to make a limit in the software just for rendering 3.9 millions triangles and it is not good.

And one more question in my mind is, c++ "new" operator for memory allocation giving me error, how about c "malloc" operator ? can "malloc" fix this issue, is there any diffirence between these two?

Please guide me. Thanks.

Update # 1:

I have tried a lot, modify the code, remove memory leaks, etc, but I can not allocate memory more than 4 millions. Its not possible to change my whole code into "vector". I can't change into "vector", I have to stuck on my own data structure now with "new". Following are the pointers that I want to allocate in order to render 1 object.

P = new points[10000000]; // points is the class with 3 floats X, Y, Z;
N = new Norm[10000000]; // Norm is the class with 3 floats X, Y, Z;
V = new vNorm[10000000]; // vNorm is the class with 3 floats X, Y, Z;
T = new Tri[10000000]; // Tri is the class with 3 integers v1, v2, v3;
12
  • 2
    Are you running a Release build? Have you tried running the program directly from Explorer (ie outside of Visual Studio)?
    – paddy
    Commented Mar 25, 2013 at 1:49
  • yes of course, I am talking about release mode, after build, instal, and tried in other PCs too.
    – maxpayne
    Commented Mar 25, 2013 at 1:51
  • 2
    Are you sure you aren't experiencing memory leaks or address space fragmentation? Commented Mar 25, 2013 at 1:54
  • 2
    @AlexeyFrunze I don't think that's significant. If you allocate chunks of 64MB or thereabouts, the cost of maintaining a few extra pointers is nothing. The point is to reduce sensitivity to fragmented memory. You can't just ask the OS for hundreds of megabytes of contiguous memory and expect to get it.
    – paddy
    Commented Mar 25, 2013 at 3:05
  • 2
    You can fix it by increasing the paging file to 4xRAM, then by using VirtualAlloc and also getting a better (and 64bit) OS for your system... XP is a terrible memory conservationist. In any case, you won't get any contiguous memory block larger than 1.5GB, since, as others said, it's just not contiguous in the system itself and at some point it needs more memory to just track fragmentation than there actually is. Otherwise, just limp along... hoping you'll get a single block of contiguous memory on a super-outdated x86 setup without using a crutch for astronauts! Commented Mar 27, 2013 at 7:24

4 Answers 4

16

It is one of the Great Myths of Windows programming, a process can never run out of RAM. Windows is a demand-paged virtual memory operating system, if a process needs more RAM then the operating system makes room by paging out other memory pages, owned by other processes. Or the process itself, swapping pages out that haven't been used for a while.

That myth is encouraged by the way Task Manager reports memory usage for a process with its default settings. It shows working set, the actual number of bytes of the process that are in RAM. A value that's usually much smaller than the amount of virtual memory allocated by the process. A process dies on OOM when it can't allocate virtual memory anymore. Another statistic in Taskmgr, the VM size value. And it usually dies not because all VM was used but because there isn't a hole left that's big enough. The SysInternals' VMMap utility is a good way to see how a process uses its address space.

Getting a larger virtual memory address space requires a pretty fundamental overhaul. Albeit that it is easy today, just target x64 as the platform target. A 64-bit process has massive amounts of address space available, limited only by the maximum size of the paging file. You could limp along in 32-bit mode, as long as you can count on actually running on a 64-bit operating system, by using the /LARGEADDRESSAWARE linker option. Which increases the VM size from 2 GB to 4 GB on a 64-bit operating system.

3
  • Hans, isn't the very nature of your last statement, the limitation of the 32bit address space, wood behind the array that you can run out of memory simply by exhausting the manageable VM size to its limit (given in 64bit land thats a tall order, but anyone running a remotely-taxed Tomcat server in a 32bit Java VM has seen it up-close and personal, one of the reasons we ended up requiring a 64bit Java VM for our project at work). Looking back at the first claim, I see it is specifically tagged "a process can never run out of RAM", and I concur to that, but you can run out of memory.
    – WhozCraig
    Commented Mar 25, 2013 at 2:15
  • 1
    Let's not go there, the term "memory" is entirely too vague. I specifically addressed the OP's assumption that he ran out of RAM. Commented Mar 25, 2013 at 2:19
  • I agree. I was so happy when 64bit was adopted at work I practically danced a jig (not kidding).
    – WhozCraig
    Commented Mar 25, 2013 at 2:22
2

For one of the questions:

is there any diffirence between these two?

the different between new and malloc is as follows:

  1. malloc is used in C, malloc allocates uninitialized memory. The allocated memory has to be released with free.

  2. new initializes the allocated memory by calling the corresponding constructor. Memory allocated with new should be released with delete (which calls the destructor). You don't need to give the size of memory block in order to release the allocated memory.

It is not clear whether new and malloc are related according to the standard (it depends on whether a specific compiler implements new using malloc or not), so the issue may or may not be resolved by simply replacing new with malloc.

From the code you showed, it is difficult to spot the cause of error. You may try to replace the dynamic array with vector to see if it solves your problem. Meanwhile, you may use valgrind to check whether you have memory leak in your code (if you can somehow port your code to Linux with makefiles since unfortunately valgrind is not available on Windows.).

5
  • Valgrind does not exist on Windows. I so miss it :-( Commented Mar 25, 2013 at 2:04
  • yup.. it is not supported windows. Thanks.
    – maxpayne
    Commented Mar 25, 2013 at 2:17
  • It doesn't, indeed, which is a true-bummer. WinDbg will give you everything (nearly) Valgrind will, but you really gotta want it, and work for it.
    – WhozCraig
    Commented Mar 25, 2013 at 2:19
  • the important question is, how can I allocate a huge heap of memory, approximate 10 millions for each pointer. Please check my updated question part. when I allocate 5 millions memory for each pointer, total memory consumed by my software in 400 MB, still more than 1 GB free in the system. I can't allocate more than this number. How can I fix it?
    – maxpayne
    Commented Mar 27, 2013 at 7:13
  • @furqan one option is to break up the allocation into multiple, smaller allocations. Even another option is to use the concept of "memory pools". You can even overload operator new and have the allocation go to your own custom-managed memory pools. Memory pools can increase performance in graphics applications that require a lot of allocation and deallocation. But I'm guessing you already solved your problem sometime in the last 44 months :@)
    – Kyle Sweet
    Commented Dec 2, 2016 at 19:16
2

And one more question in my mind is, c++ "new" operator for memory allocation giving me >error, how about c "malloc" operator ? can "malloc" fix this issue, is there any diffirence >between these two?

There are differences between malloc and new, for example, new will initialize your memory and call the constructor of the class automatically. Or initialize if they are primitive types(such as float, int, char etc). Also the memory allocated by new should be deleted with the delete keyword which calls the destructor.

C's malloc() as well as new operator in Visual Studio internally call HeapAlloc(). HeapAlloc() calls VirtualAlloc() if the memory required is too large, or is shared between processes. So, it will not necessarily fix your issue. Infact if you are using C++ stick to using new.

5
  • "call the constructor of the class automatically" - well new can be also used with primitive types. You might as well include what happens when you use it on them.
    – Shoe
    Commented Mar 25, 2013 at 2:39
  • @Jueecy yes.. And i have, it initializes the newly allocated memory. For example in primitive types it initializes to 0 for integers and characters, 0.00000 for floats and doubles. Commented Mar 25, 2013 at 2:41
  • Ok, but it doesn't call any constructor or destructor for them, because they are not classes. Do you see what I mean?
    – Shoe
    Commented Mar 25, 2013 at 2:45
  • Ah you wanted it in the answer - I thought it was clear enough @Jueecy cool, fixed it Commented Mar 25, 2013 at 2:46
  • Yup, something like that, I was hoping for something along the lines of "new will initialize your memory and, if it's a class, call the constructor automatically" but that fine anyway. +1.
    – Shoe
    Commented Mar 25, 2013 at 2:48
0
P = new points[10000000]; // points is the class with 3 floats X, Y, Z;
N = new Norm[10000000]; // Norm is the class with 3 floats X, Y, Z;
V = new vNorm[10000000]; // vNorm is the class with 3 floats X, Y, Z;
T = new Tri[10000000]; // Tri is the class with 3 integers v1, v2, v3;

This code allocates 30M vectors of 3 floats each, so that's 30M * 3 * 4 = 360MB of data, plus the 10M vectors of 3 integers each, 10M * 3 * 2 = 60MB of data.

That generally should work. And to prove to yourself that it works, you should put this code in a stand alone project. You must start from what works. And it works indeed. Just try it:

// main.cpp

struct Vector3 { float x, y, z; }
struct Tri { int v1, v2, v3; }

int main() {
  const int N = 10000000;
  new Vector3[N]; // no need to assign to variables in this simple test
  new Vector3[N];
  new Vector3[N];
  new Tri[N];
  // At this point, all four arrays are dynamically allocated although
  // inaccessible as we don't have any variables to work with. That's fine
  // for what we want to show: that the allocations do succeed. If they won't,
  // the code will "crash", i.e. abort via std::terminate.

  // No need to deallocate - it's a waste of time. The process is exiting,
  // the memory will be freed no matter what you do at this point. The OS
  // is taking over
}

It'll work fine if you have > 2GB of RAM on your system. I did try on Windows XP SP 6 (32 bit), in a VM, with a slightly newer MSVC (2017). The MSVC version won't make much difference in that respect, barring some horrible bug in MSVC 2010 (something as fundamental as array new wouldn't be so badly broken, I don't think).

As shown, new never returns nullptr! If new returns, it returns a valid value. If it fails to allocate, it won't return, so no problem there.

How does it not return? It throws an exception, and the program terminates with an error indication, since that exception will propagate unhandled.

Its not possible to change my whole code into "vector"

Huh? It's fairly trivial. The vectors can be indexed just as C-style arrays, so the code that uses the vectors will remain the same.

Here's how:

// main.cpp
#include <algorithm>
#include <vector>
#define CPP20 (__cplusplus > 201703L)
#if CPP20
#  include <ranges>
#endif

struct Vector3 { float x, y, z; }
struct Tri { int v1, v2, v3; }

int main() {
  const int N = 10000000;
  auto P = std::vector<Vector3>(N);
  auto N = std::vector<Vector3>(N);
  auto V = std::vector<Vector3>(N);
  auto T = std::vector<Tri>(N);

  // awful C code as an example
  for (int i = 0; i < N; ++i)
    N[i] = P[i];

  // more idiomatic C++ code
  std::copy(P.begin(), P.end(), N.begin());

  // even more idiomatic on C++20
  #if CPP20
  std::ranges::copy(P, N);
  #endif

  // KISS code
  P = N; // you can't do that with raw C arrays!

  // C++20 KISS with superpowers
  std::ranges::copy(P | std::views::reverse, N)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.