简体   繁体   English

大量计算后减少 C++ 程序的堆大小

[英]Reducing the heap size of a C++ program after large calculation

Consider an MPI application based on two steps which we shall call load and globalReduce .考虑一个基于两个步骤的 MPI 应用程序,我们将其称为loadglobalReduce Just for simplicity the software is being described as such yet there is a lot more going on, so it is not purely a Map/Reduce problem.只是为了简单起见,软件被描述为这样,但还有很多事情要做,所以它不仅仅是一个 Map/Reduce 问题。

During the load step, all ranks in each given node are queued so that one and only one rank has full access to all memory of the node.加载步骤期间,每个给定节点中的所有rank 都排队,因此只有一个 rank 可以完全访问节点的所有内存。 The reason for this design arises from the fact that during the load stage, there is a set of large IO blocks being read, and they all need to be loaded in memory before a local reduction can take place.这种设计的原因是因为在加载阶段,有一组大的 IO 块被读取,它们都需要加载到内存中,然后才能进行本地缩减 We shall call the result of this local reduction a named variable myRankVector .我们将把这个局部归约的结果称为命名变量myRankVector Once the myRankVector variable is obtained, the IO blocks are released.一旦获得myRankVector变量,IO 块就会被释放。 The variable myRankVector itself uses little memory, so while during its creation the node can be using all the memory, after completion the rank only needs to use 2-3 GB to hold myRankVector .变量myRankVector本身使用很少的内存,因此在创建期间节点可以使用所有内存,完成后等级只需要使用 2-3 GB 来保存myRankVector

During the globalReduce stage in the node, it is expected all ranks in the node had loaded their corresponding globalReduce .在节点的globalReduce阶段,期望节点中的所有rank都加载了它们对应的globalReduce

So here is my problem, while I have ensured that there are absolutely not any memory leaks (I program using shared pointers, I double checked with Valgrind, etc.), I am positive that the heap remains expanded even after all the destructors have released the IO blocks.所以这是我的问题,虽然我已经确保绝对没有任何内存泄漏(我使用共享指针编程,我使用 Valgrind 进行了双重检查等),但我确信即使在所有析构函数都释放后堆仍然保持扩展IO 块。 When the next rank in the queue comes to do its job, it starts asking for lots of memory just as the previous rank did and of course the program gets the Linux kill yielding "Out of memory: Kill process xxx (xxxxxxxx) score xxxx or sacrifice child".当队列中的下一个等级开始执行其工作时,它开始像前一个等级一样要求大量内存,当然程序会导致 Linux 终止,产生“内存不足:终止进程 xxx (xxxxxxxx) 得分 xxxx 或牺牲孩子”。 It is clear why this is the case, the second rank in the queue wants to use all the memory yet the first rank remains with a large heap.很明显为什么会这样,队列中的第二列想要使用所有内存,而第一列仍然有一个大堆。

So, after the setting the context of this question: is there a way to manually reduce the heap size in C++ to truly release memory not being used?那么,在设置这个问题的上下文之后:有没有办法在 C++ 中手动减小堆大小以真正释放未使用的内存?

Thanks.谢谢。

Heaps are implemented using mmap on linux, and you would need to use your own heap, which you can dispose and munmap completely.堆是在 linux 上使用 mmap 实现的,您需要使用自己的堆,您可以完全处置和 munmap。

The munmap would free the space required. munmap将释放所需的空间。

Look at code in boost : pool for an implementation which would allow you to manage the underlying heaps independently.查看boost : pool中的代码以获取允许您独立管理底层堆的实现。

In my experience, it is very difficult to manage std containers with custom allocators, as they are class derived, rather than instance derived.根据我的经验,使用自定义分配器管理std容器非常困难,因为它们是类派生的,而不是实例派生的。

So, after the setting the context of this question: is there a way to manually reduce the heap size in C++ to truly release memory not being used?那么,在设置这个问题的上下文之后:有没有办法在 C++ 中手动减小堆大小以真正释放未使用的内存?

That's operating system dependent, but most probably not possible.这取决于操作系统,但很可能不可能。

Most operating systems leave you with memory allocations you've done from a single process until that process is completely done and killed.大多数操作系统都会为您分配从单个进程完成的内存分配,直到该进程完全完成并终止。

Could shared memory solve your problem (even if you do not want to share this memory)?共享内存能否解决您的问题(即使您不想共享此内存)? You can allocate a block of shared memory in your "load" phase and unattach it after "myRankVector" is calculated.您可以在“加载”阶段分配一块共享内存,并在计算“myRankVector”后取消附加它。

(see shmget, shmat, shmdt, shmctl( ..., IPC_RMID, . ) ) (见 shmget, shmat, shmdt, shmctl( ..., IPC_RMID, . ) )

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

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