Pthreads - 高内存使用率

发布于 2024-12-02 03:40:33 字数 684 浏览 1 评论 0 原文

我正在用 C 编写一些东西,在 256Mb 系统上的 Linux 中创建大量 Pthread。我通常有+200Mb 的免费空间。

当我使用少量线程运行该程序时,它可以工作,但是一旦我让它创建大约 100 个线程,它就会出现错误,因为系统内存不足。我做了几次测试,每个线程使用了近 2Mb。线程的堆栈大小设置为 16Kb。

我用来创建每个线程的代码:

pthread_attr_t attr;
pthread_attr_init(&attr);
size_t stacksize;
stacksize = (double) 16*1024;
int res = pthread_attr_setstacksize (&attr, stacksize);
int res2 = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (res != 0 || res2 != 0) {
    logs << "pthread_attr_XX: error "+int2string(res);
    exit(-1);
}
pthread_t id;
pthread_create(&id, &attr, &Class::thread_callback, &some_var);

这是正常的还是我遗漏了什么?谢谢。

I am programming something in C that creates a lot of Pthreads in Linux on a 256Mb system. I usually have +200Mb free.

When I run the program with a low amount of threads it works, but once I make it create around 100 threads it gives errors because the system runs out of memory. I did several tests and each threads use almost 2Mb. The stack size of the threads is set to 16Kb.

The code I use to create each thread:

pthread_attr_t attr;
pthread_attr_init(&attr);
size_t stacksize;
stacksize = (double) 16*1024;
int res = pthread_attr_setstacksize (&attr, stacksize);
int res2 = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (res != 0 || res2 != 0) {
    logs << "pthread_attr_XX: error "+int2string(res);
    exit(-1);
}
pthread_t id;
pthread_create(&id, &attr, &Class::thread_callback, &some_var);

Is it normal or am I missing something? Thanks.

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

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

发布评论

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

评论(6

眼前雾蒙蒙 2024-12-09 03:40:34

系统线程库并不是为了在内存非常有限的系统上支持大量线程而设计的。您需要使用为此目的设计的线程库,或者更好的是使用更少的线程。

The system threads library is just not designed to support large numbers of threads on systems with very limited memory. You need to either use a threading library designed for that purpose or, better, use fewer threads.

最丧也最甜 2024-12-09 03:40:34

是的,对于几乎任何操作系统来说,在这么多线程上阻塞和死掉是相当常见的。这不是操作系统制造商(Linux 或其他)给予太多关注的问题,因为很少有系统拥有那么多 CPU,因此您的代码在 100 个线程下的执行速度可能不会比在 8 个线程(或者无论您使用多少个 CPU)下执行得快得多。有)。

我认为你需要做的是某种线程池。我还怀疑,如果您确实需要那么多线程,则此页面上的 ulimit 答案会很有用,但正如我所说,我认为在大多数情况下,许多线程不会给您带来太多好处,并且如果您在程序中而不是在程序中修复它,系统,它将使其更加便携。

Yeah, it's fairly common for pretty much any operating system to choke and die on that many threads. It's not a problem that OS makers (Linux or otherwise) have given much attention to since very few systems have that many CPUs, so your code wouldn't likely executing much faster with 100 threads than it would with 8 (or however many CPUs you have).

I think what you need to do is some sort of thread pooling. I also suspect the ulimit answer on this page would be useful if you really need that many threads, but like I said, I don't think that many threads gets you much in most circumstances and if you fix it in your program instead of in the system, it'll make it more portable.

伪装你 2024-12-09 03:40:34

为什么 stacksize = (double) 16*1024; ?它是整数。

尝试设置为32或64 KB,因为16*1024可能不允许;堆栈中也可以有 TLS(即使您不使用 TLS;库也使用 TLS,包括 libc)。

因此,将行更改为

stacksize = 64*1024; 

并检查消耗了多少内存。

Why stacksize = (double) 16*1024; ? It is integer.

Try to set it to 32 or 64 kilobytes, because 16*1024 may be not allowed; there can be also TLS in the stack (even you doesn't use TLS; libraries do, including libc).

So, change the line to

stacksize = 64*1024; 

and check how much memory is consumed.

慢慢从新开始 2024-12-09 03:40:34

如果您希望降低开销,请考虑用户空间线程技术,例如纤程、协作任务管理等。

http://en.wikipedia.org/wiki/Fiber_(computer_science)

http://www.evanjones.ca/software/threading.html

GNU 可移植线程:

http://www.gnu.org/software/pth/

Boost C++ 协同例程:

http://www.boost.org/doc/libs/1_60_0/libs/coroutine/doc/html/index.html

仅 Windows 光纤仅供参考:

http://msdn.microsoft.com/en-us/library/ms682661(v=vs.85).aspx

有关更多示例实现,请参阅 Wikipedia 链接。

If you want lower overheads consider user-space threading technologies such as fibers, ala co-operative task management.

http://en.wikipedia.org/wiki/Fiber_(computer_science)

http://www.evanjones.ca/software/threading.html

GNU Portable threads:

http://www.gnu.org/software/pth/

Boost C++ co-routines:

http://www.boost.org/doc/libs/1_60_0/libs/coroutine/doc/html/index.html

Windows-only fibers as an FYI:

http://msdn.microsoft.com/en-us/library/ms682661(v=vs.85).aspx

See the Wikipedia link for more example implementations.

阳光①夏 2024-12-09 03:40:34

可能是这个原因:

“(识别泄漏

如果您创建一个可连接线程但忘记连接它,则其资源或私有内存始终保留在进程空间中并且永远不会回收。始终连接可连接线程;通过不连接它们,你面临着严重内存泄漏的风险。)”

在这里阅读:
http://www.ibm.com/developerworks/library/l-memory-泄漏/

May be this is the reason:

"(Recognizing leaks

If you create a joinable thread but forget to join it, its resources or private memory are always kept in the process space and never reclaimed. Always join the joinable threads; by not joining them, you risk serious memory leaks.)"

Read it here:
http://www.ibm.com/developerworks/library/l-memory-leaks/

蓝眸 2024-12-09 03:40:33

不确定它是否有帮助,但尝试在创建第一个线程之前使用 RLIMIT_STACK 调用 setrlimit 以将堆栈大小限制为 16k。

Not sure it will help, but try calling setrlimit with RLIMIT_STACK to limit the stack size to 16k before creating your first thread.

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