Skip to content

Instantly share code, notes, and snippets.

@pawl
Last active October 29, 2017 09:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawl/c3ed7663b94abec01d75 to your computer and use it in GitHub Desktop.
Save pawl/c3ed7663b94abec01d75 to your computer and use it in GitHub Desktop.
Trying to figure out how to release a large dictionary from memory...
import random
import optparse
import gc
from flask import redirect, Flask
from memory_profiler import memory_usage
# Create flask app
app = Flask(__name__)
app.debug = True
@app.route('/')
def hello_world():
###
# The test:
# Generate a 2440 mb dictionary and return a blank page.
###
large_dict = {}
for x in xrange(125000):
large_row = {}
for y in xrange(125):
large_row[random.randint(1, 10000000000)] = random.randint(1, 10000000000)
large_dict[random.randint(1, 10000000000)] = large_row
large_dict = {}
gc.collect()
return ''
@app.after_request
def after(response):
print memory_usage(-1, interval=.2, timeout=1), "after request"
return response
def main():
app.config['SECRET_KEY'] = "BLAH"
parser = optparse.OptionParser()
parser.add_option("--host", default="0.0.0.0") # can't use -h, it's already taken by help
parser.add_option("-p", "--port", default="5123")
options, args = parser.parse_args()
if len(args) > 0:
parser.error("Unexpected arguments: %s" % args)
app.run(host=options.host, port=int(options.port))
if __name__ == '__main__':
# Start app
main()
import random
import gc
from memory_profiler import memory_usage
large_dict = {}
for x in xrange(50000):
large_row = {}
for y in xrange(125):
large_row[random.randint(1, 10000000000)] = random.randint(1, 10000000000)
large_dict[random.randint(1, 10000000000)] = large_row
large_dict = {}
del large_dict
gc.collect()
print memory_usage(-1, interval=.2, timeout=1), "after gc.collect()"
import random
import gc
import time
large_dict = {}
for x in xrange(50000):
large_row = {}
for y in xrange(125):
large_row[random.randint(1, 10000000000)] = random.randint(1, 10000000000)
large_dict[random.randint(1, 10000000000)] = large_row
# Force a sweep
large_dict = {}
del large_dict
gc.collect()
time.sleep(55000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment