All Projects → Sandec → JMemoryBuddy

Sandec / JMemoryBuddy

Licence: Apache-2.0 license
No description or website provided.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to JMemoryBuddy

Javafx Gradle Plugin
Gradle plugin that makes it easy to work with JavaFX 11+
Stars: ✭ 214 (+386.36%)
Mutual labels:  javafx
Recaf
The modern Java bytecode editor
Stars: ✭ 3,374 (+7568.18%)
Mutual labels:  javafx
Lifetimetracker
Find retain cycles / memory leaks sooner.
Stars: ✭ 2,529 (+5647.73%)
Mutual labels:  memory-leaks
Dev Tools
The most popular software developer tools in one app
Stars: ✭ 221 (+402.27%)
Mutual labels:  javafx
Chart Fx
A scientific charting library focused on performance optimised real-time data visualisation at 25 Hz update rates for data sets with a few 10 thousand up to 5 million data points.
Stars: ✭ 227 (+415.91%)
Mutual labels:  javafx
mem usage ui
Measuring and graphing memory usage of local processes
Stars: ✭ 124 (+181.82%)
Mutual labels:  memory-leaks
Jitwatch
Log analyser / visualiser for Java HotSpot JIT compiler. Inspect inlining decisions, hot methods, bytecode, and assembly. View results in the JavaFX user interface.
Stars: ✭ 2,545 (+5684.09%)
Mutual labels:  javafx
dbfx
This is a free, cross platform, open source database management tool based on JavaFX and vertx SQL client.
Stars: ✭ 63 (+43.18%)
Mutual labels:  javafx
Storagesystem
🗒️ Personal Stock Control System
Stars: ✭ 236 (+436.36%)
Mutual labels:  javafx
Heapinspector For Ios
Find memory issues & leaks in your iOS app without instruments
Stars: ✭ 1,819 (+4034.09%)
Mutual labels:  memory-leaks
Dashboardfx
JavaFx Dashboard
Stars: ✭ 219 (+397.73%)
Mutual labels:  javafx
Gmapsfx
Java API for using Google Maps within a JavaFX application.
Stars: ✭ 233 (+429.55%)
Mutual labels:  javafx
cpp-tutor
Code examples for tutoring modern C++
Stars: ✭ 52 (+18.18%)
Mutual labels:  memory-leaks
Javafx Maven Plugin
Maven plugin to run JavaFX 11+ applications
Stars: ✭ 213 (+384.09%)
Mutual labels:  javafx
Dluid
Deep learning user interface designer
Stars: ✭ 27 (-38.64%)
Mutual labels:  javafx
Substrate
Create native Java(FX) apps for desktop, mobile and embedded
Stars: ✭ 210 (+377.27%)
Mutual labels:  javafx
Fxtutorials
A collection of JavaFX tutorials from my channel on YouTube
Stars: ✭ 252 (+472.73%)
Mutual labels:  javafx
elm-doctest
doctest runner against Elm-lang source files
Stars: ✭ 13 (-70.45%)
Mutual labels:  unittest
FineCodeCoverage
Visualize unit test code coverage easily for free in Visual Studio Community Edition (and other editions too)
Stars: ✭ 391 (+788.64%)
Mutual labels:  unittest
vld
Visual Leak Detector for Visual C++ 2008-2015
Stars: ✭ 869 (+1875%)
Mutual labels:  memory-leaks

JMemoryBuddy

Build Status Maven Central

JMemoryBuddy provides an API to unit-test your code for memory leaks. It also provides an API to monitor a running JVM for memory leaks. It is used for internal projects at Sandec, especially for JPro. We've made it public, so everyone can fix and test their code for memory leaks in a professional way.

Together we can fix all memory leaks in the world. :-)

Dependency

The library is published at MavenCentral

Maven

<dependency>
  <groupId>de.sandec</groupId>
  <artifactId>JMemoryBuddy</artifactId>
  <version>0.5.2</version>
  <scope>test</scope>
