This question has been bothering me for two weeks and I've searched online and asked people but couldn't get an answer.
Python by default build the library libpythonMAJOR.MINOR.a
and statically links it into the interpreter. Also it has an --enable-shared
flag, which will build a share library libpythonMAJOR.MINOR.so.1.0
, and dynamically link it to the interpreter.
Based on my poor CS knowledge, the first thought came into my mind when I saw "shared library", is that, "the shared bulid one must save a lot of memory compared to the static build one!".
Then I had this assumption:
# share build
34K Jun 29 11:32 python3.9
21M Jun 29 11:32 libpython3.9.so.1.0
10 shared python processes, mem usage = 0.034M * 10 + 21M ≈ 21M
# static build
22M Jun 27 23:45 python3.9
10 static python processes, mem usage = 10*22M = 220M
shared python wins!
Later I ran a toy test on my machine and found that's wrong.
test.py
import time
i = 0
while i < 20:
time.sleep(1)
i += 1
print('done')
mem_test.sh
#! /bin/bash
for i in {1..1000}
do
./python3.9 test.py &
done
For share python to run I set export LD_LIBRARY_PATH=/home/tian/py3.9.13_share/lib
.
I ran mem_test.sh
separately (one by one) with 2 pythons and simply monitored the total mem usage via htop
in another console. It turns out that both eat almost the same amount of memory.
Later on people taught me there's something call "paging on demand":
Is an entire static program loaded into memory when launched?
so my previous calculation of static python mem usage is completely wrong.
Now I am confused. Shared build python doesn't use less memory via a share library runtime?
Question:
What's the benefit of a shared build python vs a static build python? Or the shared built python indeed save some memory by the mechanism of using a share library, but my test is too trival to reveal?
P.S.
Checking some python official Dockerfiles, e.g. this one you would see they all set --enable-shared
.
Also there's related issue on pyenv https://github.com/pyenv/pyenv/issues/2294 , it seems that neither they figure that out.