Top
Best
New

Posted by thorel 20 hours ago

Finding and fixing Ghostty's largest memory leak(mitchellh.com)
529 points | 117 comments
quantummagic 18 hours ago|
This is great news! Well done to everyone who helped sort it out. It was a problem noted by users in a thread here just last week, https://news.ycombinator.com/item?id=46460319

While Claude Code might have been the reason this bug became triggered by more people, there are some of us who were hitting it without ever having used Claude Code at all. Maybe the assumption about what makes a page non-standard, isn't as black-and-white as presumed. And I wonder if the leak would have been triggered more often for people who use scrollback-limit = 0, or something very small.

Probably not a huge deal, but it does seem the fix will needlessly delete and recreate non-standard pages in the case where the new page needs to be non-standard, and the oldest one (that needs to be pruned) already is non-standard and could be reused.

mitchellh 18 hours ago||
> Probably not a huge deal, but it does seem the fix will needlessly delete and recreate non-standard pages in the case where the new page needs to be non-standard, and the oldest one (that needs to be pruned) already is non-standard and could be reused.

This is addressed in the blog post.

It is how the PageList has always worked, and also how it worked before with the bug, because during capacity adjustment we would see the wrong size. This shouldn't change any perceived performance.

And as I note in the blog post, there are alternative approaches such as the one you suggested, but we don't have enough empirical data to support changing our viewpoint on that whereas our current viewpoint (standard sizes are common) is well supported by known benchmarks. I'm open to changing my mind here, but I didn't want to change worldviews AND fix the leak in the same go.

fartfeatures 11 hours ago|||
How come this isn't released as a hotfix / out of band patch but will follow the standard release cycle in March?
fragmede 15 hours ago|||
Of all the things to be impressed by you about, your patience is commendable. I'd be losing my shit if someone couldn't be bothered to read what I wrote and just spout off about something I'd addressed in my writing, but I suppose that's why your bank account has two commas and a bunch more. Thank you for everything. Can we go flying sometime?
ATMLOTTOBEER 32 minutes ago||
Simpin
commandersaki 13 hours ago|||
> Well done to everyone who helped sort it out. It was a problem noted by users in a thread here just last week

I'm feeling a bit lucky I was able to sneak in an issue during the beta phase, but it was a real reproducible one that led to a segfault.

macote 17 hours ago|||
The thread about memory leak is here: https://news.ycombinator.com/item?id=46461061
Maxious 7 hours ago||
And the same diagnosis in the blog post was reported by a user in discussions a month ago but ignored https://github.com/ghostty-org/ghostty/discussions/9786#disc...
julien_p 5 hours ago||
That doesn't sound like the actual issue, or am I not understanding it correctly?
dkdcio 3 hours ago||
I think you’re correct. the reproduction isn’t very precise and the solution doesn’t seem right (I’m not seeing anything about the non-standard pages not being freed). I’d guess this was ignored because it was wrong…
larodi 9 hours ago||
As a side note - Claude Code is making the CLI attractive in a renewed fashion - more than anything else did it last 20years.
bschwarz 7 minutes ago||
[delayed]
jrpelkonen 17 hours ago||
Great write-up. And, thanks mitchellh for Ghostty, I switched to it last year, and have not regretted it.

However, I am a somewhat surprised that the fix is reserved for a feature release in a couple of months. I would have expected this to be included in a bug fix release.

msephton 14 hours ago|
It's already released in the latest nightly build.
DrammBA 12 hours ago||
Are the nightly releases the expected way to get timely bugfixes?
amazingman 9 hours ago||
That is how software releases generally work. AFAICT this is not a bug with broad impact or security implications.
fartfeatures 7 minutes ago||
I guess thats arguable, a memory leak can make a system unpleasant to use although I accept it can be solved by repeatedly restarting the offending app.
reactordev 13 hours ago||
The moment you started talking about pages, I was like: “Ok, obviously memory pooled” and yup, it is. Then I said “obviously ring buffered” and yeah, essentially your scroll back reuse. Then I knew exactly where the bug was before getting to that part, not freeing the pages memory properly and sure enough - bingo! With some great looking diagrams of memory space alignment.

Kudos, that was a good read. Just remember that every time you do something novel, there’s potential for leaks :D

jhhh 8 hours ago||
This feels like a case of guessing at something you could know. There are two types of allocations that each have a size and free method. The free method is polymorphic over the allocations type. Instead of using a tag to know absolutely which type an object it is you guess based on some other factor, in this case a size invariant which was violated. It also doesn't seem like this invariant was ever codified otherwise the first time a large alloc was modified to a standard size it would've blown up. It's worth asking yourself if your distinguishing factor is the best you can use or perhaps there is a better test. Maybe in this case a tag would've been too expensive.
neobrain 16 hours ago||
Funny timing, I moved to Ghostty this week and just today I ran into OOM crashes in Ghostty while developing a terminal UI app. Coincidentally this TUI has a tab bar that looks like this, where UTF8 icons are used for recognizability and activity indicators (using © and € as placeholders here):

    1|Flakes ©    2|Installed ©    3|Store © €    4|Security © €
   ──────────────────────────────────────────────────────────────
This works fine normally, but resizing the terminal would quickly trigger the crash - easy to avoid but still annoying!

I was already preparing myself to file a bug report with the easy repro, but this sounds suspiciously close to what the blog post is describing. Fingers crossed :)

(EDIT: HN filters unicode, booo :( )

smoyer 3 hours ago|
Why would I move to GhosTTY versus the terminal emulator that comes with my OS as it's not clear to me from the documentation?
neobrain 2 hours ago||
I don't think I can do a better overview than https://ghostty.org/docs/about . It's not world-changing but simply a very polished, well-executed terminal.

GPU rendering virtually eliminates typing latency. Most terminals that have it don't support native content like tabs, but Ghostty gets minimal latency without having to compromise on essentials since it uses native toolkits under the hood.

The modern TTY has lots of protocol extensions that allow your CLI tools to do things like display high-resolution images. There's tons of good-quality color themes out-of-the-box (with a built-in browser for preview).

Configuration is highly customizable but the defaults are good enough that you barely need it.

hotpotat 17 hours ago||
@mitchellh what did you use for the memory visualizations? Looks nice, and the website plays well with mobile. Whats the stack?
mitchellh 17 hours ago|
Static HTML/CSS generated by Opus 4.5.

I like using AI for visualizations because it is one-time use throwaway code, so the quality doesn't matter at all (above not being TOTALLY stupid), it doesn't need to be maintained. I review the end result carefully for correctness because it's on a topic I'm an expert of.

I produce non-reusable diagrams namespaced by blog post (so they're never used by any other post). I just sanity check that the implementation isn't like... mining bitcoin or leaking secrets (my personal site has no secrets to build) or something. After that, I don't care at all about that quality.

The information is conveys is the critical part, and diagrams like this make it so much more consumable for people.

63 17 hours ago|||
That's really cool. I was looking at them and thinking "I could probably make these with vanilla html/css but it'd be pretty tedious." Perfect use case for AI. I need to work on developing a reflex for it.
mjn 17 hours ago||
I've also started doing this, and it's surprisingly enjoyable to both do and even to read. The end result is often more readable to me than using a 3rd-party JS visualization library, because I only need to know standard HTML/CSS concepts to understand what's going on. And a side benefit is smaller pages with less bitrot due to being able to skip the dependencies.
hotpotat 17 hours ago|||
That’s reasonable, thanks!
stephc_int13 15 hours ago||
I've been following the development of Ghostty for a while and while I have the feeling that there is a bit of over-engineering in this project, I find this kind of bug post mortem to be extremely valuable for anyone in love with the craft.
trevorhinesley 12 hours ago|
Over-engineered in what way?
nesarkvechnep 9 hours ago|||
It’s just a feeling, man.
cbmuser 7 hours ago|||
Having to introduce a new language stack to distributioms just to be able to build a terminal emulator is what I would consider over-engineering.
weebull 5 hours ago|||
A 50-ish MB build time dependency that doesn't need any special privileges or installation to run? That's over engineering? A binary release of just CMake is bigger than all of Zig.
surajrmal 6 hours ago|||
So anything that uses a less popular language is considered over engineering? Distros support lots of different languages already and there are likely other packages built with zig already.
bryancoxwell 17 hours ago||
Super accessible write up as someone unfamiliar with Ghostty and terminal emulators in general. Thanks!
kepano 18 hours ago|
Reliable reproductions are so valuable.
More comments...