</dependency>

Gradle

dependencies {
    compile "de.sandec:JMemoryBuddy:0.5.2"
}

How to use:

Write unit tests for memory leaks!

The method JMemoryBuddy.memoryTest is the usual way to test for leaks with JMemoryBuddy. A typicial test might look like the following:

@Test
public void simpleTest() {
    JMemoryBuddy.memoryTest(checker -> {
        A referenced = new A();
        checker.setAsReferenced(referenced);
        A notReferenced = new A();
        checker.assertCollectable(notReferenced); // notReferenced should be collectable
        checker.assertNotCollectable(referenced); // referenced should not be collectable
    });
}

The lambda provided to the memory test method is executed only once. The provided argument named "checker" provides an API to declare how the memory semantic should be.

Method
assertCollectable(ref) After executing the lambda, the provided ref must be collectable. Otherwise, an exception is thrown.
assertNotCollectable(ref) After executing the lambda, the provided ref must be not collectable. Otherwise, an exception is thrown.
setAsReferenced(ref) The provided reference won't be collected, until memoryTest finishes all it's tests.

Other utility methods:

You can also use the method assertCollectable and assertNotCollectable to check whether a single WeakReference can be collected, but usually, the memoryTest method is prefered because it results in more elegant tests.

Analyzing the heap dump:

JMemoryBuddy makes it easy to analyze the heap dump because all problematic instances are wrapped inside a class with the name AssertCollectable. Just search your heap dump with your prefered tool for this class name: visualvm

Configure JMemoryBuddy

You can configure VisualVM with SystemProperties:

Tables Effect Default
-Djmemorybuddy.createHeapDump Should a heap dump created on failure? true
-Djmemorybuddy.output The folder where the heap dump gets saved. if target exists, then "target" otherwise "build"

The following values usually shouldn't be changed but might be useful to make tests more stable or reduce the time required.

Tables Effect Default
-Djmemorybuddy.steps Maximum number of times we check whether something is collectable. You probably shouldn't change it. 10
-Djmemorybuddy.testDuration Maximum time in ms used to check whether something is collectable. You probably shouldn't change it. 1000
-Djmemorybuddy.garbageAmount How much garbage is created to stimulate the garbage collector 999999

Monitor running systems:

Mark references, where you know they can be collected:

JMemoryBuddyLive.markCollectable("description",reference);

Afterwards you can create a report during runtime with details about leaks.

System.gc();
JMemoryBuddyLive.getReport();

or search for AssertCollectableLive in a HeapDump. If the referent of an AssertCollectableLive is reachable, then you have a memory leak. It's especially interesting to see which leaks happen, if an application runs for severy hours, days, weeks or months.

FAQ - Why is no one else writing unit-tests for memory leaks?

There are various reasons for this. By spec the command System.gc() doesn't have to do anything, This makes it hard and undeterministic to test for collectability. Nevertheless, JMemoryBuddy makes testing for memory leaks reliably!. Currently, all known cases reliable and don't cause false-negative test results.

  • What can i do about SoftReferences? It's hard to check whether they are strongly reachable.

You can use the following JVM arugment: -XX:SoftRefLRUPolicyMSPerMB=0. With this argument, SoftReferences behave like WeakReferences.

  • I'm getting Leaking references during development, but not during production. What could be the reason?

A common reason might be various debuging features of you IDE. If you use the debugger, the garbage collector doesn't work reliably anymore.

Real test samples:

  • controlsfx - A simple test for a isolated JavaFX Components.
  • CSSFX (1, 2) - Various tests to make sure that a listener based code base doesn't have unwanted changes to the memory semantics.
  • JavaFX - PR for JavaFX itself to simplify some of the existing tests for memory leaks.

Projects using JMemoryBuddy:

internal developer

publish local:

./gradlew publishToMavenLocal

publish to sonatype:

./gradlew uploadArchives
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].