Marko Lalic: PTS rewrite: Django Memory Usage
Rewriting the PTS in a fairly large Web framework such as Django is sure to cause larger memory consumption by even the more simple tasks, when compared to a couple of (loosely related) perl scripts. If anything, simply due to the fact that the entire Django machinery needs to be loaded.
However, just how large the memory consumption difference between the two can be seen in the example of the command to dispatch received package emails to users. In the new version, the command is implemented as a Django management command, whereas before it was a perl script dispatch.pl relying on the common.pl script for some additional functions.
The reason this command's memory usage is important is an incident with the current test deployment of the new PTS found at http://pts.debian.net. All package mails that the old PTS receives are also forwarded to the new instance in order to expose the new implementation to real-world mail traffic. At a certain point, over a hundred mails were received instantly causing over a hundred dispatch processes to be launched. This led to the system running out of memory and the kernel had to run the OOM Killer which brought down PostgreSQL server thereby causing the site to become unavailable.
For now, such a problem seems to have been prevented from reoccurring by setting up exim to queue messages when a certain system average load is exceeded, however it is interesting to check how large the memory consumption difference between the old and new dispatch really is.
Measurement setupIn order to measure the memory use, two messages of different sizes were used: 1KB and 1MB.
However, just how large the memory consumption difference between the two can be seen in the example of the command to dispatch received package emails to users. In the new version, the command is implemented as a Django management command, whereas before it was a perl script dispatch.pl relying on the common.pl script for some additional functions.
The reason this command's memory usage is important is an incident with the current test deployment of the new PTS found at http://pts.debian.net. All package mails that the old PTS receives are also forwarded to the new instance in order to expose the new implementation to real-world mail traffic. At a certain point, over a hundred mails were received instantly causing over a hundred dispatch processes to be launched. This led to the system running out of memory and the kernel had to run the OOM Killer which brought down PostgreSQL server thereby causing the site to become unavailable.
For now, such a problem seems to have been prevented from reoccurring by setting up exim to queue messages when a certain system average load is exceeded, however it is interesting to check how large the memory consumption difference between the old and new dispatch really is.
Measurement setupIn order to measure the memory use, two messages of different sizes were used: 1KB and 1MB.
For the new PTS two cases were considered, when the database used is sqlite3 and PostgreSQL. In both cases, it was simply initialized by syncdb, meaning it contained only the default keywords found in the initial fixture and no registered users and subscriptions. The DEBUG setting was set to False.
For testing the old PTS's dispatch.pl, the database was also initialized empty.
This way, the tests should show the actual difference between the two implementations.
Since the main concern here is the maximum memory usage of the dispatch script, a simple bash script was written which takes a PID of a running process and outputs the maximum memory usage as reported by ps once the process is terminated. The script used is shown below.
#!/usr/bin/env bash
pid=$1
while ps $pid >/dev/null
do
ps -o vsz= $ pid
sleep 0.1
done sort -n tail -n1
Old PTS MeasurementsThe maximum memory usage of the old PTS was:- 37.8 MB for the 1 KB message
- 38.2 MB for the 1 MB message
New PTS Measurementssqlite3The maximum memory usage of the new PTS using sqlite3 was:- 91.77 MB for the 1 KB message
- 92.59 MB for the 1 MB message
PostgreSQL
The maximum memory usage of the new PTS using PostgreSQL was:- 148 MB for the 1 KB message
- 160 MB for the 1 MB message
Comparison and DiscussionWhen comparing the memory used by the old dispatch.pl implementation and the new PTS running sqlite3, the difference does not seem to be too large and is to be expected, since, as mentioned, the whole Django framework is loaded when executing a management command.
However, the huge difference between the two versions of the new PTS: the one using sqlite3 and the one using PostgreSQL is very surprising indeed.
Another interesting measurement is the difference between running a bare Django management command: one including only a sleep statement (in order to allow enough time to measure its memory usage) on PostgreSQL and sqlite3. In both cases, the memory usage was only about 1 MB less than what the respective maximum memory usage when processing the 1 KB message.
All this considered, the logical conclusion seems to be that either psycopg2 package and/or the Django postgresql_psycopg2 database engine use a lot more memory than the corresponding sqlite3 alternatives.
Could anyone shed some more light as to what causes this stark difference between using sqlite3 and Postgres in Django? Anything that could be done to mitigate it?
pid=$1
while ps $pid >/dev/null
do
ps -o vsz= $ pid
sleep 0.1
done sort -n tail -n1
The maximum memory usage of the old PTS was:
- 37.8 MB for the 1 KB message
- 38.2 MB for the 1 MB message
New PTS Measurementssqlite3The maximum memory usage of the new PTS using sqlite3 was:- 91.77 MB for the 1 KB message
- 92.59 MB for the 1 MB message
PostgreSQL
The maximum memory usage of the new PTS using PostgreSQL was:- 148 MB for the 1 KB message
- 160 MB for the 1 MB message
Comparison and DiscussionWhen comparing the memory used by the old dispatch.pl implementation and the new PTS running sqlite3, the difference does not seem to be too large and is to be expected, since, as mentioned, the whole Django framework is loaded when executing a management command.
However, the huge difference between the two versions of the new PTS: the one using sqlite3 and the one using PostgreSQL is very surprising indeed.
Another interesting measurement is the difference between running a bare Django management command: one including only a sleep statement (in order to allow enough time to measure its memory usage) on PostgreSQL and sqlite3. In both cases, the memory usage was only about 1 MB less than what the respective maximum memory usage when processing the 1 KB message.
All this considered, the logical conclusion seems to be that either psycopg2 package and/or the Django postgresql_psycopg2 database engine use a lot more memory than the corresponding sqlite3 alternatives.
Could anyone shed some more light as to what causes this stark difference between using sqlite3 and Postgres in Django? Anything that could be done to mitigate it?
The maximum memory usage of the new PTS using sqlite3 was:
- 91.77 MB for the 1 KB message
- 92.59 MB for the 1 MB message
PostgreSQL
The maximum memory usage of the new PTS using PostgreSQL was:
- 148 MB for the 1 KB message
- 160 MB for the 1 MB message
[still no pictures here, feel free to go to
Just to let you know. We ve been celebrating 20th anniversary also in Poland.


It seems like having several SSH keys on the same system starts giving issues with some SSH servers. The issue is that while connecting it returns:
It'd be easy to assume that in a Mir-based world, the Mir server receives input events and hands them over to Mir clients. In fact, as I described
Very long post ahead (sorry for the wall of text), part of a series of posts on some sysadmin topics, see
Seeing as recent versions of Xorg have made it into Debian's testing (which
I am using right now, instead of my usual sid installations), I decided to
see what they would bring me in terms of performance, since they have the
SNA acceleration implemented as well as the standard UXA acceleration.
Well, I did not have any other tests to perform, but below are the results
on my wife's laptop (a Core i5 2410M), when running
As usual with our
The
Another member of Debian's GSoC program,
As the grand finale, we took
The grand-finale of the
This weekend I'm mostly alternating between reading, writing, and trying to avoid death by the plague.[*]
I've switched Lumail to using a UTF-8 aware string library, which means we can now handle the obvious case:
I write a fair number of Unix command line programs, and I want to
have test suites for them. A year and a half ago I wrote a tool to
make that easier: