简体   繁体   English

如果我的应用内存不足怎么办?

[英]What happens if my app is out of memory?

If my application is out of memory when i call new() i will get exception and malloc() i will get 0 pointer. 如果我的应用程序内存不足,当我调用new()时,我将获得异常,而malloc()将获得0指针。

But what if i call a method with some local variables? 但是,如果我调用带有局部变量的方法怎么办? They occupy memory too. 它们也占用内存。 Is there some way to reserve memory for "normal" variables? 有什么方法可以为“普通”变量保留内存吗? So that even though new() throws exception i can just catch it, fix stuff and still call methods like usual. 这样即使new()抛出异常,我也可以捕获它,修复问题并像往常一样调用方法。

Your data is allocated in one of three ways: 您的数据通过以下三种方式之一分配:

  • Statically allocated data (static members or globals) are allocated when the app starts up, which means that they're not really going to be a problem. 静态分配的数据(静态成员或全局变量)在应用程序启动时分配,这意味着它们并不是真正的问题。
  • Stack allocated data is allocated on the stack (surprise!) The stack is an area of memory that's set aside for local variables and function stackframes. 堆栈分配的数据分配在堆栈上(惊奇!)。堆栈是为局部变量和函数堆栈帧预留的内存区域。 If you run out of space there, it's undefined what happens. 如果空间用完了,将无法确定会发生什么。 Some implementations might detect it and give you an access violation/segmentation fault, and others will just make you overwrite heap data. 一些实现可能会检测到它,并给您访问冲突/分段错误,而其他实现只会使您覆盖堆数据。 In any case, there's no way to detect this, because in general, there's no way to handle it. 在任何情况下,都无法检测到此情况,因为通常没有办法处理它。 If you run out of stack space, there's just nothing you can do. 如果堆栈空间不足,则无能为力。 You can't even call a function, because that takes up stack space. 您甚至不能调用函数,因为这会占用堆栈空间。
  • Heap allocated memory is what you use when you call new/malloc. 堆分配的内存是您在调用new / malloc时使用的内存。 Here, you have a mechanism to detect out-of-memory situations, because you may be able to handle it. 在这里,您可以检测到内存不足的情况,因为您可以处理它。 (Instead of allocating 200mb, you might be able to make do with 100mb, and just swap the data out halfway through) (而不是分配200mb,您也许可以使用100mb,而只需在中途交换数据)

You generally shouldn't run out of stack space unless you perform some heavy recursion though. 但是,除非执行一些繁重的递归操作,否则通常不应该用完堆栈空间。

THe C++ language doesn't provide any mechanism for reserving memory for local variables. C ++语言不提供任何用于为局​​部变量保留内存的机制。 Your specific C++ implementation and/or operating system may provide some means of increasing the total stack size, but this is not normally necessary. 您的特定C ++实现和/或操作系统可以提供一些增加总堆栈大小的方法,但这通常不是必需的。

Note also that if a call to new does fail, there is probably very little you can practically do to recover from it. 还要注意,如果对new的调用确实失败了,从恢复中恢复实际上几乎没有什么可做的。 Many people (me included) no longer bother checking for new failure. 许多人(包括我在内)不再费心检查新的故障。

New allocates memory from the heap, but local vars are normally on the stack - which can overflow, but is less likely, depending on your platform. New从堆中分配内存,但是本地var通常在堆栈上-可能会溢出,但可能性较小,具体取决于您的平台。 Can you provide more details? 您能否提供更多详细信息?

The compiler knows how much of memory per stack you need. 编译器知道每个堆栈需要多少内存。 However, sufficiently high number of stacks (caused due to recursion) will crash your program -- there probably isn't another way to fix this. 但是,足够多的堆栈(由于递归导致)将使程序崩溃—可能没有其他方法可以解决此问题。

The standard has an interesting annexure called Implementation Quantities . 该标准有一个有趣的附件,称为“ 实施数量” This is non-normative (informative) and hence should not be treated as the absolute truth, but provides you with a fair idea. 这是非规范性的(信息性的),因此不应将其视为绝对真理,而应为您提供一个公正的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM