lem icon indicating copy to clipboard operation
lem copied to clipboard

Memory leak in SDL2 build

Open sbenitezb opened this issue 1 year ago • 9 comments

I compiled Lem from source on macOS/ARM and there appears to be a memory leak, as the running image grows continuously above 1GB.

sbenitezb avatar May 12 '24 19:05 sbenitezb

Does it grow on it's own? I just tried it and for me it only grows when I scroll in a text buffer. According to (room) lisp doesn't know about the growth. I'm guessing some of the textures aren't being freed properly.

BierLiebHaber avatar May 13 '24 21:05 BierLiebHaber

I left it over night and didn't grow a lot, to 1GB. But if I scroll, yeah it jumps by around 100MB. Most of it seems to be released often, but not all, and so it will start accumulating over time.

sbenitezb avatar May 14 '24 10:05 sbenitezb

It might not be a leak after all. 🤔 Can you try (lem-sdl2/text-surface-cache:clear-text-surface-cache) when the memory usage is high? For me it reclaimed most of the memory. Edit: I tried again after a while and it doesn't seem to reclaim all of it

BierLiebHaber avatar May 14 '24 15:05 BierLiebHaber

I managed to get memory usage up to 5GB by opening micros.lisp then pressing and holding the right arrow key until I reached the end of the file.

BierLiebHaber avatar May 14 '24 21:05 BierLiebHaber

After disabling the text-surface-cache I retried moving through micros.lisp and the memory footprint stayed below 600MB. I disabled the cache by adding #+() to https://github.com/lem-project/lem/blob/60a8d57a676b487699ef6147cf0cff6e67800863/frontends/sdl2/text-surface-cache.lisp#L25-L29

What's the reason for the surface-cache anyways? Rendering doesn't seem slower without it and it causes problems when it gets really big (backspace with a big cache feels unresponsive). At the very least there should be something cleaning up old entries so it doesn't grow to a ridiculous size.

Edit: interestingly clearing the cache while going though the file doesn't seem to have the same effect

BierLiebHaber avatar May 21 '24 21:05 BierLiebHaber

Thank you for your research. It is true that this cache is not very useful. I remember that when I was drawing characters one by one, the process of creating the surface was very slow and I implemented this cache. But, the current situation is that the surface is created in larger units, so the effect may be lost.

cxxxr avatar May 22 '24 14:05 cxxxr

according to #1255 we are still creating 1 surface per character if line-wrapping is disabled, correct? If that's the case, then cache is probably still useful.

A compromise might be to either only enable the cache if line-wrapping is disabled, or only enable the cache for small strings.

BierLiebHaber avatar May 22 '24 17:05 BierLiebHaber

realted issue: https://github.com/lem-project/lem/issues/1332#issue-2177441613

cxxxr avatar Jul 15 '24 07:07 cxxxr

Still existed. Steps to reproduce:

  1. Open a text buffer.
  2. Navigating your cursor.
  3. Each time your cursor moved to a new position, the memory usage grow.

sakurawald avatar Dec 05 '24 07:12 sakurawald