pstree
apt-get install psmisc
iftop 查看网络流量
apt-get install iftop
strace 查看系统调用
apt-get install strace
gdb python查看调用栈
apt-get install gdb python2.7-dbg
https://github.com/python/cpython/blob/master/Tools/gdb/libpython.py
curl -o .gdbinit http://svn.python.org/projects/python/trunk/Misc/gdbinit
gdb python
(gdb) info threads
(gdb) thread apply all py-list
查看线程栈
pip install pystack-debugger
pystack
连接进入线上python程序 (易引起线上程序挂)
apt-get install gdb
pip install pyrasite
pyrasite-shell
import gc
import sys
sys.getrefcount(a)
gc.garbage # 不可达但又不能释放的对象, 引用环安全删除次序未知
gc.get_objects()
然后可参考如下代码来做统计
stats = []
objects = gc.get_objects()
objects_temp = objects[100:120]
print('objects:', objects_temp)
run_objects = {}
run_others = {}
for o in objects:
if hasattr(o, '__class__'):
key = str(o.__class__)
run_list = run_objects
else:
key = type(o)
run_list = run_others
if key in run_list:
run_list[key] += 1
else:
run_list[key] = 1
for obj in run_objects:
print('object', run_objects[obj], obj)
stats.append([run_objects[obj], obj])
for obj in run_others:
print('none object', run_others[obj], obj)
stats.append([run_others[obj], obj])
"""
('object', 4, "<type 'zipimporter'>")
('object', 1, "<type 'NotImplemented'>")
('object', 1, "<class 'float_info'>")
('object', 3, "<class 'site._Printer'>")
('object', 5, "<type 'frame'>")
('object', 64, "<type 'weakref'>")
('object', 109, "<type 'set'>")
('object', 2583, "<type 'builtin-code'>")
('object', 6, "<type 'SRE_Pattern'>")
('object', 48, "<class '_weakrefset.WeakSet'>")
('object', 1361, "<type 'tuple'>")
('object', 25, "<type 'classmethod'>")
('object', 18, "<type 'method'>")
转自 https://blog.csdn.net/vinrex/article/details/38390181
"""
抽取数量较多的class的对象查看
import gc
import random
objects = gc.get_objects()
dicts = []
for o in objects:
if hasattr(o, '__class__'):
if str(o.__class__) == "<class 'dict'>":
dicts.append(o)
dicts[random.randint(0,len(dicts) - 1)]
引用关系导出为图像(objgraph详细介绍见objgraph小节)
import objgraph
objgraph.show_backrefs(dicts[random.randint(0,len(dicts) - 1)], max_depth=10)
> 如果内存占用较大, 这里会超时.
> 可下载git源码: https://github.com/lmacken/pyrasite.git
> 将pyrasite.PyrasiteIPC相关语名改为以下内容:
ipc = pyrasite.PyrasiteIPC(pid, 'ReversePythonShell',
timeout=7200)
查看不同类型object的内存占用
pip install meliae urwid
pyrasite-memory-viewer
guppy
只支持python2
objgraph (结合pyrasite-shell)
pip install graphviz==0.1
pip install objgraph
objgraph.show_most_common_types(limit=50)
objgraph.show_growth()
选择一个type导出调用图像查看调用关系
objgraph.show_backrefs([ objgraph.by_type(‘weakproxy’)[1] ], max_depth=10)