Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Python handle memory?

I've been looking at a in-memory database -- and it got me thinking, how does Python handle IO that's not tied to a connection (and even data that is); for example, hashes, sets, etc.; is this a config somewhere, or is it dynamically managed based on resources; are there "easy" ways to view the effect resources are having on a real program, and simulate what the performance hit would be differing hardware setups?

NOTE: If it matters, Redis is the in-memory data store I'm looking at; there's an implementation of a wrapper for Redis datatypes so they mimic the datatypes found in Python.

like image 567
blunders Avatar asked Feb 26 '23 13:02

blunders


2 Answers

Python allocates all memory that the application asks for. There is not much room for policy. The only issue is when to release memory. (C)Python immediately releases all memory that is not referenced anymore (this is also not tunable). Memory that is referenced only from itself (ie. cycles) are released by the garbage collector; this has tunable settings.

It is the operating system's decision to write some of the memory into the pagefile.

like image 99
Martin v. Löwis Avatar answered Feb 28 '23 02:02

Martin v. Löwis


Not exactly what you're asking for, but Dowser is a Python tool for interactively browsing the memory usage of your running program. Very useful in understanding memory usage and allocation patterns.

http://www.aminus.net/wiki/Dowser

like image 23
Bill Gribble Avatar answered Feb 28 '23 03:02

Bill Gribble