Skip to content

Instantly share code, notes, and snippets.

@mattharrison
Created December 9, 2010 03:43
Show Gist options
  • Save mattharrison/734308 to your computer and use it in GitHub Desktop.
Save mattharrison/734308 to your computer and use it in GitHub Desktop.
using gc in qtile
diff --git a/libqtile/manager.py b/libqtile/manager.py
index 17175de..5283b01 100644
--- a/libqtile/manager.py
+++ b/libqtile/manager.py
@@ -596,6 +596,21 @@ class Log:
def clear(self):
self.log = []
+def get_leaking_objects():
+ """
+ from http://blog.ccpgames.com/kristjan/2010/12/08/finding-c-reference-leaks-using-the-gc-module/
+ """
+ import gc
+ #create a dict of ids to objects
+ all = dict((id(i), i) for i in gc.get_objects())
+
+ #find all the objects that aren't referred to by any other object
+ ids = set(all.keys())
+ for i in all.values():
+ ids.difference_update(id(j) for j in gc.get_referents(i))
+
+ #this then is our set of objects without referrers
+ return [all[i] for i in ids]
class Qtile(command.CommandObject):
"""
@@ -606,6 +621,7 @@ class Qtile(command.CommandObject):
_testing = False
_logLength = 100
def __init__(self, config, displayName=None, fname=None, testing=False):
+ self.prev = None
self._testing = testing
if not displayName:
displayName = os.environ.get("DISPLAY")
@@ -979,11 +995,20 @@ class Qtile(command.CommandObject):
self.xpoll()
self.conn.flush()
hook.fire("tick")
+ self.get_leaks()
except:
# We've already written a report.
if not self._exit:
self.writeReport(traceback.format_exc())
+ def get_leaks(self):
+ if self.prev is None:
+ self.prev = get_leaking_objects()
+ else:
+ cur = get_leaking_objects()
+ print "LEN LEAK", len(cur)
+ print "DIFF", [x for x in cur if x not in self.prev]
+ self.prev = cur
def find_screen(self, x, y):
"""
Find a screen based on the x and y offset.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment