Giter VIP home page Giter VIP logo

gc-maybe's Introduction

gc-maybe (or the Garbage Collector Maybe Trick)

I have used GCMH (the Garbage Collector Magic Hack) for many years. It’s great.

GCMH hooks itself in pre/post-command hooks. It (re)sets its idle timer on every command, which seems to be a good strategy, but also feels a bit too much.

Conversely, all gc-maybe-mode does is this:

  • with a predefined idle timer (default is 5 seconds), call garbage-collect-maybe (from Emacs 28.1 or above) with a factor based on the current gc-cons-percentage, so it garbage collects if needed;
  • add hooks to the minibuffer-setup-hook (raising the GC threshold), minibuffer-exit-hook (restoring the GC threshold), and the post-gc-hook (for optional stats in a buffer);

That’s it. It is pretty much the same GCMH hack, but as a cheap trick.

With custom advices, we can raise the threshold (gc-maybe-raise-threshold or gc-maybe-raise-threshold-briefly) for known costly functions; with a “restore” timer, we reset to defaults shortly after raising them (gc-maybe-restore-threshold).

Setup

I’m doing this in the very beginning of my init.el file (before bootstrapping straight.el), so adapt accordingly:

(load "~/.emacs.d/site-lisp/gc-maybe/gc-maybe.el" nil t)
(gc-maybe-raise-threshold)
(add-hook 'after-init-hook 'gc-maybe-restore-threshold 99)

Then, in my config file, with numbers that seems to better fit my machine:

(use-package gc-maybe
  :straight (:local-repo "~/.emacs.d/site-lisp/gc-maybe")
  :init
  (setopt gc-maybe-threshold-default (* 16 1024 1024)) ; 16 MiB
  (setopt gc-maybe-percentage-default 0.2)
  (gc-maybe-mode +1) ; set minibuffer hooks and idle timer
  :config
  ;; optionally log GCs in a buffer
  (setopt gc-maybe-log-stats t)
  ;; example function advice
  (advice-add 'save-buffers-kill-emacs :before #'gc-maybe-raise-threshold))

Related

gc-maybe's People

Contributors

bcardoso avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.