Skip to content

Commit

Permalink
Add sample application to show thread leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishantha committed Jul 5, 2018
1 parent 6215dce commit 7dab469
Show file tree
Hide file tree
Showing 5 changed files with 412 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pom.xml
Expand Up @@ -48,8 +48,9 @@
<module>hotmethods</module>
<module>latencies</module>
<module>allocations</module>
<module>memoryleak</module>
<module>deadlock</module>
<module>memoryleak</module>
<module>threadleak</module>
</modules>

<dependencyManagement>
Expand All @@ -64,6 +65,11 @@
<artifactId>base</artifactId>
<version>${samples.version}</version>
</dependency>
<dependency>
<groupId>org.hdrhistogram</groupId>
<artifactId>HdrHistogram</artifactId>
<version>${hdrhistogram.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -144,5 +150,6 @@
<javac.target>1.8</javac.target>
<samples.version>0.0.2-SNAPSHOT</samples.version>
<jcommander.version>1.72</jcommander.version>
<hdrhistogram.version>2.1.10</hdrhistogram.version>
</properties>
</project>
31 changes: 31 additions & 0 deletions threadleak/README.md
@@ -0,0 +1,31 @@
Sample to show thread leaks in a Java Application
=================================================

This sample application implements merge sort algorithm using multiple threads.

The algorithm for parallel merge sort was taken from the [Merge Sort example available online from the University
of Washington](https://courses.cs.washington.edu/courses/cse373/13wi/lectures/03-13/MergeSort.java)

The original example uses threads directly. This sample application uses an `ExecutorService` to run threads.

This application runs continuously and prints an interval summary statistics of algorithm run time for multiple random
number arrays. The application will also print final summary statistics of algorithm run time at the program exit.

### How to run

The application will throw Out of Memory error after some time when you run following command

`java -Xmx1g -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints
-XX:+UnlockCommercialFeatures -XX:+FlightRecorder
-XX:StartFlightRecording=settings=profile,duration=2m,name=ThreadLeak,filename=threadleak.jfr
-XX:FlightRecorderOptions=loglevel=info
-jar target/threadleak.jar`

### Analyzing Java Flight Recording

In Threads -> Overview tab, you should see thread count is increasing steadily.

### How to fix the Thread Leak

Run the application with `--stop-leakage` parameter, which will use a single `ExecutorService` throughout the
application.
51 changes: 51 additions & 0 deletions threadleak/pom.xml
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
# Copyright 2018 M. Isuru Tharanga Chrishantha Perera
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>com.github.chrishantha.sample</groupId>
<artifactId>java-samples</artifactId>
<version>0.0.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>threadleak</artifactId>
<packaging>jar</packaging>
<name>threadleak</name>

<dependencies>
<dependency>
<groupId>com.github.chrishantha.sample</groupId>
<artifactId>base</artifactId>
</dependency>
<dependency>
<groupId>org.hdrhistogram</groupId>
<artifactId>HdrHistogram</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 7dab469

Please sign in to comment.