Skip to content

Commit 7dab469

Browse files
committed
Add sample application to show thread leaks
1 parent 6215dce commit 7dab469

File tree

5 files changed

+412
-1
lines changed

5 files changed

+412
-1
lines changed

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@
4848
<module>hotmethods</module>
4949
<module>latencies</module>
5050
<module>allocations</module>
51-
<module>memoryleak</module>
5251
<module>deadlock</module>
52+
<module>memoryleak</module>
53+
<module>threadleak</module>
5354
</modules>
5455

5556
<dependencyManagement>
@@ -64,6 +65,11 @@
6465
<artifactId>base</artifactId>
6566
<version>${samples.version}</version>
6667
</dependency>
68+
<dependency>
69+
<groupId>org.hdrhistogram</groupId>
70+
<artifactId>HdrHistogram</artifactId>
71+
<version>${hdrhistogram.version}</version>
72+
</dependency>
6773
</dependencies>
6874
</dependencyManagement>
6975

@@ -144,5 +150,6 @@
144150
<javac.target>1.8</javac.target>
145151
<samples.version>0.0.2-SNAPSHOT</samples.version>
146152
<jcommander.version>1.72</jcommander.version>
153+
<hdrhistogram.version>2.1.10</hdrhistogram.version>
147154
</properties>
148155
</project>

threadleak/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Sample to show thread leaks in a Java Application
2+
=================================================
3+
4+
This sample application implements merge sort algorithm using multiple threads.
5+
6+
The algorithm for parallel merge sort was taken from the [Merge Sort example available online from the University
7+
of Washington](https://courses.cs.washington.edu/courses/cse373/13wi/lectures/03-13/MergeSort.java)
8+
9+
The original example uses threads directly. This sample application uses an `ExecutorService` to run threads.
10+
11+
This application runs continuously and prints an interval summary statistics of algorithm run time for multiple random
12+
number arrays. The application will also print final summary statistics of algorithm run time at the program exit.
13+
14+
### How to run
15+
16+
The application will throw Out of Memory error after some time when you run following command
17+
18+
`java -Xmx1g -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints
19+
-XX:+UnlockCommercialFeatures -XX:+FlightRecorder
20+
-XX:StartFlightRecording=settings=profile,duration=2m,name=ThreadLeak,filename=threadleak.jfr
21+
-XX:FlightRecorderOptions=loglevel=info
22+
-jar target/threadleak.jar`
23+
24+
### Analyzing Java Flight Recording
25+
26+
In Threads -> Overview tab, you should see thread count is increasing steadily.
27+
28+
### How to fix the Thread Leak
29+
30+
Run the application with `--stop-leakage` parameter, which will use a single `ExecutorService` throughout the
31+
application.

threadleak/pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
# Copyright 2018 M. Isuru Tharanga Chrishantha Perera
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
20+
<parent>
21+
<groupId>com.github.chrishantha.sample</groupId>
22+
<artifactId>java-samples</artifactId>
23+
<version>0.0.2-SNAPSHOT</version>
24+
<relativePath>../pom.xml</relativePath>
25+
</parent>
26+
27+
<modelVersion>4.0.0</modelVersion>
28+
<artifactId>threadleak</artifactId>
29+
<packaging>jar</packaging>
30+
<name>threadleak</name>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>com.github.chrishantha.sample</groupId>
35+
<artifactId>base</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.hdrhistogram</groupId>
39+
<artifactId>HdrHistogram</artifactId>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-shade-plugin</artifactId>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>

0 commit comments

Comments
 (0)