aboutsummaryrefslogtreecommitdiffstats
path: root/klippy/extras/garbage_collection.py
diff options
context:
space:
mode:
authorBranden Cash <203336+ammmze@users.noreply.github.com>2025-02-02 16:40:43 -0700
committerGitHub <noreply@github.com>2025-02-02 18:40:43 -0500
commitd57fe4395e77b0b6f5eabccf890498049fabbcfe (patch)
treef01d2e2dd711ce93f93382a278baacb928273ba8 /klippy/extras/garbage_collection.py
parent0114d72a6cd8140d7dc80a590fc50e7073069edc (diff)
downloadkutter-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/extras/garbage_collection.py')
-rw-r--r--klippy/extras/garbage_collection.py31
1 files changed, 31 insertions, 0 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)