为什么python没有垃圾收集器线程? [英] Why python doesn't have Garbage Collector thread?

查看:120
本文介绍了为什么python没有垃圾收集器线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java有daemon线程来监视内存使用情况并执行gc任务。从jstack中我可以看到

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$'$ ]
java.lang.Thread.sleep的
(原生方法)
....
GC任务线程#
java.lang.Thread.State:TIMED_WAITING(sleeping) 0(ParallelGC)os_prio = 0 tid = 0x00007f34b0023000 nid = 0x809 runnable
GC任务线程#1(ParallelGC)os_prio = 0 tid = 0x00007f34b0024800 nid = 0x80a可运行
GC任务线程#2 ParallelGC)os_prio = 0 tid = 0x00007f34b0026800 nid = 0x80b runnable

但是在谈到python时,我写了一篇


$ b $ pre $ #!/ usr / bin / env python
导入gc
导入时间
gc .enable()
,而True:
print每分钟打印一次。
time.sleep(60)

我看到python进程只有一个线程,

  $ cat / proc / 1627 / status 
名称:python
...
线程:1

问题是,为什么python没有像Java那样的gc线程?然后哪个线程执行gc任务?

解决方案

如果您使用 -XX启动java:+ UseSerialGC 选项,您将看不到任何GC线程。
对于单线程GC算法,应用程序线程可用于执行GC活动。



所需的专用线程

并行GC(您需要多于单线程)

  • 并行GC(与应用程序逻辑并行运行的GC活动)

    Java has daemon thread to monitor memory usage and do the gc task. From jstack I see

    "main" #1 prio=5 os_prio=0 tid=0x00007f34b000e000 nid=0x808 waiting on condition [0x00007f34b6f02000]
       java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
    ....
    "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f34b0023000 nid=0x809 runnable 
    "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f34b0024800 nid=0x80a runnable 
    "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007f34b0026800 nid=0x80b runnable
    

    But speaking of python, I wrote a

    #!/usr/bin/env python
    import gc
    import time 
    gc.enable()
    while True:
        print "This prints once a minute."
        time.sleep(60) 
    

    I saw the the python process has only one thread,

    $ cat /proc/1627/status
    Name:   python
    ...
    Threads:    1
    

    The question is, why python doesn't have gc thread like Java? Then which thread does the gc task?

    解决方案

    If you start java with -XX:+UseSerialGC options you will not see any GC thread. For single threaded GC algorithm application thread can be used to do GC activities.

    Dedicated threads required for

    • Parallel GC (you need more than single thread)
    • Concurrent GC (GC activities running in parallel with application logic)

    这篇关于为什么python没有垃圾收集器线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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