zed icon indicating copy to clipboard operation
zed copied to clipboard

Zed is leaking memory

Open berkus opened this issue 1 year ago • 16 comments

Check for existing issues

  • [X] Completed

Describe the bug / provide steps to reproduce it

I have upgraded to Zed 0.130.6 about yesterday and it's been running non stop since update.

I have installed some extensions, but other than that been just using Zed for its primary function - editing code.

Today macOS forced close citing Zed consuming over 62Gb of memory.

Environment

Zed: v0.130.6 (Zed) OS: macOS 14.3.0 Memory: 16 GiB Architecture: aarch64

If applicable, add mockups / screenshots to help explain present your vision of the feature

zed-leaking

If applicable, attach your ~/Library/Logs/Zed/Zed.log file to this issue.

the log seems to be overwritten by the next zed launch already...

berkus avatar Apr 14 '24 12:04 berkus

As a result, i've lost all open files projects state; the window that loaded had ENTIRE ssd drive added as a project (but old files from the project correctly opened); when i tried to re-add my project folder I had to remove the ENTIRE ssd from there first and thus I lost all the open files states, now it has a project I want in the sidebar but none of the previously open files are open anymore.

Recent projects look like this: image

berkus avatar Apr 14 '24 12:04 berkus

Hey! So, to be clear: what are the steps to reproduce? Open the whole harddrive as a folder in Zed? Or how did that happen?

mrnugget avatar Apr 15 '24 12:04 mrnugget

This may be a dupe of:

  • https://github.com/zed-industries/zed/issues/9744

Its hard to tell though, because we don't really know what is causing this.

JosephTLyons avatar Apr 15 '24 15:04 JosephTLyons

No, I didn't do anything except have my project open.

The opening of the whole harddrive happened after I restarted Zed - it was unwanted, to say the least.

berkus avatar Apr 15 '24 17:04 berkus

This may be a dupe of:

I was running 2 instances of Zed working between 2 repos with small codebases. Each repo has ~2k lines of code.

this part seems similar, I had a second instance running in the background, but I didn't use it actively.

berkus avatar Apr 15 '24 17:04 berkus

Same for me.

But in my case try to reproduce this scenario:

  1. Open any large project which causes Zed to run node ts langguage server process, for example https://github.com/vuetifyjs/vuetify
  2. Check RAM usage top/htop
  3. Open a bunch of different files ts, tsx, vue,... - around 15-20
  4. Look for the memory usage
  5. Try to close all opened tabs
  6. RAM usage will be the same, without freeing up space
  7. You can continue and open 10 more files (different from previous)
  8. RAM usage will be the same or in some cases will increase

I had another case when I tried to open 3GB file, Zed handled it perfectly, but after closing and removing this file - memory usage was really huge, so I handled it by restarting editor.

I'm running latest stable release at this moment.

joshuaia avatar Apr 15 '24 23:04 joshuaia

I see. Technically that's not a leak, I'd say, but more that MiMalloc (the allocator we use) is really aggressive about retaining memory. See this comment for more details: https://github.com/zed-industries/zed/pull/10321#issuecomment-2056763742

Makes me wonder whether we shouldn't switch from MiMalloc back to the default allocator that's less aggressive.

mrnugget avatar Apr 16 '24 07:04 mrnugget

This may be a dupe of:

Its hard to tell though, because we don't really know what is causing this.

Part of this is definitely MiMalloc as discussed in other threads such as https://github.com/zed-industries/zed/pull/10321#issue-2233490156 .

There are probably a few memory leaks here and there (even the best written rust programs may have those...) and MiMalloc makes them worse.

mocenigo avatar Apr 16 '24 22:04 mocenigo

I see. Technically that's not a leak, I'd say, but more that MiMalloc (the allocator we use) is really aggressive about retaining memory. See this comment for more details: #10321 (comment)

Makes me wonder whether we shouldn't switch from MiMalloc back to the default allocator that's less aggressive.

Hello, what steps should I take to suggest to revert back from MiMalloc?

The discussions about memory usage and retention, as well as the facf that MiMalloc does not play well with Instruments, suggests to me that it is a mistake to use it during development.

Zed (as in the company) can always use it to compile releases.

mocenigo avatar Apr 26 '24 08:04 mocenigo

So, i tried re-running latest version of zed and seeing how memory usage changes over time.

Zed: v0.132.3 (Zed)
OS: macOS 14.3.0
Memory: 16 GiB
Architecture: aarch64

Ran in the morning, RAM was really nice: about 256Mb total. Started working on the project - single Zed window, single project, about 20 files edited in total. Memory usage grew to 2.5Gb. And kept growing as I jumped from file to file.

Restarted Zed. Started idle at about 256Mb RAM.

Started working on the same project: was about 393Mb of RAM taken, I just clicked around through 4 or 5 tabs with already open files and memory usage grew by 5Mb (398Mb now).

Came back to Zed from this browser window and it grew to 400Mb, then fell down to 398Mb again.

Clicked through some tabs, memory usage grew to 403Mb then fell to 397Mb.

Clicked some not-yet-open files and usage jumped to 425Mb then quickly shrank to 415Mb.

Pretty sure the OS does memory page compression behind the scenes quite aggressively, so the taken RAM for idle files will quickly be moved to the compressed area.

I will keep it running for some time.

berkus avatar Apr 28 '24 07:04 berkus

So it keeps running, slowly leaking into the compressed memory:

After a day of doing almost nothing, it's at 555Mb RAM and grown into 389,5Mb compressed (from about 230Mb) - so it seems like memory is leaking all the time, slowly, never addressed again so the OS just moves it to the compressed part.

berkus avatar Apr 29 '24 17:04 berkus

Couple days passed, I didn't have any chance to open the laptop and use Zed at all.

However, it had consumed a lot more compressed memory now - meaning there's still some mem leaking and being compressed by the OS into the idle memory:

image

berkus avatar May 01 '24 07:05 berkus

So it keeps running, slowly leaking into the compressed memory:

After a day of doing almost nothing, it's at 555Mb RAM and grown into 389,5Mb compressed (from about 230Mb) - so it seems like memory is leaking all the time, slowly, never addressed again so the OS just moves it to the compressed part.

More than leaking it is MiMalloc that is very aggressive in not releasing the large chunks of memory (sometimes called arenas or pools) it then uses for carving the actual allocations out of them. In other words, once MiMalloc has gotten some memory from the system, it will likely not give it back for a LONG time, if at all. It is an ideal allocator for a computer game, not so sure for an editor, or a service. This also malkes it VERY difficult to do a proper analysis of memory leakagem since Instruments.app does not work well with MiMalloc.

@mrnugget and @mikayla-maki : can we please revert the use of MiMalloc on macOS?

mocenigo avatar May 01 '24 21:05 mocenigo

@mocenigo

From my personal observations, this repository favors "proof of work" model and creating a PR might be a faster way to fix the issue. This time it also took a conveniently small change, compared to the volume of words in any of the comments above: https://github.com/zed-industries/zed/pull/11293

SomeoneToIgnore avatar May 02 '24 08:05 SomeoneToIgnore

@mocenigo

From my personal observations, this repository favors "proof of work" model and creating a PR might be a faster way to fix the issue. This time it also took a conveniently small change, compared to the volume of words in any of the comments above: #11293

Thank you. Fact is, I am not versed in Rust yet, so I would just have removed the lines, and would not have made it into an optional feature…

mocenigo avatar May 02 '24 12:05 mocenigo

0.133.7 seems to be very stable about memory usage so far (I did use it very little on a few files, but it still seems to not leak much, if at all)

image

berkus avatar May 06 '24 09:05 berkus

Observed, Zed 0.133.7 is extremely stable with the memory, the compressed memory amount even goes down after a short time.

Will try upgrading to the next version.

berkus avatar May 08 '24 22:05 berkus

I think I'm going to close this issue, as it seems it's been resolved with the allocator changes, I'm going to close this for now. Thanks for the investigative work everyone!

mikayla-maki avatar May 09 '24 03:05 mikayla-maki