如何在Visual Studio中为我的应用程序分配所有可用内存?

时间:2021-11-06 20:49:53

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.

我想在我的基于Windows的软件中渲染4百万个三角形,该软件是用Visual Studio C ++ 2010(在发布模式下构建)编写的。当我渲染390万个三角形时,软件消耗的总RAM内存为400MB。但是当我尝试渲染4百万个三角形(仅多100K)时,系统会给我一个错误。

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?

其实我没有得到的是,我有8 GB RAM内存,(但在32位XP窗口= 3.2GB内存),软件只保留400MB,可用内存超过1 GB,但当我尝试只渲染100K三角形,它给了我一个错误。为什么它给我一个错误?因为它仍有超过1 GB的可用RAM内存?

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.

无论如何要解决这个问题,如何为我的应用程序分配所有可用内存?由于这个问题,我必须在软件中限制只是为了渲染390万个三角形,这并不好。

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?

还有一个问题在于,c ++“new”运算符用于内存分配给我错误,c“malloc”运算符怎么样?可以“malloc”解决这个问题,这两者之间有什么不同?

Please guide me. Thanks.

请指导我。谢谢。

Update # 1:

更新#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.

我已经尝试了很多,修改代码,删除内存泄漏等,但我不能分配超过4百万的内存。它不可能将我的整个代码更改为“向量”。我不能改成“向量”,我现在必须用“新”来坚持我自己的数据结构。以下是我想分配以呈现1个对象的指针。

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;

3 个解决方案

#1


12  

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.

它是Windows编程的伟大神话之一,一个进程永远不会耗尽RAM。 Windows是一个按需分页的虚拟内存操作系统,如果一个进程需要更多的RAM,那么操作系统会通过分页其他进程拥有的其他内存页面来腾出空间。或者流程本身,将页面交换掉一段时间没用过的页面。

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.

任务管理器使用其默认设置报告进程的内存使用情况,这一点鼓舞了这一神话。它显示了工作集,即RAM中进程的实际字节数。一个值,通常远小于进程分配的虚拟内存量。当OOM无法再分配虚拟内存时,进程就会在OOM上消失。 Taskmgr中的另一个统计信息,VM大小值。而且它通常不会因为所有的VM被使用而死亡,但因为没有留下足够大的洞。 SysInternals的VMMap实用程序是查看进程如何使用其地址空间的好方法。

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.

获得更大的虚拟内存地址空间需要进行相当彻底的改革。虽然今天很容易,但只需将x64作为平台目标。 64位进程具有大量可用的地址空间,仅受页面文件的最大大小限制。通过使用/ LARGEADDRESSAWARE链接器选项,只要您可以指望实际在64位操作系统上运行,您就可以在32位模式下跛行。这样可以在64位操作系统上将VM大小从2 GB增加到4 GB。

#2


2  

For one of the questions:

对于其中一个问题:

is there any diffirence between these two?

这两者之间有什么不同吗?

the different between new and malloc is as follows:

new和malloc之间的区别如下:

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

    malloc用于C,malloc分配未初始化的内存。分配的内存必须免费释放。

  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.

    new通过调用相应的构造函数初始化分配的内存。分配有new的内存应该与delete一起释放(调用析构函数)。您不需要给出内存块的大小以释放分配的内存。

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.

目前尚不清楚new和malloc是否根据标准相关(它取决于特定编译器是否使用malloc实现new),因此只需用malloc替换new即可解决或不解决该问题。

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.).

从您显示的代码中,很难发现错误原因。您可以尝试用向量替换动态数组,看看它是否能解决您的问题。同时,您可以使用valgrind检查代码中是否有内存泄漏(如果您可以以某种方式将代码移植到带有makefile的Linux,因为很遗憾,valgrind在Windows上不可用)。

#3


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?

还有一个问题在于,c ++“new”运算符用于内存分配给我>错误,c“malloc”运算符怎么样?可以“malloc”解决这个问题,这两者之间有什么不同吗?

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.

malloc和new之间存在差异,例如,new会初始化你的内存并自动调用类的构造函数。或者初始化它们是否是原始类型(例如float,int,char等)。还应使用调用析构函数的delete关键字删除new分配的内存。

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.

C的malloc()以及Visual Studio中的新运算符在内部调用HeapAlloc()。如果所需的内存太大,或者在进程之间共享,HeapAlloc()会调用VirtualAlloc()。因此,它不一定能解决您的问题。事实上,如果你使用C ++坚持使用新的。

#1


12  

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.

它是Windows编程的伟大神话之一,一个进程永远不会耗尽RAM。 Windows是一个按需分页的虚拟内存操作系统,如果一个进程需要更多的RAM,那么操作系统会通过分页其他进程拥有的其他内存页面来腾出空间。或者流程本身,将页面交换掉一段时间没用过的页面。

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.

任务管理器使用其默认设置报告进程的内存使用情况,这一点鼓舞了这一神话。它显示了工作集,即RAM中进程的实际字节数。一个值,通常远小于进程分配的虚拟内存量。当OOM无法再分配虚拟内存时,进程就会在OOM上消失。 Taskmgr中的另一个统计信息,VM大小值。而且它通常不会因为所有的VM被使用而死亡,但因为没有留下足够大的洞。 SysInternals的VMMap实用程序是查看进程如何使用其地址空间的好方法。

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.

获得更大的虚拟内存地址空间需要进行相当彻底的改革。虽然今天很容易,但只需将x64作为平台目标。 64位进程具有大量可用的地址空间,仅受页面文件的最大大小限制。通过使用/ LARGEADDRESSAWARE链接器选项,只要您可以指望实际在64位操作系统上运行,您就可以在32位模式下跛行。这样可以在64位操作系统上将VM大小从2 GB增加到4 GB。

#2


2  

For one of the questions:

对于其中一个问题:

is there any diffirence between these two?

这两者之间有什么不同吗?

the different between new and malloc is as follows:

new和malloc之间的区别如下:

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

    malloc用于C,malloc分配未初始化的内存。分配的内存必须免费释放。

  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.

    new通过调用相应的构造函数初始化分配的内存。分配有new的内存应该与delete一起释放(调用析构函数)。您不需要给出内存块的大小以释放分配的内存。

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.

目前尚不清楚new和malloc是否根据标准相关(它取决于特定编译器是否使用malloc实现new),因此只需用malloc替换new即可解决或不解决该问题。

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.).

从您显示的代码中,很难发现错误原因。您可以尝试用向量替换动态数组,看看它是否能解决您的问题。同时,您可以使用valgrind检查代码中是否有内存泄漏(如果您可以以某种方式将代码移植到带有makefile的Linux,因为很遗憾,valgrind在Windows上不可用)。

#3


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?

还有一个问题在于,c ++“new”运算符用于内存分配给我>错误,c“malloc”运算符怎么样?可以“malloc”解决这个问题,这两者之间有什么不同吗?

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.

malloc和new之间存在差异,例如,new会初始化你的内存并自动调用类的构造函数。或者初始化它们是否是原始类型(例如float,int,char等)。还应使用调用析构函数的delete关键字删除new分配的内存。

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.

C的malloc()以及Visual Studio中的新运算符在内部调用HeapAlloc()。如果所需的内存太大,或者在进程之间共享,HeapAlloc()会调用VirtualAlloc()。因此,它不一定能解决您的问题。事实上,如果你使用C ++坚持使用新的。