快速成功退出 C++分配了很多对象

发布于 2024-10-13 09:06:24 字数 346 浏览 9 评论 0原文

我正在寻找一种方法来快速退出已使用 C++ 类在内存中分配大量结构的 C++。程序正确完成,但在程序中最后“返回”后,所有自动析构函数都会启动。问题是程序通过大量 C++ 类结构分配了大约 15GB 内存,并且此自动析构过程需要大约还需要 1 个小时才能完成,因为它会遍历所有结构 - 尽管我并不关心结果。截至目前,该程序只用了1个小时就完成了任务。我想返回到操作系统并让它执行正常的批发进程分配删除 - 这非常快。我一直在清理阶段通过手动终止进程来做到这一点 - 但我正在寻找更好的程序解决方案。

我想向操作系统返回成功,但不关心保留任何内存内容。程序在正常处理过程中确实执行了大量的动态分配/释放,因此它不仅仅是简单的堆管理。

有什么意见吗?

I'm looking for a way to quickly exit a C++ that has allocated a lot of structures in memory using C++ classes. The program finishes correctly, but after the final "return" in the program, all of the auto-destructors kick in. The problem is the program has allocated about 15GB of memory through lots of C++ class structures, and this auto-destruct process takes about 1 more hour itself to complete as it walks through all of the structures - even though I don't care about the results. The program only took 1 hour to complete the task up to this point. I would like to just return to the OS and let it do its normal wholesale process allocation deletion - which is very quick. I've been doing this by manually killing the process during the cleanup stage - but am looking for a better programic solution.

I would like to return a success to the OS, but don't care to keep any of the memory content. The program does perform a lot of dynamic allocation/deallocation during the normal processing, so it's not just simple heap management.

Any opinions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

踏雪无痕 2024-10-20 09:06:24

在标准 C++ 中,只有 abort(),但进程会向操作系统返回失败。

在许多平台(Unix、MS Windows)上,您可以使用 _exit() 退出程序而不运行清理和析构函数。

In Standard C++ you only have abort(), but that has the process return failure to the OS.

On many platforms (Unix, MS Windows) you can use _exit() to exit the program without running cleanup and destructors.

清风不识月 2024-10-20 09:06:24

如果您的编译器正在寻找 C++0x std::quick_exit已经支持它(g++-4.4.5 支持)。

C++0x std::quick_exit is what you are looking for if your compiler already supports it (g++-4.4.5 does).

能怎样 2024-10-20 09:06:24

如果 15 GB 内存被分配给相当少量的类,您可以覆盖这些类的删除操作符。只需将调用传递给标准删除,但设置一个全局标志,如果设置了该标志,将使删除调用成为无操作。或者,如果程序的逻辑是在构建数据结构的正常过程中不会删除这些对象,则您可以在所有情况下忽略这些类的删除。

If the 15 GB of memory is being allocated to a reasonably small number of classes, you could override operator delete for those classes. Just pass the call to the standard delete, but set up a global flag that, if set, will make the call to delete a no-op. Or, if the logic of your program is such that these objects are not deleted in the normal course of building your data structures, you could simply ignore delete in all cases for these classes.

沫尐诺 2024-10-20 09:06:24

正如纳文所说,这不可能是内存释放的问题。我用进化算法编写了神经网络模拟,其中以小块和大块的形式分配和释放大量内存,这从来都不是一个主要问题。

As Naveen says, this can't be a matter of memory deallocation. I've written neural network simulations with evolutionary algorithms that where allocating and freed lots of memory in small and large chunks and this was never a major issue.

厌倦 2024-10-20 09:06:24

如果您有 C99 编译器,则可以使用 _Exit函数立即结束,无需调用全局对象析构函数或使用 atexit 注册的任何函数;是否刷新未写入的缓冲文件数据、关闭打开的流或删除临时文件是实现定义的(C99 §7.20.4.4)。

如果您使用的是 Windows,还可以使用 ExitProcess 达到同样的效果。

但是,正如其他人所说,除非您执行大量 I/O(写入日志文件等),否则您的析构函数实际上不应该花费一个小时来运行。我强烈建议您分析您的程序,看看时间都花在哪里了。

If you have a C99 compiler, you can use the _Exit function to end immediately without having global object destructors or any functions registered with atexit to be called; whether or not unwritten buffered file data is flushed, open streams are closed, or temporary files are removed is implementation-defined (C99 §7.20.4.4).

If you're on Windows, you can also use ExitProcess to achieve the same effect.

But, as others have said, your destructors should really not be taking an hour to run unless you're doing a fair amount of I/O (writing log files, etc.). I strongly, strongly recommend you profile your program to see where the time is spent.

祁梦 2024-10-20 09:06:24

可能的策略取决于 main 中直接可见的对象数量(您可以通过这些对象访问 15GB 数据)以及这些对象是在 main 本地还是静态分配的。

如果对 15GB 数据的所有访问都是通过 main 中的本地对象进行的,那么您只需替换 return 0; 即可代码>main 和exit(0);
exit 将终止您的应用程序并触发静态分配变量的清理,但不会局部变量。

如果通过一些静态分配的变量访问数据,您可以将它们转换为动态分配内存的指针(或引用)并故意泄漏该内存。

The possible strategies depend on the number of objects that are directly visible in main through which you access the 15GB of data and if these are local to main or statically allocated.

If all access to the 15GB of data is through local objects in main, then you can simply replace the return 0; at the end of main with exit(0);.
exit will terminate your application and trigger cleanup of statically allocated variables, but not of local variables.

If the data is accessed through a handful of statically allocated variables, you could turn them into pointers (or references) to dynamically allocated memory and deliberately leak that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文