第一次创建线程时消耗多少内存? [英] How much memory does a thread consume when first created?

查看:146
本文介绍了第一次创建线程时消耗多少内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在应用程序中创建太多的线程不是你可能称为其他运行进程的好邻居,因为即使这些线程处于高效的休眠状态,也会消耗cpu和内存资源。 / p>

我感兴趣的是:睡眠线程消耗多少内存(win32平台)



理论上,我假设在1mb的范围内(因为这是默认的堆栈大小),但我确定它小于这个,但我不知道为什么



对此有任何帮助。



我想考虑引入一个线程池,我想了解多少内存我可以通过创建一个5个线程池比较20手动创建的线程)

解决方案

我有一个在线程使用量很大的服务器应用程序,它使用由客户设置的可配置线程池,并在至少一个网站它有1000多个线程,并且启动时它只使用50 MB。原因是Windows 为堆栈保留1MB(它映射其地址空间),但它不一定分配在物理内存中,只有其中的一小部分。如果堆栈增长超过页面错误生成和更多的物理内存分配。我不知道初始分配是什么,但我认为它等于系统的页面粒度(通常为64 KB)。当然,线程也会使用一些更多的内存为其他的东西,当创建(TLS,TSS等),但我的猜测的总数将是大约200 KB。并记住任何不经常使用的内存将被虚拟内存管理器卸载。


I understand that creating too many threads in an application isn't being what you might call a "good neighbour" to other running processes, since cpu and memory resources are consumed even if these threads are in an efficient sleeping state.

What I'm interested in is this: How much memory (win32 platform) is being consumed by a sleeping thread?

Theoretically, I'd assume somewhere in the region of 1mb (since this is the default stack size), but I'm pretty sure it's less than this, but I'm not sure why.

Any help on this will be appreciated.

(The reason I'm asking is that I'm considering introducing a thread-pool, and I'd like to understand how much memory I can save by creating a pool of 5 threads, compared to 20 manually created threads)

解决方案

I have a server application which is heavy in thread usage, it uses a configurable thread pool which is set up by the customer, and in at least one site it has 1000+ threads, and when started up it uses only 50 MB. The reason is that Windows reserves 1MB for the stack (it maps its address space), but it is not necessarily allocated in the physical memory, only a smaller part of it. If the stack grows more than that a page fault is generated and more physical memory is allocated. I don't know what the initial allocation is, but I would assume it's equal to the page granularity of the system (usually 64 KB). Of course, the thread would also use a little more memory for other things when created (TLS, TSS, etc), but my guess for the total would be about 200 KB. And bear in mind that any memory that is not frequently used would be unloaded by the virtual memory manager.

这篇关于第一次创建线程时消耗多少内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