nicegui: Client-Side Memory Leak

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

from nicegui import ui
from datetime import datetime

class TimeManager:
    def __init__(self):
        self.setup_ui()

    def setup_ui(self):

        with ui.row().classes('flex w-full justify-between'):
            ui.label('Current Time : ').classes('flex-start')
            self.clock_label = ui.label('...').classes('flex-end')

        ui.timer(0.1, self.update_clock)

    def update_clock(self):
        """Update the clock label with the current time."""
        current_time = datetime.now()
        self.clock_label.set_text(f'{current_time:%Y-%m-%d %H:%M:%S}')

def build_gui():
    ui.separator()
    with ui.row().classes('w-full no-wrap'):
        with ui.column().classes('md:w-4/12 w-full'):
            TimeManager()

if __name__ in {"__main__", "__mp_main__"}:
    
    # ui.image('https://picsum.photos/id/377/640/360')

    build_gui()

    ui.run()

Description

Hopefully this is just something I am doing wrong and is a quick fix! I’ve been encountering a slow memory leak in the client-side (browser tab) that seems to be tied to a simple static ui.image(). I have tried to provide a single file MWE for testing. The layout is similar to the larger application I am developing, where there are several separate class files that are instantiated by the main file. It can likely be made even simpler, but maybe the error is in the order.

In any case, if the image is uncommented, I get a slow leak. If I omit the image, there is no leak. The timer refresh is fast only as an example, and doesn’t seem to leak on its own. I test for leaks by right clicking in Google Chrome and using the Inspect, then recording a couple hundred seconds of the performance with the memory button checked.

Here’s no leak with just the timer Image

And here’s the leak with the ui.image uncommented.

Image

NiceGUI Version

2.19.0

Python Version

Python 3.10.12

Browser

Chrome

Operating System

macOS

Additional Context

No response

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 28 (12 by maintainers)

Most upvoted comments

Also no leak with the Options API:

<html>
  <head>
    <link href="https://fonts.googleapis.com/css?family=Roboto:500|Material+Icons" rel="stylesheet" type="text/css" />
    <link href="https://cdn.jsdelivr.net/npm/quasar@2.17.3/dist/quasar.prod.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div id="q-app">
      {{ currentTime }}
      <q-img v-for="i in 1000" :key="i" style="width: 50px" />
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/quasar@2.17.3/dist/quasar.umd.prod.js"></script>
    <script>
      const { createApp } = Vue;
      const app = createApp({
        data() {
          return { currentTime: "" };
        },
        mounted() {
          setInterval(() => (this.currentTime = Date.now()), 100);
        },
      });
      app.use(Quasar);
      app.mount("#q-app");
    </script>
  </body>
</html>

#4859 Comes in real handy: We’ll use ElementPlus’s image, then!

from nicegui import ui, app
from time import time

ui.add_body_html('''
    <link rel="stylesheet" href="//unpkg.com/element-plus/dist/index.css" />
    <script defer src="https://unpkg.com/element-plus"></script>
''')
app.config.vue_config_script += '''
    app.use(ElementPlus);
'''

with ui.row():
    for i in range(1000):
        ui.element('el-image')

label = ui.label()
ui.timer(0.1, lambda: label.set_text(time()))

ui.run()
Image

So Quasar’s issue?

I think the heap snapshot distinguishes between ui.image, BoringElement and ui.element('div'). The memory timeline isn’t accurate because it doesn’t consider GC.

ui.image (modified, see below)

Image

BoringElement

Image

ui.element('div')

Image

Remember to use a new tab for each tested element!

My ui.image is modified, and it’s just an empty shell:

export default {
  template: `
    <q-img></q-img>
  `,
};

And yes, it doesn’t display an image.

Now it looks like the issue is in Quasar’s QImg, then.