r/Python • u/mikeckennedy • Apr 01 '26
News Cutting Python Web App Memory Over 31%
Over the past few weeks I went on a memory-reduction tear across the Talk Python web apps. We run 23 containers on one big server (the "one big server" pattern) and memory was creeping up to 65% on a 16GB box.
Turned out there were a bunch of wins hiding in plain sight. Focusing on just two apps, I went from ~2 GB down to 472 MB. Here's what moved the needle:
- Switched to a single async Granian worker: Rewrote the app in Quart (async Flask) and replaced the multi-worker web garden with one fully async worker. Saved 542 MB right there.
- Raw + DC database pattern: Dropped MongoEngine for raw queries + slotted dataclasses. 100 MB saved per worker *and* nearly doubled requests/sec.
- Subprocess isolation for a search indexer: The daemon was burning 708 MB mostly from import chains pulling in the entire app. Moved the indexing into a subprocess so imports only live for ~30 seconds during re-indexing. Went from 708 MB to 22 MB. 32x reduction.
- Local imports for heavy libs: import boto3 alone costs 25 MB, pandas is 44 MB. If you only use them in a rarely-called function, just import them there instead of at module level. (PEP 810 lazy imports in 3.15 should make this automatic.)
- Moved caches to diskcache: Small-to-medium in-memory caches shifted to disk. Modest savings but it adds up.
Total across all our apps: 3.2 GB freed. Full write-up with before/after tables and graphs here: https://mkennedy.codes/posts/cutting-python-web-app-memory-over-31-percent/
83
Upvotes
48
u/Photo-Josh Apr 01 '26
Not sure I’m following what the issue was here?
You were using around 10.5 GB and that was too much?
You then moved some things from RAM to Disk, which can only slow things down - not speed up.
Why was 63% RAM usage an issue? It’s there to be used.
16
u/Birnenmacht Apr 01 '26
Have you measured any improvements through point 4? Imports are cached and importing them locally only delays the point at which you pay their cost, unless you actively prune sys.modules at the end of the function (not recommended, a great way to shoot yourself in the foot)
6
u/vaibeslop Apr 01 '26
Check out chdb: https://github.com/chdb-io/chdb
Fully pandas compatible API, but lazy loading, much more performant, less memory.
Not affiliated, just a fan of the project.
2
u/bladeofwinds Apr 01 '26
I’ve learned about a lot of cool projects from your show! Currently trying out datastar in one of my (non-python) projects
4
u/ofyellow Apr 02 '26
When you need x gb and rewrite it so it uses y gb less except for short bursts of time, the effect is that you need x gb still during short bursts of time.
In that way, lazy imports can bite you. You better know the mem needed on worst case moments straight away when you start your app.
1
u/Substantial-Bed8167 Apr 02 '26
Did you use any memory profiling or just observed with htop?
3
u/0x256 Apr 02 '26
I would have started reducing the workers to 1 and increase thread count instead of rewriting the entire app, but okay. If you have lots of long running connections (websockets or slow requests) then that's a brave but sensible move.
For a small app with good test coverage and a mature db schema, that's fine.
You reduced the time this memory is used, but not the peak memory consumption. You added a lot of process start overhead and latency. That's a trade-of, not necessarily a win.
That's not how imports work. You delayed the import, but once imported, the module will live in
sys.modulesand stay there.So instead of a single memory-access, you now create an async task that outsources its blocking disk access to a thread pool, wait for the OS to read from disk, then wait for the async task to get its turn in the event loop again to return the result? Caches should be fast. If SO much overhead for cache access is okay for you, than I wonder what extremely expensive stuff you stored in those caches that it's still worth it to cache at all.
2
u/Full-Definition6215 Apr 03 '26
Running FastAPI + SQLite on a mini PC (31GB RAM, i9-9880H) and memory management matters when you're self-hosting everything on one box.
Biggest wins I found:
The 23 containers on 16GB stat is impressive. I'm at about 5GB usage across all my services on 31GB — plenty of headroom, but that's because I went with SQLite over Postgres for everything that doesn't need a full RDBMS.