-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemcount.py
More file actions
executable file
·58 lines (43 loc) · 1.3 KB
/
memcount.py
File metadata and controls
executable file
·58 lines (43 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
a simple test of a circular reference that gc.collect() will not clean up.
And watch the memory grow!
"""
import sys
import gc
import weakref
import mem_check
class MyChild(object):
def __init__(self, parent):
#self.parent = parent
# if a weak ref is used, then no memory leak.
self.parent = weakref.proxy(parent)
## store some data so it will use appreciable memory
## multiply by 1234 to reduce interning
self.data = [1234*i for i in range(100000)]
def __del__(self):
""" __del__ defined to defeat GC"""
print 'MyChild deleted', id(self)
class MyParent(object):
def __init__(self):
self.children = []
def addChild(self):
child = MyChild(self)
self.children.append(child)
return child
def __del__(self):
""" __del__ defined to defeat GC"""
print 'MyParent deleted', id(self)
if __name__ == "__main__":
# create a bunch in a loop:
for i in range(50):
print "iteration:", i
p = MyParent()
p.addChild()
p.addChild()
p.addChild()
print "ref count:", sys.getrefcount(p)
print "mem_use: %f MB"%mem_check.get_mem_use()
del p
print "collected", gc.collect()