简体   繁体   English

bad_alloc详细?

[英]bad_alloc in detail?

I work on a game project. 我从事游戏项目。 Recently, we run into a problem which we catch a "bad_alloc" exception when we load/unload different scenes for about three times. 最近,我们遇到了一个问题,当我们加载/卸载不同的场景大约3次时,会捕获到一个“ bad_alloc”异常。

Eachtime we load a scene, we first load the compressed .zip folder into the memory and then extract game objects from it. 每次加载场景时,我们首先将压缩的.zip文件夹加载到内存中,然后从中提取游戏对象。 Since we don't have any memory profiler tool, I simply use the performance graph of Process Explorer to see what is going on: 由于我们没有任何内存探查器工具,因此我仅使用Process Explorer的性能图来查看发生了什么:

  1. Run the game ==> ~620 MB 运行游戏==>〜620 MB
  2. Load the 1st scene ==> ~1.1 GB 加载第一个场景==>〜1.1 GB
  3. Unload it ==> ~620 MB 卸载==>〜620 MB
  4. Load the 2nd scene ==> ~1.1 GB 加载第二个场景==>〜1.1 GB
  5. Unload it ==> ~620 MB 卸载==>〜620 MB
  6. Load the 3rd scene ==> bad_alloc 加载第三个场景==> bad_alloc

I trace down to the new operator of each load. 我追溯到每个负载的新运算符。 The loading of the compressed folder costs 100~120 MB memory itself, and the bad_alloc is throw from it at the 3rd load. 压缩文件夹的加载本身需要100〜120 MB的内存,并且在第3次加载时会从bad_alloc抛出该内存。

Our platform is installed with 2GB physics memory. 我们的平台安装了2GB物理内存。 It seems that we do not left anything behind after unloading the scene. 卸载场景后,我们似乎没有留下任何东西。 Each compressed folder is about the same size. 每个压缩文件夹的大小大约相同。 However, the new operator of the 3rd load just doesn't work. 但是,第3次加载的新运算符不起作用。

I'm wondering, what's the possible meaning of this bad_alloc? 我想知道这个bad_alloc的可能含义是什么? Out of memory? 记不清? or simply can't allocate a contiguous block of the required size? 还是根本无法分配所需大小的连续块? If it does have several possibility, how can I determine it? 如果确实有几种可能性,我该如何确定呢?

Any help is apppreciated, thanks! 感谢您的帮助,谢谢!

If you are allocating all that memory at a time (only one new ), probably the heap is too fragmented to find enough contiguous memory to allocate what you requested. 如果您一次分配所有内存(仅一个new ),则可能是堆太零散了,无法找到足够的连续内存来分配您所请求的内容。

That could be solved by allocating the new scene by parts. 可以通过按部分分配新场景来解决。

bad_alloc is thrown by new when the allocation could not be fulfilled. 当分配无法完成时, bad_allocnew抛出。 Likely, you've simply run out of memory. 可能您的内存已耗尽。 Typically, custom memory managers will also throw bad_alloc when they request cannot be fulfilled, but you said you aren't using any. 通常,自定义内存管理器在无法满足他们的请求时也会抛出bad_alloc ,但是您说您没有使用任何内存。 Run your code in a debugger to see where the exception originates from. 在调试器中运行代码以查看异常的来源。

By the way, that's a lot of memory. 顺便说一下,那是很多内存。 Most professional games require half to a quarter of what you're doing; 大多数专业游戏都需要您完成一半或四分之一的工作; look into ways of reducing memory usage. 研究减少内存使用的方法。 Another thing to watch out for is memory leaks; 需要注意的另一件事是内存泄漏。 if you never free memory, you'll eventually run out. 如果您从不释放内存,最终将用完。

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

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