Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix prepare_tcmalloc (fix: #14227)(Fixed memory leak issue in Ubuntu 22.04 or modern linux environment) #14883

Merged
merged 1 commit into from
Feb 11, 2024

Conversation

analysisjp
Copy link
Contributor

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/automatic#1357 (comment)

Screenshots/videos:

###pure dev branch
can not found libtcmalloc ????
01_devrepo_tcmalloc_notfound

tcmalloc is unable to PRELOAD!!!
This can be easily seen by looking at /proc/PID/maps
02_devrepo_tcmalloc_notpreload

After generating the image 5 times, if you switch the checkpoint and generate it 5 times again, the memory situation will be like this.
03_devrepo_memover

And finally it was terminated by OOMKiller
04_devrepo_oom_dead

after merge this patch

I started checking the glibc version, so the appropriate processing is being performed.
05_fixrepo_tcmalloc_found

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

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.
07_fixrepo_result_memory

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.

@JMLLR1
Copy link

JMLLR1 commented Feb 10, 2024

Nice to see that you made a fix for this. I am running ubuntu 22.04 as well and in some constellations my whole system froze while running due to OOM.

@AUTOMATIC1111 AUTOMATIC1111 merged commit 4d46f8c into AUTOMATIC1111:dev Feb 11, 2024
3 checks passed
@analysisjp analysisjp deleted the dev_fix_memleak_new branch February 13, 2024 12:10
analysisjp added a commit to analysisjp/stable-diffusion-webui that referenced this pull request Feb 13, 2024
AUTOMATIC1111 added a commit that referenced this pull request Feb 17, 2024
fixed webui.sh issue that occurred in WSL environment (fix: #14883)
@w-e-w w-e-w mentioned this pull request Feb 17, 2024
@pawel665j pawel665j mentioned this pull request Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants