Python memory consumption on Linux: physical and virtual memory are growing while the heap size remains the same

Active3 hr before
Viewed126 times

4 Answers


Python memory consumption on Linux: physical and virtual memory are growing, but heap size remains unchanged - python,What is the structure of PHP and which one is good? - php,Why is there such a big mismatch between the memory size of python physical memory and the size of the python heap?,However, the amount of memory remains unchanged:

I am having memory problems when using a python script to issue a large solr request. I use the solrpy library to interact with the solr server. The query returns about 80,000 records. Immediately after issuing the request, the python memory size is scanned through the upper balls up to ~ 190 MB.

PID USER PR NI VIRT RES SHR S % CPU % MEM TIME + COMMAND 8225 root 16 0 193 m 189 m 3272 S 0.0 11.2 0: 11.31 python...
load more v

Inexperienced programmers often think that Java’s automatic garbage collection completely frees them from worrying about memory management. This is a common misperception: while the garbage collector does its best, it’s entirely possible for even the best programmer to fall prey to crippling memory leaks. Let me explain.,Before we start our analysis of an application with a memory leak issue, let’s first look at how garbage collection works in the JVM.,This error message doesn’t necessarily imply a memory leak. In fact, the problem can be as simple as a configuration issue.,One of the quickest ways to assert that you indeed have a memory leak is to enable verbose garbage collection. Memory constraint problems can usually be identified by examining patterns in the verbosegc output.

First, we need to grant ourselves JVM access on the target machine. To do so, create a file called jstatd.all.policy with the following content:

grant codebase "file:${java.home}/../lib/tools.jar" {


load more v

allocates more RAM than our physical memory with mmap,and this time that killed some Chromium process, which is usually my computers normal memory hog:,and further ahead we see that our own little main.out actually got killed on the previous invocation:,VSZ virtual memory remains constant at printf '0x%X\n' 0x40009A4 KiB ~= 64GiB (ps values are in KiB) after the mmap.

Compile and run:

gcc - ggdb3 - O0 - std = c99 - Wall - Wextra - pedantic - o main.out main.c
echo 1 | sudo tee / proc / sys / vm / overcommit_memory
sudo dmesg - c
   . / main.out 0x1000000000 0x200000000
echo $ ?
   sudo dmesg
load more v

The Python memory manager manages chunks of memory called “Blocks”. A collection of blocks of the same size makes up the “Pool”. Pools are created on Arenas, chunks of 256kB memory allocated on heap=64 pools. If the objects get destroyed, the memory manager fills this space with a new object of the same size.,Methods and variables are created in Stack memory. A stack frame is created whenever methods and variables are created. These frames are destroyed automatically whenever methods are returned.,Objects and instance variables are created in Heap memory. As soon as the variables and functions are returned, dead objects will be garbage collected.,The good thing about Python is that everything in Python is an object. This means that Dynamic Memory Allocation underlies Python Memory Management. When objects are no longer needed, the Python Memory Manager will automatically reclaim memory from them.

Static Memory Allocation — The program is allocated memory at compile time. An example of this would be in C/C++, you declare static arrays only with fixed sizes. The memory is allocated at the time of compilation. Stack is used for implementing static allocation. In this case, memory can not be reused.

static int a = 10;

Dynamic Memory Allocation — The program is allocated memory at runtime. An example of this would be in C/C++, you declare arrays with the unary operator new. The memory is allocated at runtime. Heap is used for implementing dynamic allocation. In this case, memory can be freed and reused when not required.

int * p;
p = new int;
load more v

Other "consumption-python" queries related to "Python memory consumption on Linux: physical and virtual memory are growing while the heap size remains the same"