Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么python线程会消耗这么多内存?_Python_Multithreading - Fatal编程技术网

为什么python线程会消耗这么多内存?

为什么python线程会消耗这么多内存?,python,multithreading,Python,Multithreading,为什么python线程会消耗这么多内存 我测量到生成一个线程会消耗8兆的内存,几乎和一个新的python进程一样大 操作系统:Ubuntu 10.10 编辑:由于大众的需求,我将给出一些无关的例子,如下所示: from os import getpid from time import sleep from threading import Thread def nap(): print 'sleeping child' sleep(999999999) print getp

为什么python线程会消耗这么多内存

我测量到生成一个线程会消耗8兆的内存,几乎和一个新的python进程一样大

操作系统:Ubuntu 10.10

编辑:由于大众的需求,我将给出一些无关的例子,如下所示:

from os import getpid
from time import sleep
from threading import Thread

def nap():
    print 'sleeping child'
    sleep(999999999)

print getpid()
child_thread = Thread(target=nap)
sleep(999999999)
在我的盒子上,pmap pid将给出9424K

现在,让我们运行子线程:

from os import getpid
from time import sleep
from threading import Thread

def nap():
    print 'sleeping child'
    sleep(999999999)

print getpid()
child_thread = Thread(target=nap)
child_thread.start()             # <--- ADDED THIS LINE
sleep(999999999)
从操作系统导入getpid
从时间上导入睡眠
从线程导入线程
def nap():
打印“熟睡的孩子”
睡眠(999999999)
打印getpid()
子线程=线程(目标=nap)

child_thread.start()#这不是特定于Python的,与操作系统为每个线程分配的独立堆栈有关。操作系统上默认的最大堆栈大小恰好是8MB

请注意,8MB只是一块留出的地址空间,最初只占用很少的内存。额外的内存在需要时提交到堆栈,最高可达8MB限制

可以使用
ulimit-s
调整限制,但是在这个例子中,我认为没有理由这样做


另外,
pmap
显示地址空间使用情况。这不是衡量内存使用情况的好方法。如果这两个概念相关,那么它们是截然不同的。

除非您告诉我们您想做什么以及如何做,否则任何人都无法回答这个问题。密码?使用示例?输入数据?谢谢,你刚刚救了我的命@Guido对不起,下一个问题我要表扬python。