bubbles icon indicating copy to clipboard operation
bubbles copied to clipboard

V1: Viewport Potentially Using a Bunch of RAM/Memory

Open pjkaufman opened this issue 4 months ago • 2 comments

Describe the bug I am working on a BubbleTea CLI app that allows me to fix potentially broken content in them. However I noticed that the TUI setup was using something like 20 to 40 MB of RAM in order to read in and do modifications on an EPUB that is only about 1 MB uncompressed. So I did a memory usage pprof. I assumed the issue was with the copying of the the model from one view instance to the next since all the text files are on the model and don't necessarily use a pointer. So I only used the RAM measurement on the TUI logic. To my surprise, the pprof seems to indicate that almost all of the RAM/memory allocations come from the viewport bubble and its use of lipgloss:

Image Image

It is possible that I did the pprof wrong, but it seems to be consistent in this memory pprof result.

Setup

  • OS: Windows (Running from WSL Under Ubuntu 24.04.1)
  • Shell: bash (?)
  • Terminal Emulator: Windows Terminal
  • Terminal Multiplexer: N/A
  • Locale:
$ locale
LANG=en_US.utf8
LANGUAGE=en_US.utf8
LC_CTYPE="en_US.utf8"
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE="en_US.utf8"
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=

Note: I have seen similar RAM usage levels of POP_OS! 22.04 using Kitty as the terminal emulator, but I have not done a pprof there at this time.

Here is the original pprof from the discord message that led to this issue being created and the pprof for the screenshots on this issue: profiles.zip

To Reproduce Steps to reproduce the behavior:

  1. Clone https://github.com/pjkaufman/go-go-gadgets/tree/RAM-Usage
  2. navigate to the base of the repo and then run the following command: go run ./epub-lint/ fixable -f ./epub-lint/cmd/testdata/original/jules-verne_from-the-earth-to-the-moon_ward-lock-co.epub --use-tui -a

You should see a screen something akin to

Image
  1. Enter ---- as the section break (it really does not matter so long as it is not something that would be common in books because that may result in more section breaks being identified in the book even though it should not affect the behavior seen on the ticket)
  2. Move back and forth on the suggestions view by clicking the right and left arrow keys (do this for about 10 to 20 seconds)
  3. Hit esc to exit, do not use Ctrl+c as that will result in the pprof not getting properly written
  4. Run go tool pprof -http=:8080 mem.pprof to see the pprof

Source Code Should be able to get the source code at https://github.com/pjkaufman/go-go-gadgets/tree/80fa36aea6997b899a98bfad277f70900dbcc84f

Expected behavior I expect the used memory to be much smaller than it currently is when using Bubble Tea. At most I am setting the content of the viewport to a couple hundred KB or maybe event an MB of content on screen. So I would expect the RAM/memory usage to be much closer to somewhere between 500KB to 1MB for the TUI in a worst case scenario for this file.

Additional context There are other issues that need working out in the current program including the UI not properly cleaning up previous view calls for some reason (it seems to only happen on larger screens). But resizing the terminal does fix the UI and does make it show properly again if you need to reference the controls of the program.

The pprof is happening here in the code: https://github.com/pjkaufman/go-go-gadgets/blob/80fa36aea6997b899a98bfad277f70900dbcc84f/epub-lint/cmd/check-for-manually-fixable-issues.go#L155-L165

The TUI logic resides in this folder: https://github.com/pjkaufman/go-go-gadgets/tree/80fa36aea6997b899a98bfad277f70900dbcc84f/epub-lint/internal/ui

Viewports are used in the fixable issue body here and in the suggestions stage for its scrollable view here.

Hopefully that helps in looking into this.

Thank you for all that you do and for a good library to make these TUIs!

Edit: made wording a little clearer for expected behavior.

pjkaufman avatar Aug 19 '25 13:08 pjkaufman

Although for v2, plug for https://github.com/charmbracelet/bubbles/pull/823, which could be upgraded to once v2 officially rolls out, and fixes all sorts of bugs, and performance issues (CPU, memory & allocations).

lrstanley avatar Aug 19 '25 13:08 lrstanley

Here is an update with an alloc based pprof instead of a heap based one:

alloc.zip

It looks like allocations is pretty similar to heap usage, but there are a lot more paths:

Image

pjkaufman avatar Aug 21 '25 14:08 pjkaufman