我目前正在研究一次加载和转换多个图像的管道。由于这会同时发生在许 multimap 像上 (1440),因此内存占用量非常大。因此,我尝试实现基于 setrlimit 的内存管理系统,但它似乎不会影响生成的线程 (std::thread),因为它们会愉快地忽略该限制——我知道这一点是因为在线程函数 - 并最终导致我的程序被终止。这是我用来设置限制的代码:
void setMemoryLimit(std::uint64_t bytes)
{
struct rlimit limit;
getrlimit(RLIMIT_AS, &limit);
if(bytes <= limit.rlim_max)
{
limit.rlim_cur = bytes;
std::cout << "New memory limit: " << limit.rlim_cur << " bytes" << std::endl;
}
else
{
limit.rlim_cur = limit.rlim_max;
std::cout << "WARNING: Memory limit couldn't be set to " << bytes << " bytes" << std::endl;
std::cout << "New memory limit: " << limit.rlim_cur << " bytes" << std::endl;
}
if(setrlimit(RLIMIT_AS, &limit) != 0)
std::perror("WARNING: memory limit couldn't be set:");
// included for debugging purposes
struct rlimit tmp;
getrlimit(RLIMIT_AS, &tmp);
std::cout << "Tmp limit: " << tmp.rlim_cur << " bytes" << std::endl; // prints the correct limit
}
我正在使用 Linux。手册页指出 setrlimit 会影响整个过程,所以我有点不知道为什么线程似乎没有受到影响。
编辑:顺便说一下,上面的函数是在 main() 的最开始调用的。
最佳答案
这个问题很难找到,因为它由两个完全独立的部分组成:
我的可执行文件是使用 -fomit-frame-pointer 编译的。这将导致重置限制。请参阅以下示例:
/* rlimit.cpp */ #include <iostream> #include <thread> #include <vector> #include <sys/resource.h> class A { public: void foo() { struct rlimit limit; getrlimit(RLIMIT_AS, &limit); std::cout << "Limit: " << limit.rlim_cur << std::endl; } }; int main() { struct rlimit limit; limit.rlim_cur = 500 * 1024 * 1024; setrlimit(RLIMIT_AS, &limit); std::cout << "Limit: " << limit.rlim_cur << std::endl; std::vector<std::thread> t; for(int i = 0; i < 5; i++) { A a; t.push_back(std::thread(&A::foo, &a)); } for(auto thread : t) thread.join(); return 0; }
输出:
> g++ -std=c++11 -pthread -fomit-frame-pointer rlimit.cpp -o limit > ./limit Limit: 524288000 Limit: 18446744073709551615 Limit: 18446744073709551615 Limit: 18446744073709551615 Limit: 18446744073709551615 Limit: 18446744073709551615 > g++ -std=c++11 -pthread rlimit.cpp -o limit > ./limit Limit: 524288000 Limit: 524288000 Limit: 524288000 Limit: 524288000 Limit: 524288000 Limit: 524288000
对于图像处理部分,我使用 OpenCL。显然 NVIDIA 的实现调用了 setrlimit 并将限制推到 rlim_max。
关于c++ - setrlimit() 不影响生成的 std::threads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23171165/