branch: externals/gcmh commit 5bf0ed3384ea5dd43c5ca007603f7b05aac5d2cf Author: Stuart Hickinbottom <stu...@hickinbottom.com> Commit: Stuart Hickinbottom <stu...@hickinbottom.com>
Prevent duplicate idle timers being created I found that when combined with desktop-save-mode, upon session reloading in a new Emacs instance, then gcmh-mode would be enabled multiple times even when it was already active. This causes multiple duplicate idle timers to be created (one for each time the mode is enabled). The same effect can be provoked by manually running (gcmh-mode t) multiple times. The effect of this is garbage collection being executed multiple times each time the idle time expired. I noticed this because GC was slower than expected, and turning on gcmh-verbose revealed all those duplicate GC events each time Emacs had been idle. This change simply removes any extant timer before enabling the mode so we're guaranteed to only have one or zero idle timers defined. --- gcmh.el | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gcmh.el b/gcmh.el index d2eae63..3564e48 100644 --- a/gcmh.el +++ b/gcmh.el @@ -86,6 +86,10 @@ This is to be used with the `pre-command-hook'." "Minor mode to tweak Garbage Collection strategy." :lighter " GCMH" :global t + + ;; Cancel any pending timer (prevents duplicate idle timers). + (when (timerp gcmh-idle-timer) + (cancel-timer gcmh-idle-timer)) (if gcmh-mode (progn (setq gc-cons-threshold gcmh-high-cons-threshold @@ -94,7 +98,6 @@ This is to be used with the `pre-command-hook'." #'gcmh-idle-garbage-collect)) ;; Release severe GC strategy before the user restart to working (add-hook 'pre-command-hook #'gcmh-set-high-threshold)) - (cancel-timer gcmh-idle-timer) (setq gc-cons-threshold gcmh-low-cons-threshold gcmh-idle-timer nil) (remove-hook 'pre-command-hook #'gcmh-set-high-threshold)))