Please also consider the ability to capture memory snapshots on low memory or when OutOfMemory occurs.
You can instruct the profiler to capture a memory snapshot after a specified period of time.
When used memory constantly increases, this often means there is a memory leak. This feature greatly simplifies the detection of such situations in e.g. long-running applications such as servers. One of the benefits is that, after being triggered, the feature requires no further human interaction.
To toggle this feature, connect to the profiled application and press the button shown on the picture below:
Then, every time the specified period elapses, a memory snapshot will be created and the following notification will be shown in the UI:
How to capture snapshots periodically with the help of the profiler API
The following code snippet starts a thread that captures memory snapshots of the application itself each 10 minutes.
Please don't forget to add yjp-controller-api-redist.jar to the classpath. This jar can be found in
<Profiler Installation Directory>/lib.
Read more about the profiler API.
import com.yourkit.api.Controller;
...
final Thread thread = new Thread(
new Runnable(){
public void run() {
try {
final Controller controller = new Controller();
for (;;) {
Thread.sleep(10 /* minutes */ * 60 /*seconds in minute*/ * 1000 /* millis in second */);
controller.captureMemorySnapshot();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
);
thread.setDaemon(true); // let the application normally terminate
thread.start();
The second example is a small standalone Java application that each 10 minutes captures snapshots of a given profiled application. It can be used instead of the first snippet when direct code insertions into the profiled application are not possible or not desired.
import com.yourkit.api.Controller;
public class Dumper {
public static void main(String[] args) {
try {
final Controller controller = new Controller(
"localhost", // the host where profiled application runs
10001 // the port the profiler agent listens on - CAN BE DIFFERENT IN YOUR CASE!
);
for (;;) {
Thread.sleep(10 /* minutes */ * 60 /*seconds in minute*/ * 1000 /* millis in second */);
controller.captureMemorySnapshot();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
