Skip to content

Instantly share code, notes, and snippets.

@pawl
Created October 13, 2014 16:20
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/8067c988b1cbfd48b855 to your computer and use it in GitHub Desktop.
Save pawl/8067c988b1cbfd48b855 to your computer and use it in GitHub Desktop.
Flask isn't releasing memory used for large dictionaries
import random
import optparse
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
print memory_usage(-1, interval=.2, timeout=1), "end"
return ''
###
# The result:
# large_dict stays in memory, and memory usage never goes below ~2440 mb
###
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()
@davidism
Copy link

There's still a reference to the memory, large_dict, when you run the profiler. Even if there wasn't you can't guarantee that the interpreter will have released the memory.

Try running the profiler in the after_request handler, or on a subsequent request. (Provided that the garbage collection has run.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment