nicegui: High Memory Usage in SPA Implemented with NiceGUI - Anyone Else Encountering This?
First Check
- I added a very descriptive title here.
- This is not a Q&A. I am sure something is wrong with NiceGUI or its documentation.
- I used the GitHub search to find a similar issue and came up empty.
Example Code
Description
I’ve noticed that the memory usage of an SPA (Single Page Application) I built with NiceGUI keeps increasing significantly. Even with very low traffic, the memory gets exhausted quickly. About a month ago, I spent some time investigating the issue, and I strongly suspect that the memory leak is primarily coming from NiceGUI itself. Based on version 2.10.1 at the time, my main findings were as follows:
- Using
BindablePropertycausedbindable_propertiesto hold long-term references to objects, leading to memory leaks. - There were a large number of circular object references in memory.
I made some modifications myself, but I wasn’t fully satisfied with the results, so I never got around to organizing a report to post here.
That said, NiceGUI has been improving very quickly. Looking at the latest code today, it seems the bindable_properties issue might be resolved, as it now uses weakref. However, the problem of circular references still appears to persist.
For example, on a page displaying many ui.chat_message objects, after navigating away from that page, I can still see a significant number of ui.chat_message instances lingering in memory when using objgraph. I randomly picked one and generated a backrefs graph, which looks like this:
The references from the coroutine in the graph are likely overhead from the objgraph code itself, so they can be ignored.
Beyond that, this graph doesn’t show the full picture of references. Comparing it to NiceGUI’s code, there are actually many more circular references. For instance, an element references a Props object, which in turn directly references the element back. In my test modifications, I broke these kinds of circular references after deleting elements, and I observed a noticeable improvement in memory usage—no more obvious remnants of deleted elements lingering in memory.
Currently, I’m busy porting my previous test modifications to the latest NiceGUI code branch for further testing.
In the meantime, I find it strange—if no one else is encountering this same issue, could it be something wrong with how I’m using NiceGUI? My approach to implementing the SPA is to clear all elements from the container holding the page content when switching pages, then recreate all the necessary elements from scratch.
Has anyone else run into similar memory usage problems? I’d appreciate any insights or suggestions!
NiceGUI Version
2.13
Python Version
3.12.8
Browser
Chrome
Operating System
Linux
Additional Context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 39 (12 by maintainers)
I’m closing this issue because we haven’t heard any updates for one month now and we still can’t reproduce the problem. Feel free to re-open and continue the discussion once you have new information. Thanks!
@RhythmicGaming there would be two more things you could try:
uvicorn main:app --reload --port 8080Thanks
I’m actually using debian I guess at source: buildpack-deps:bullseye
docker_packages.txt: aiofiles==24.1.0 aiohappyeyeballs==2.6.1 aiohttp==3.11.13 aiosignal==1.3.2 annotated-types==0.7.0 anyio==4.8.0 attrs==25.3.0 bidict==0.23.1 certifi==2025.1.31 cffi==1.17.1 charset-normalizer==3.4.1 click==8.1.8 cryptography==44.0.2 docutils==0.21.2 fastapi==0.115.11 frozenlist==1.5.0 h11==0.14.0 httpcore==1.0.7 httptools==0.6.4 httpx==0.28.1 idna==3.10 ifaddr==0.2.0 itsdangerous==2.2.0 Jinja2==3.1.6 markdown2==2.5.3 MarkupSafe==3.0.2 multidict==6.1.0 narwhals==1.30.0 nicegui==2.13.0 orjson==3.10.15 packaging==24.2 plotly==6.0.0 propcache==0.3.0 pscript==0.7.7 pycparser==2.22 pydantic==2.10.6 pydantic_core==2.27.2 Pygments==2.19.1 pymongo==3.12.3 PyMySQL==1.1.1 python-dateutil==2.9.0.post0 python-dotenv==1.0.1 python-engineio==4.11.2 python-multipart==0.0.20 python-socketio==5.12.1 PyYAML==6.0.2 redis==5.2.1 requests==2.32.3 simple-websocket==1.1.0 six==1.17.0 sniffio==1.3.1 starlette==0.46.1 typing_extensions==4.12.2 urllib3==2.3.0 uvicorn==0.23.2 uvloop==0.21.0 vbuild==0.8.2 watchfiles==1.0.4 websockets==15.0.1 wsproto==1.2.0 yarl==1.18.3
which is consistent to my requirements.txt:
nicegui==2.13 plotly redis pymysql python-dateutil pymongo==3.12.3 cryptography uvicorn==0.23.2
(upgrading uvicorn to 0.34.0.did not improve anything)
which proves the point I think you were trying to make of: os.environ[“UVICORN_WORKERS”] = “1” and os.environ[“OMP_NUM_THREADS”] = “1”
are pointless lines
My code is a little messy, 😃 So I am working hard to isolate the real culprit myself. Maybe it is just bug in my code.
I don’t know why the bad process not got killed by OS. I used to have another bug that exhausted memory did got killed by OOM. But OOM did not happen in this case. Maybe because I restarted OS before OOM would happen.
This issue is probably not related to Docker, because it happens reliably in my dev server (which is good for debugging), where it does not run in Docker.
My app is basially a AI chatbot using langchain, and depends many other libraries. There are too may things are suspicious.
I also made a simple test program similar to the code posted by @rodja. It works without leaking memory.
I am still debugging. I will post new info about what I find later.