This is a linkage question rather than a uwsgi question. But I will explain the story.
I am using uwsgi to host my flask app. After running some weeks on production, I found that my app have some slight memory leak; its RSS size reported by uswgitop is increasing. Although it is a not big deal as it is only around 50MiB to 60MiB, I want to find out why it is increasing.
After some work, I found that it might due to memory fragmentation of the malloc implementation in libc.
So I am considering using tcmalloc or jemalloc, which is famous for the memory fragmentation management.
Then I came to build uwsgi on my own. It produced the following linking command.
x86_64-linux-gnu-gcc -pthread -o uwsgi -L/usr/lib -Wl,-rpath,/usr/lib .......tons of object files
-lpthread -lm -rdynamic -ldl -lz -ltcmalloc
-L/home/alex/local/lib -lpcre -lssl -lcrypto -lxml2 -lpthread -ldl -lutil -lm -lpython2.7 -lcrypt
As you can see from the comand, it explicitly links against tcmalloc, which is a dynamic link not a static one.
As far as I know, tcmalloc implements the standard posix malloc interface, that is also why we can use LD_PRELOAD
to hook into one application to replace its malloc implementation.
My question here is:
Why we need to explicitly specify to link against tcmalloc when we can use LD_PRELOAD
to do so?