Skip to content

Commit 74166a9

Browse files
authored
Merge branch 'chrishantha:master' into master
2 parents b7c7344 + 6d115ce commit 74166a9

24 files changed

Lines changed: 465 additions & 19 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
language: java
22
jdk:
3-
- oraclejdk8
3+
- openjdk8

allocations/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
Sample to show allocations in Java Flight Recording
2-
===================================================
1+
Sample to show allocations in a Java Flight Recording
2+
=====================================================
33

44
This program checks whether a number is prime.
55

66
Run the program and also make a profiling recording.
77

88
### How to run
9-
`java -Xms64m -Xmx64m -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=30s,name=Allocations,filename=allocations.jfr -XX:FlightRecorderOptions=loglevel=info -jar target/allocations.jar`
9+
```bash
10+
java -Xms64m -Xmx64m -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=30s,name=Allocations,filename=allocations.jfr -Xlog:jfr=info -jar target/allocations.jar
11+
```
1012

1113
### Analyzing Java Flight Recording
1214

1315
In Memory -> Allocation tab, you should see a high allocation rate.
1416

15-
Also see Memory -> Garbage Collections tab and check the frequency of GC events.
17+
Also, see Memory -> Garbage Collections tab and check the frequency of GC events.
1618

1719
### Improving Performance
1820

19-
Try the program again after changing `Long` types to primitive `long`
21+
Try the program again after changing `Long` types to primitive `long`.

base/src/main/java/com/github/chrishantha/sample/base/Utils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2018 M. Isuru Tharanga Chrishantha Perera
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.github.chrishantha.sample.base;
217

318
import java.util.Timer;

highcpu/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ Sample program consuming high CPU usage
44
A java program consuming high CPU usage. This program was inspired by a sample found in the article "[Identifying which Java Thread is consuming most CPU](http://code.nomad-labs.com/2010/11/18/identifying-which-java-thread-is-consuming-most-cpu/)"
55

66
### How to run
7-
`java -Xms128m -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=30s,name=HighCPU,filename=highcpu.jfr -XX:FlightRecorderOptions=loglevel=info -jar target/highcpu.jar`
7+
```bash
8+
java -Xms128m -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=30s,name=HighCPU,filename=highcpu.jfr -Xlog:jfr=info -jar target/highcpu.jar
9+
```

hotmethods/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
Sample to show hot methods in Java Flight Recording
2-
===================================================
1+
Sample to show hot methods in a Java Flight Recording
2+
=====================================================
33

44
This program checks whether a given random number is primitive or not.
55

66
Run the program and also make a profiling recording.
77

88
### How to run
9-
`java -Xms64m -Xmx64m -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=60s,name=Hotmethods,filename=hotmethods.jfr -XX:FlightRecorderOptions=loglevel=info -jar target/hotmethods.jar`
9+
```bash
10+
java -Xms64m -Xmx64m -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=60s,name=Hotmethods,filename=hotmethods.jfr -Xlog:jfr=info -jar target/hotmethods.jar
11+
```
1012

1113
### Analyzing Java Flight Recording
1214

latencies/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
Sample to show latencies in Java Flight Recording
2-
=================================================
1+
Sample to show latencies in a Java Flight Recording
2+
===================================================
33

44
This program has two threads: Even and Odd to print Even and Odd numbers.
55

66
Run the program and also make a profiling recording.
77

88
### How to run
9-
`java -Xms64m -Xmx64m -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=30s,name=Latencies,filename=latencies.jfr -XX:FlightRecorderOptions=loglevel=info -jar target/latencies.jar`
9+
```bash
10+
java -Xms64m -Xmx64m -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=30s,name=Latencies,filename=latencies.jfr -Xlog:jfr=info -jar target/latencies.jar
11+
```
1012

1113
### Analyzing Java Flight Recording
1214

memoryleak/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Sample to a memory leak in Java Application
2-
===========================================
1+
Sample to show a memory leak in Java Application
2+
================================================
33

44
This sample application implements a simple caching solution using a `HashMap`.
55

memoryref/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Sample to a show memory references in a Java Application
2+
========================================================
3+
4+
This sample application creates objects from classes `A`, `B`, `C`, `D`, `E`, `F`, and `G`.
5+
Each of the previously mentioned classes extend to `Base` class, which creates a significantly
6+
large byte array in the constructor.
7+
8+
Following is the class diagram.
9+
10+
![Class Diagram](classes.png "Class Diagram")
11+
12+
The application is created to demonstrate the references among objects.
13+
14+
The sample application creates an object from class `A` in the main thread and another object from
15+
class `G` in a daemon thread.
16+
17+
The instance created from class `A` references the instances created from other classes as shown below.
18+
19+
![References](references.png "References")
20+
21+
Following is the object diagram showing the object references after the application is started.
22+
23+
![Object Diagram](objects.png "Object Diagram")
24+
25+
### How to run
26+
27+
Use the following command to run the application. The application creates an object from class `A` in
28+
the main thread and another object from class `G` in a daemon thread. After that the application will
29+
wait for a keyboard input.
30+
31+
```bash
32+
java -Xmn1g -Xmx2g -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -javaagent:target/memoryref.jar -jar target/memoryref.jar
33+
```
34+
35+
### Looking at object references using a Heap Dump.
36+
37+
A heap dump can be obtained from following command to look at the object references in heap memory.
38+
39+
```bash
40+
jmap -dump:file=target/heap.hprof $(pgrep -f memoryref)
41+
```
42+
43+
Use [Eclipse Memory Analyzer Tool (MAT)](https://www.eclipse.org/mat/) to analyze the heap dump.
44+
45+
For more information, read following story based on this sample application.
46+
47+
[Basic concepts of Java heap dump analysis with Eclipse Memory Analyzer Tool (MAT)](https://medium.com/@chrishantha/basic-concepts-of-java-heap-dump-analysis-with-mat-e3615fd79eb)
48+
49+
Note: Images were created using [draw.io](https://www.draw.io/).

memoryref/classes.png

24.5 KB
Loading

memoryref/objects.png

9.18 KB
Loading

0 commit comments

Comments
 (0)