-
-
Notifications
You must be signed in to change notification settings - Fork 605
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
Is memory leaking? #1771
Comments
It's pretty confusing... I created a import gc
import os
from collections import Counter
import psutil
from psutil._common import bytes2human
class MemoryMonitor:
def __init__(self) -> None:
self.last_mem: int = 0
self.obj_count: dict[str, int] = {}
def print_stats(self) -> None:
gc.collect()
memory = psutil.Process(os.getpid()).memory_info().rss
growth = memory - self.last_mem
print(bytes2human(memory), end=' ')
print(bytes2human(growth, r"%(value)+.1f%(symbol)s") if growth else '', end=' ')
counter: Counter = Counter()
for obj in gc.get_objects():
counter[type(obj).__name__] += 1
for cls, count in counter.items():
prev_count = self.obj_count.get(cls, 0)
if count != prev_count:
print(f'{cls}={count} ({count - prev_count:+})', end=' ')
self.obj_count[cls] = count
self.obj_count = {cls: count for cls, count in self.obj_count.items() if count > 0}
print()
self.last_mem = memory It can be used like this: from nicegui.debug import MemoryMonitor
monitor = MemoryMonitor()
ui.timer(5.0, monitor.print_stats) My observations for a page with 10,000 labels: Each new client connection causes a few extra MB of memory. The number of object instances also increases significantly. When closing a connection, the instance counts drop, but the memory doesn't decrease. This might be due to internal workings of Python's Memory Allocator. So to me it doens't necessarily looks like a general leak in NiceGUI, at least for a simple "Hello World" (x10,000) app. When analyzing our website, the main page seems to cost around 10MB per client. When opening a bunch of connections, each consumes 10MB. When closing them, lots of instances are removed, but the memory doesn't decrease. But when connecting new clients again, they consume much less memory. This supports the theory that the Memory Allocator somehow keeps the memory allocated for later. If the memory usage of nicegui.io keeps growing, this could either be due to lots of open connections, or the memory gets too fragmented. Or there is still a subtle leak somewhere... |
The fact that Python does not hand back once claimed memory makes it a bit harder to investigate potential issues. But your |
An E-Chart "ThemeRiver" would be a great way to visualize: https://echarts.apache.org/examples/en/editor.html?c=themeRiver-basic |
I just saw the log:
If we exit the loop due to key error we may miss deleting some objects. Or not? |
@rodja Yes, that sounds plausible. I just wonder why the element is missing in the dictionary. I'll try to reproduce it locally. |
So far no luck reproducing it. But looking into the traceback more closely, it seems like |
The analysis made on #2502 did not discover a memory leak. But on our servers for https://nicegui.io we occasionally still see
|
I'm not sure if this is the cause of the issue but It's used in a few places: https://github.com/search?q=repo%3Azauberzeug%2Fnicegui%20BaseHTTPMiddleware&type=code |
Interesting, @Ezbaze! Can you elaborate? It is marked as "potential issue"... What makes you think it is causing a leak in the NiceGUI library? Can we somehow reproduce a growth in memory consumption? |
Description
Even an empty auto-index page is leaking memory. This can be observed by regularly getting the process memory via
psutil
:The growth is 0 if you do nothing, but if you request the empty index page, the code reports a memory growth.
The text was updated successfully, but these errors were encountered: