- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在研究一次加载和转换多个图像的管道。由于这会同时发生在许 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/
我正在尝试使用 setrlimit()限制流程花费的时间。但是,当我执行某些操作(如 printf())时,它似乎不起作用。 这是一个说明问题的测试程序: #include #include in
我的计算机接受每个进程 380 个线程。我尝试使用 settlimit () 增加到更大的数字,但我得到了预期的结果。 如何增加进程数? 以下代码不能正常工作: #include #
我正在尝试模拟 Linux 中进程的错误场景,即堆不足以在 C++ Linux 应用程序中分配内存。但是即使我使用“setrlimit”来减少进程可用的堆内存,堆内存仍然被成功分配。 struct r
我想限制 execv 生成的程序可以使用一定数量的内存和一定数量的 CPU 时间。例如,我想设置限制,比如程序不能超过 100MB 30 秒,程序不能使用 90% 或更多的 CPU 超过 20 秒。
我正在使用一个库,该库使用默认的 8MB 堆栈大小创建 pthread。是否有可能以编程方式减少库创建的线程的堆栈大小?我尝试在我的 main() 函数中使用 setrlimit(RLIMIT_STA
我写了一个简单的程序来将它的数据大小限制为 65Kb 并验证我正在分配一个超过 65Kb 的虚拟内存并且逻辑上如果我做的都是正确的(如下所示)malloc 调用应该失败,不是吗是吗? #include
关注此SO question和 this answer特别是,似乎在 printf 之后调用 setrlimit 使其无法正常工作。 示例代码如下: #include #include int m
我目前正在研究一次加载和转换多个图像的管道。由于这会同时发生在许 multimap 像上 (1440),因此内存占用量非常大。因此,我尝试实现基于 setrlimit 的内存管理系统,但它似乎不会影响
在下面给出的示例中,我尝试将堆栈大小设置为 1kb。 为什么现在可以在 foo() 中分配大小为 8kb 的整数数组? #include #include void foo(void); int
阅读有关如何为使用 gnu 编译的 c++ 应用程序增加堆栈大小的信息,在编译时,我了解到可以在程序开头使用 setrlimit 来完成。尽管如此,我找不到任何关于如何使用它以及在程序的哪一部分应用它
我可能遗漏了一些东西,也可能没有,但是 setrlimit 函数在 valgrind 下运行时一直失败 int main() { const struct rlimit file_li
我在 Ubuntu 中编写了一个 C++ 程序。在 main 函数中,我有这样的二维数组: int main() { unsigned long long int s[11000][100];
我在为各种程序编写音频插件的公司工作,目前我在 Os X Mavericks 和 Yosemite 上遇到以下逻辑问题。Logic 会扫描我们所有的插件,并且在某些时候它无法再访问这些文件。 errn
我编写了以下 C 程序来限制该程序可以创建的最大进程数(在 Linux 上)。该程序使用了setrlimit(),预计该程序最多可以创建4个进程。 // nproc.c #include #incl
以下男女同校给我一个 EINVAL 错误和 Node 崩溃: var posix = require('posix'); console.log(posix.getrlimit('nofile'));
我注意到在/etc/security/limits.conf 中,限制是基于每个用户(或每个组)配置的,例如: @faculty hard nproc 50 我假设是 setrlimit 完成设置限制
在 Linux(例如 Ubuntu 18.04 或其他)中,getrlimit() 和 setrlimit() 通过读取和写入 /etc/security/来工作限制.conf? 我试图了解 getr
我正在尝试使用 setrlimit 来限制我在 Linux 系统上的内存使用,以阻止我的进程使机器崩溃(我的代码使高性能集群上的节点崩溃,因为一个错误导致内存消耗超过 100 GiB)。我似乎找不到要
我是一名优秀的程序员,十分优秀!