C++应用程序超出内存但不使用虚拟内存
我有一个应用程序,在代码的某些部分使用“new”分配内存并使用“delete”释放内存。 问题是,每当它超过系统的内存限制(比如 2GB)时,Windows 就会向该进程发送 Kill 信号。 我认为这不常见,因为它应该使用交换空间(我认为在 Windows 中它称为虚拟内存),对吧? 我的应用程序是用 C++/Visual Studio 编写的。
I have an application that allocates memory with 'new' and frees them with 'delete' in some parts of the code.
The problem is that whenever it exceeds the memory limit of the system (let's say 2GB), Windows sends a Kill signal to the process.
I think it is not usual since it should use the swap space(I think in windows it is called virtual memory), right?
My application is written in C++/Visual Studio.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是如何将一个进程的大小设置为 3GB< /a>;这是 32 位 Windows 应用程序可以拥有的绝对最大值。除此之外,您将需要使用 64 位版本的 Windows。
那是很多内存。也许您可以考虑将您的应用程序拆分为多个进程并在它们之间进行通信。
Here is how you can make it up to 3GB for a process; That is the absolute max you can have it for 32 bit windows apps. Any more than that and you are going to need to use a 64 bit version of windows.
That is a lot of memory. maybe you could consider splitting your app into multiple processes and communicating between them.
操作系统不会杀死您的应用程序,而是未处理的异常。您将需要使用 perfmon 检查您的应用程序,并观察这些计数器、工作集、虚拟字节、专用字节。当您的保留字节接近 2GB 时,您会遇到异常。所以你提交的字节和 RAM 字节要少得多。
这是一篇关于虚拟地址空间的好文章,包括已提交和保留
这个故事的寓意是,对于 32 位进程,当保留字节接近 2GB 时,不要尝试进行分配。
The OS doesn't kill your app, an unhandled exception does. You will want to examine your app with perfmon, and watch these counters, Working Set, Virtual Bytes, Private Bytes. You will get exceptions when your reserved bytes gets close to 2GB. So your committed bytes and RAM bytes are much less.
Here is a nice article on Virtual Address Space, including committed vs reserved.
The moral of the story, don't try to allocate when the reserved bytes gets close to 2GB, for a 32-bit process.
Windows 不使用信号。当内存不足时,您应该得到 std::badalloc 异常。当未被捕获时,将自动运行终止()函数。该异常在“输出”窗口中可见。
Windows doesn't use signals. You should get the std::badalloc exception when you run out of memory. Which, when uncaught, will automatically run the terminate() function. The exception is visible in the Output window.
我没有仔细研究过这一点,但您可能会在这里找到您想要的答案:
您可以在 c 或 c++ 中分配非常大的单个内存块(> 4GB)吗?
I haven't looked too closely at this, but you may find the answers you want here:
Can you allocate a very large single chunk of memory ( > 4GB ) in c or c++?