Django using too much memory? Turn off DEBUG=True!
When the Django docs say to turn off DEBUG in settings.py to save memory, they’re not talking peanuts. Over the weekend I imported the largest section of my old blog to my new Django blog, and the command-line script to publish these pages crashed with a MemoryError: “*** error: can't allocate region”.
I ran the script again and watched memory use in top. After a few hundred pages, my 1.5 gigabytes of free memory had dropped to under 10. A quick Google search indicated that the first thing to do when Django has memory problems is turn off DEBUG in settings.py. I did that this morning, and free memory, after scaring me by dropping considerably in the first few minutes, hovered around 1.4 gig for the rest of the process’s life. The Python process, which had grown to over a gigabyte last night, remained at 54 megabytes throughout the upload process.
I’d have to say that DEBUG is meant only for debugging immediate issues and should never be used for long-term development.
More Django
- Converting an existing Django model to Django-MPTT
- Using a SQL database to mimic a filesystem will, eventually, create bottlenecks when it comes to traversing the filesystem. One solution is modified preordered tree traversal, which saves the tree structure in an easily-used manner inside the model.
- Two search bookmarklets for Django
- Bookmarklets—JavaScript code in a bookmark—can make working with big Django databases much easier.
- Fixing Django’s feed generator without hacking Django
- It looks like it’s going to be a while before the RSS feed generator in Django is going to get fixed, so I looked into subclassing as a way of getting a working guid in my Django RSS feeds.
- ModelForms and FormViews
- This is just a notice because when I did a search, nothing came up. Don’t use ModelForm with FormView, use UpdateView instead.
- Django: fix_ampersands and abbreviations
- The fix_ampersands filter will miss some cases where ampersands need to be replaced.
- 29 more pages with the topic Django, and other related pages
More Duh
- ModelForms and FormViews
- This is just a notice because when I did a search, nothing came up. Don’t use ModelForm with FormView, use UpdateView instead.
- minidom self-closes empty SCRIPT tags
- Python’s minidom will self-close empty script tags—as it should. But it turns out that Firefox 3.6 and IE 8 don’t support empty script tags.
- Add nodes to SimpleXMLElement
- If you want to add child nodes in PHP’s SimpleXML, the correct way to do it is to add the node first, then create it.
- No distutils? Install Xcode
- If Distutils is not available on Mac OS X Leopard, install the Xcode developer tools. Also, the upgrade process I followed for upgrading from Mailman 2.1.9 to 2.1.12.
- Django QuerySet Heisenberg gotcha
- Observing a system changes the system. That’s especially true with Django’s QuerySet API.