I have a question about the virtual memory in Python.
When the process is consuming a relatively large amount of memory, it doesn’t “release” the unused memory. For example, after creating a massive list of strings, let’s say the list uses 30MB of memory, so the entire process takes roughly 40MB, when the list is deleted, the process still consuming 40MB, but if another list with the same amount of data is created, the process will not take more memory, because it will use the virtual memory that is available but not released to the OS.
My question is: What kind of data will reuse that non-released virtual memory? I mean, that 30MB was “taken” from the OS when I created a list of strings, and even when I delete it, the next list of strings will not take more memory from the OS as long as it fits in the 30MB. But if instead a list of strings another type of data is created, like a QPixmap (from Qt, using PyQt), will it use that 30MB originally allocated by the list of strings?
Thank you in advance.
Edit: Well, this question sounds lazy. I know I could simply test this specific case, but i want to know in theory, I don’t want the answer for this “list of strings and qpixmap” specific case, but in general.
At the C level (CPython’s implementation), anything that is allocated on the heap with
malloc()will consume memory and this memory will not be released to the OS when that memory is freed withfree(). It will only be returned when the process dies. But when new blocks are allocated withmalloc()they will use the freed-up memory.(Unless the free memory is really badly fragmented and there is not enough contiguous free space in the freed-up zones to accommodate new allocations. But let’s not worry about this pathological case.)
Every Python object is implemented by CPython as one or more blocks of memory allocated with
malloc()so the answer to your question is: pretty much any piece of Python data can reuse the space that was freed by the deallocation of some other piece of Python data.