I have a flask application running on Ubuntu which reads from a file and creates several large dictionaries. After the dictionaries are created and flask returns the request, the memory used for the dictionaries is not released back to the OS. This example shows the problem:
It’s not really a Flask issue, but the Flask guys were super helpful when I asked why my large dictionary wasn’t released from memory: Flask issue #1202
I dug even deeper and learned this is a Linux thing: “Python returns memory to the OS on the heap (that allocates other objects than small objects) only on Windows, if you run on Linux, you can only see the total memory used by your program increase.” Theano: Python memory management
My solution: Turning it into a function and running it as a separate process:
If you want to go even deeper down the rabbit hole, read these:
“We compiled a version of Python with TCMalloc that only uses mmap. When testing the new Python in one of our largest projects, we found that not only did Python give back memory to the OS correctly, it also had a reduced memory usage and no apparent CPU penalty for using mmap instead of brk.” - pushingtheweb.com: Python and TCMalloc
You’ll need to have used virtualenv to appreciate this.
Basically, you create virtual environments with mkvirtualenv and they’re stored in a central location. When you want to switch to your virtual environment, you use the workon command.
You can also create a postactivate script that loads environmental variables when you switch to your virtual environment.
I was getting this error while using the POST /venues method of the meetup API.
The error seems to happen when you duplicate the address of another venue. I was able to add the venue, but when I tried to add it a second time - I would get this error.
I ended up getting the list of venues and trying to find a match first.