Skip to content

Instantly share code, notes, and snippets.

@korobochka
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korobochka/26d5256a4d7601e9347b to your computer and use it in GitHub Desktop.
Save korobochka/26d5256a4d7601e9347b to your computer and use it in GitHub Desktop.
unitleak

Тема работы: отслеживать, что все объекты, созданные в рамках юниттеста, собираются в конце него (что не происходит утечек памяти при повторных запусках теста).

Пример с JUnit:

public class SomeLeak {
    static ArrayList<String> strings = new ArrayList<String>();

    public void doSomething() {
        String s = "Some leaking string " + strings.size();
        strings.add(s);
    }
}

public class BaseTest {
    @Rule
    public Watcher watcher = new Watcher();
}

public class SomeLeakTest extends BaseTest {
    @Test
    @FindLeaks
    public void testDoSomething() throws Exception {
        SomeLeak l = new SomeLeak();
        l.doSomething();
    }
}

После запуска подобного кода SomeLeakTest будет помечен, как Failed, так как выделено 4 объекта, а освобождено 3.

Технологии: JVM TI агент на C++ (объекты отслеживаются с помощью инъекций байткода при загрузку классов), небольшой интерфейс к нему на Java, Watcher и аннотация FindLeaks для интеграции с JUnit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment