Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample application to show thread leaks
- Loading branch information
1 parent
6215dce
commit 7dab469
Showing
5 changed files
with
412 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Oops, something went wrong.