diff options
author | Branden Cash <203336+ammmze@users.noreply.github.com> | 2025-02-02 16:40:43 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-02 18:40:43 -0500 |
commit | d57fe4395e77b0b6f5eabccf890498049fabbcfe (patch) | |
tree | f01d2e2dd711ce93f93382a278baacb928273ba8 /klippy | |
parent | 0114d72a6cd8140d7dc80a590fc50e7073069edc (diff) | |
download | kutter-d57fe4395e77b0b6f5eabccf890498049fabbcfe.tar.gz kutter-d57fe4395e77b0b6f5eabccf890498049fabbcfe.tar.xz kutter-d57fe4395e77b0b6f5eabccf890498049fabbcfe.zip |
garbage_collection: freeze objects on klippy ready (#6794)
This significantly reduces the amount of data in the generation 2 garbage collection bucket from the initial startup of klipper.
Signed-off-by: Branden Cash <203336+ammmze@users.noreply.github.com>
Diffstat (limited to 'klippy')
-rw-r--r-- | klippy/extras/garbage_collection.py | 31 | ||||
-rw-r--r-- | klippy/toolhead.py | 2 |
2 files changed, 32 insertions, 1 deletions
diff --git a/klippy/extras/garbage_collection.py b/klippy/extras/garbage_collection.py new file mode 100644 index 00000000..da51df3b --- /dev/null +++ b/klippy/extras/garbage_collection.py @@ -0,0 +1,31 @@ +# Garbage collection optimizations +# +# Copyright (C) 2025 Branden Cash <ammmze@gmail.com> +# +# This file may be distributed under the terms of the GNU GPLv3 license. +import gc +import logging + +class GarbageCollection: + def __init__(self, config): + self.printer = config.get_printer() + # feature check ... freeze/unfreeze is only available in python 3.7+ + can_freeze = hasattr(gc, 'freeze') and hasattr(gc, 'unfreeze') + if can_freeze: + self.printer.register_event_handler("klippy:ready", + self._handle_ready) + self.printer.register_event_handler("klippy:disconnect", + self._handle_disconnect) + + def _handle_ready(self): + logging.debug("Running full garbage collection and freezing") + for n in range(3): + gc.collect(n) + gc.freeze() + + def _handle_disconnect(self): + logging.debug("Unfreezing garbage collection") + gc.unfreeze() + +def load_config(config): + return GarbageCollection(config) diff --git a/klippy/toolhead.py b/klippy/toolhead.py index f1a9afda..e15f987e 100644 --- a/klippy/toolhead.py +++ b/klippy/toolhead.py @@ -290,7 +290,7 @@ class ToolHead: self._handle_shutdown) # Load some default modules modules = ["gcode_move", "homing", "idle_timeout", "statistics", - "manual_probe", "tuning_tower"] + "manual_probe", "tuning_tower", "garbage_collection"] for module_name in modules: self.printer.load_object(config, module_name) # Print time and flush tracking |