fix prepare_tcmalloc (fix: #14227)(Fixed memory leak issue in Ubuntu 22.04 or modern linux environment) #14883
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Probrem:
Dear...
tcmalloc is not preloaded in the ubuntu 22.04LTS environment + dev branch, causing a memory leak and eventually causing the entire Webui to be OOMed (OOMKiller on Linux).
This is caused by changes in webui.sh that were merged into #14277.
Change #14277 seems to check if tcmalloc links libpthread, but modern Linux systems (ubuntu, fedora, suse, arch,WSL etcetc......) come with glibc (libc.so) version 2.34 or higher .
Starting with glibc 2.34, pthreads are integrated into libc.so(glibc).
In any case, the problem is that due to the change in #14277, even though tcmalloc is available, it is not written to LD_PRELOAD, and as a result, tcmalloc is not used.
My merge respects the changes in #14277, but also ensures that tcmalloc is automatically and properly preloaded on modern Linux systems.
Specifically, refer to the libc.so version and apply the contents of #14277 if it is 2.34 or lower, and perform the same processing as 1.7.0 if it is 2.34 or higher.
(Since there are VARIOUS Linux systems, we have changed LD_PRELOAD to specify the full path to libtcmalloc just to be sure.)
Why do we need tcmalloc?
It appears that Python's memory allocator is very conservative, so it may not be freed even if it is no longer referenced.
tcmalloc alleviates this situation.
see also-> vladmandic/sdnext#1357 (comment)
Screenshots/videos:
###pure dev branch

can not found libtcmalloc ????
tcmalloc is unable to PRELOAD!!!

This can be easily seen by looking at /proc/PID/maps
After generating the image 5 times, if you switch the checkpoint and generate it 5 times again, the memory situation will be like this.

And finally it was terminated by OOMKiller

after merge this patch
I started checking the glibc version, so the appropriate processing is being performed.

If you look at maps, tcmalloc is correctly mapped to memory.

After generating the image 5 times, if you switch the checkpoint and generate it 5 times again again again....

In my environment, it became stable at about 9GB to 12GB.
Checklist:
PS
Remember that tcmalloc is only a mitigation measure.
It's possible that there are still some memory references left somewhere.
I'm not very familiar with python so I can't track it down. (I'm a C/C++ speaker).
I'm sorry, thank you.