Last active
December 17, 2015 07:19
-
-
Save diezguerra/5571947 to your computer and use it in GitHub Desktop.
Closure saving and GC example. # needs memory_profiler module! $ python -m memory_profiler noleaks.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
import gc | |
def iterjam(): | |
import functools as ft | |
def iterjam2(): | |
return ft | |
return iterjam2 | |
@profile | |
def main(): | |
horrible_things_might_happen = {} | |
for number in xrange(1000000): | |
horrible_things_might_happen[str(number)] = iterjam() | |
print "1MM import closures saved" | |
for number in xrange(500000): | |
del horrible_things_might_happen[str(number)] | |
print ".5MM import closures deleted" | |
gc.collect() | |
print "Collected memory" | |
for number in xrange(500000, 1000000): | |
del horrible_things_might_happen[str(number)] | |
print ".5MM import closures deleted" | |
gc.collect() | |
print "Collected memory" | |
horrible_things_might_happen = None | |
print "Nullified closure container" | |
gc.collect() | |
print "Collected memory" | |
del horrible_things_might_happen | |
print "Deleted closure container" | |
gc.collect() | |
print "Collected memory" | |
horrible_things_might_happen = {} | |
for number in xrange(1000000): | |
horrible_things_might_happen[str(number)] = iterjam() | |
print "1MM import closures saved again" | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1MM import closures saved | |
.5MM import closures deleted | |
Collected memory | |
.5MM import closures deleted | |
Collected memory | |
Nullified closure container | |
Collected memory | |
Deleted closure container | |
Collected memory | |
1MM import closures saved again | |
Filename: noleaks.py | |
Line # Mem usage Increment Line Contents | |
================================================ | |
16 @profile | |
17 8.633 MB 0.000 MB def main(): | |
18 8.633 MB 0.000 MB horrible_things_might_happen = {} | |
19 421.363 MB 412.730 MB for number in xrange(1000000): | |
20 421.363 MB 0.000 MB horrible_things_might_happen[str(number)] = iterjam() | |
21 | |
22 421.363 MB 0.000 MB print "1MM import closures saved" | |
23 | |
24 422.215 MB 0.852 MB for number in xrange(500000): | |
25 286.961 MB -135.254 MB del horrible_things_might_happen[str(number)] | |
26 | |
27 286.961 MB 0.000 MB print ".5MM import closures deleted" | |
28 | |
29 299.824 MB 12.863 MB gc.collect() | |
30 299.824 MB 0.000 MB print "Collected memory" | |
31 | |
32 299.992 MB 0.168 MB for number in xrange(500000, 1000000): | |
33 190.410 MB -109.582 MB del horrible_things_might_happen[str(number)] | |
34 | |
35 190.410 MB 0.000 MB print ".5MM import closures deleted" | |
36 | |
37 190.031 MB -0.379 MB gc.collect() | |
38 190.031 MB 0.000 MB print "Collected memory" | |
39 | |
40 189.781 MB -0.250 MB horrible_things_might_happen = None | |
41 | |
42 189.781 MB 0.000 MB print "Nullified closure container" | |
43 | |
44 189.781 MB 0.000 MB gc.collect() | |
45 189.781 MB 0.000 MB print "Collected memory" | |
46 | |
47 189.781 MB 0.000 MB del horrible_things_might_happen | |
48 | |
49 189.781 MB 0.000 MB print "Deleted closure container" | |
50 | |
51 189.781 MB 0.000 MB gc.collect() | |
52 189.781 MB 0.000 MB print "Collected memory" | |
53 | |
54 189.781 MB 0.000 MB horrible_things_might_happen = {} | |
55 557.574 MB 367.793 MB for number in xrange(1000000): | |
56 557.574 MB 0.000 MB horrible_things_might_happen[str(number)] = iterjam() | |
57 | |
58 557.574 MB 0.000 MB print "1MM import closures saved again" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment