The original magic Emacs garbage collection hack (2019)
akrl.sdf.org
akrl.sdf.org
(add-function :after
after-focus-change-function
(lambda () (unless (frame-focus-state) (garbage-collect))))
So Emacs GCs whenever it loses focus (i.e. I'm looking at some docs in a browser or whatever). This works great for me - it's rare that I notice it still cleaning up when I switch back.Times when you're just staring at compiler output or something heavy like that, and now you need a different GC trigger to save the day... and if you have that, then you do not need the trick.
Of course, you'll probably blame both of those on whatever application you're using, so in a way from emacs's point of view this is all free!
I'm not saying the underlying idea is bad. Using idle time for GC is good. 1GB just seems like an awfully high threshold for non-idle time. It's giving up on the idea of "small enough to be unnoticeable" pauses. (Though the right way to do that would be to have an incremental GC, but that has costs of its own.)
Personally, I guess I'd want to try a lower non-idle heap threshold, plus a shorter idle time threshold (15 seconds seems quite long, and a shorter time increases the probability of the GC'd pages still being in RAM), plus using focus as an idle signal.
And maybe play with using variable heap size thresholds, eg base the trigger off of both how long emacs has been idle plus the size of the heap just after the previous collection. So you might say that the heap threshold is 100% of the previous heap size at 15 seconds, 400% at 5 seconds, 1000% when non-idle. Or min(10x previous, 20MB) when non-idle, perhaps, where 20MB is adjusted to take an amount of time that is barely noticeable? But that's adding complexity. I guess I should enable GC logging to see how long these collections typically take; I haven't noticed a lot of pauses in the first place.
If emacs already had this, then none of these GC hacks would be needed?
It's _not_ the default in any official release, yet. Although plenty of distros have either a separate package enabling it (or have enabled it in the main package)
Would be interesting to see how many users are actually using it on computers made in the last century, where the default strategy might be more appropriate.
But it’s still interesting to me that this thread is filled with users talking about their techniques for forcing a GC while they’re getting coffee or switching tabs.
The fact that in 2024 we’re talking about users (expert users, to be fair) changing setting in their apps to optimize GC pauses makes me think something has gone wrong somewhere.
But maybe I’m just being mean because I mostly use C++.
Goes to show that it’s trade-offs all the way down.
As I said, Emacs has many ancient defaults that many people have to fiddle with to make the editor theirs. Some of them is because of a slow drift of user preferences over the generations. But memory management strategy is one area where objectively a new default strategy would make a lot of sense. One which automatically adapts to RAM size, since Emacs scales down so well, unlike its competing memory guzzlers.
The reality is emacs is complex but today computers with 8GBs of ram has about 1000x memory of the original emacs target (a machine with 8 MB of RAM) and even faster processors.
Now I am in love with vscode, but I think I need to come back to emacs :) sometimes
It's not straightforward to guess where this threshold is because it depends on the behavior of other applications. The OS could already be swapping before your process even started.
(setq gc-cons-threshold 100000000) (add-function :after after-focus-change-function 'garbage-collect)
(setq gc-cons-threshold most-positive-fixnum)
I have 256GB RAM locally so I might as well use it.
Here's an older geekbench report: https://browser.geekbench.com/v5/cpu/3159963
Even setting it to 256GB is a much better idea than just turning off GC. I recommend to maybe halve that to leave just enough RAM for Chrome.
maybe emacs just doesn't use enough memory, relative to modern systems, for GC to be worth it.
(i do quit emacs at the end of each day, so that is a form of GC.)
You can be incorrect, or you can try it.
I've been running emacs since I logged in at 9am, edited ~100 different files. Many LSP inferior-processes running and emacs is currently sitting at 1 whole gigabyte resident memory.
What's more is that I would say this is a particularly light day for me and emacs.
I have literally never, never, not once ever had an issue with this much RAM and essentially turning gc off in emacs.
Yes, but which year ?
But how much swap space do you have?