Skip to content

Commit 1da2c56

Browse files
committed
SIGUSR1 now prints and plots memory usage and object graphs to find memleaks
1 parent bf36b0a commit 1da2c56

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

gp_learner.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from functools import partial
1919
from operator import attrgetter
2020
import random
21+
import signal
2122
from time import sleep
2223
from datetime import datetime
2324

@@ -61,6 +62,7 @@
6162
from ground_truth_tools import k_fold_cross_validation
6263
from ground_truth_tools import split_training_test_set
6364
from gtp_scores import GTPScores
65+
from memory_usage import log_mem_usage
6466
from serialization import find_last_result
6567
from serialization import find_run_result
6668
from serialization import format_graph_pattern
@@ -80,6 +82,7 @@
8082

8183

8284
logger.info('init gp_learner')
85+
signal.signal(signal.SIGUSR1, log_mem_usage)
8386

8487

8588
class GPLearnerException(Exception):

memory_usage.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import
3+
from __future__ import division
4+
from __future__ import print_function
5+
6+
7+
import gc
8+
import random
9+
from six.moves import StringIO
10+
11+
import objgraph
12+
13+
from logging_config import filename
14+
15+
_count = 0
16+
17+
18+
def log_mem_usage(signum, frame, fname=None):
19+
global _count
20+
_count += 1
21+
gc.collect()
22+
if not fname:
23+
fname = filename + '_memory_%02d.log' % _count
24+
with open(fname, 'wb') as f:
25+
f.write('gc.garbage: %d\n\n' % len(gc.garbage))
26+
objgraph.show_most_common_types(limit=50, file=f)
27+
f.write('\n\n')
28+
buf = StringIO()
29+
objgraph.show_growth(limit=50, file=buf)
30+
buf = buf.getvalue()
31+
f.write(buf)
32+
if _count < 2:
33+
return
34+
for tn, l in enumerate(buf.splitlines()[:10]):
35+
l = l.strip()
36+
if not l:
37+
continue
38+
type_ = l.split()[0]
39+
objects = objgraph.by_type(type_)
40+
objects = random.sample(objects, min(50, len(objects)))
41+
objgraph.show_chain(
42+
objgraph.find_backref_chain(
43+
objects[0],
44+
objgraph.is_proper_module),
45+
filename=fname[:-4] + '_type_%02d_backref.png' % tn
46+
)
47+
objgraph.show_backrefs(
48+
objects,
49+
max_depth=5,
50+
extra_info=lambda x: hex(id(x)),
51+
filename=fname[:-4] + '_type_%02d_backrefs.png' % tn,
52+
)

0 commit comments

Comments
 (0)