Why python doesn't have Garbage Collector thread?

﹥>﹥吖頭↗ 提交于 2019-12-22 17:20:07

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/38651571/why-python-doesnt-have-garbage-collector-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!