Skip to content

Memory Leaks

stubb edited this page Feb 18, 2013 · 1 revision

One way to find memory leaks in the game is to use objgraph. You should read (and try the example) on that page before trying it on Unknown Horizons.

Example: finding out whether the session is still alive

Add something like the following to a place where the session should not be alive (if it is then there is a leak). One possibility is the main menu: that way one can start a game, cancel it, and get the feedback when the main menu is displayed again. To do this one can add the following code snippet to the end of the show_main function in horizons.gui/gui.py

import gc
import objgraph
gc.collect()
sessions = objgraph.by_type('SPSession')
print 'Found ', len(sessions), 'live sessions'
if sessions:
    objgraph.show_backrefs(sessions[0], filename='session_backrefs.png', max_depth=2)

This example would find all objects of SPSession class and create a file called 'session_backrefs.png' in the game's root folder for the first SPSession instance (if any). It is useful to run garbage collection before doing it so that any garbage can be removed.

After that one can try to interpret the graph on the picture to find anything that is there although it shouldn't. Note that not every reference is necessarily bad: it may also be the case that a single reference is keeping a huge graph alive and removing that would make it possible to garbage-collect all the others as well.

There are other useful options in the objgraph library which are worth looking at and one would often also use different arguments to the show_backrefs call depending on that exact problem one is dealing with.

Clone this wiki locally