Skip to content

Commit 5c01384

Browse files
committed
Added allocations sample and updated README to mention instructions to fix performance issue
1 parent 37eadd6 commit 5c01384

File tree

8 files changed

+149
-32
lines changed

8 files changed

+149
-32
lines changed

allocations/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Sample to show allocations in Java Flight Recording
2+
=================================================
3+
4+
This program checks whether a number is prime.
5+
6+
Run the program without any arguments and also make a profiling recording.
7+
8+
For example:
9+
`java -Xms64m -Xmx64m -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=30s,name=Allocations,filename=allocations.jfr -XX:FlightRecorderOptions=loglevel=info -jar target/allocations.jar`
10+
11+
In Memory -> Allocation tab, you should see a high allocation rate.
12+
13+
Also see Memory -> Garbage Collections tab and check the frequency of
14+
15+
Try the program again after changing `Long` types to primitive `long`
16+
17+
Run the program without locks.
18+
`java -Xms64m -Xmx64m -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=30s,name=Allocations,filename=allocations-fixed.jfr -XX:FlightRecorderOptions=loglevel=info -jar target/allocations.jar`

allocations/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
# Copyright 2017 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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
19+
<parent>
20+
<groupId>com.github.chrishantha.sample</groupId>
21+
<artifactId>java-samples</artifactId>
22+
<version>0.0.2-SNAPSHOT</version>
23+
<relativePath>../pom.xml</relativePath>
24+
</parent>
25+
26+
<modelVersion>4.0.0</modelVersion>
27+
<artifactId>allocations</artifactId>
28+
<packaging>jar</packaging>
29+
<name>allocations</name>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>com.beust</groupId>
34+
<artifactId>jcommander</artifactId>
35+
</dependency>
36+
</dependencies>
37+
38+
39+
<properties>
40+
<fully.qualified.main.class>com.github.chrishantha.sample.allocations.App</fully.qualified.main.class>
41+
</properties>
42+
</project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2017 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+
*/
16+
package com.github.chrishantha.sample.allocations;
17+
18+
import com.beust.jcommander.JCommander;
19+
import com.beust.jcommander.Parameter;
20+
21+
public class App {
22+
23+
@Parameter(names = "--max", description = "Max Numbers")
24+
private long max = 10_000_000L;
25+
26+
@Parameter(names = "--help", description = "Display Help", help = true)
27+
private boolean help;
28+
29+
public static void main(String[] args) {
30+
App app = new App();
31+
final JCommander jcmdr = new JCommander(app);
32+
jcmdr.setProgramName(App.class.getSimpleName());
33+
jcmdr.parse(args);
34+
35+
System.out.println(app);
36+
37+
if (app.help) {
38+
jcmdr.usage();
39+
return;
40+
}
41+
42+
app.start();
43+
}
44+
45+
boolean isPrime(Long n) {
46+
//check if n is a multiple of 2
47+
if (n % 2 == 0) return false;
48+
//if not, then just check the odds
49+
for (int i = 3; i * i <= n; i += 2) {
50+
if (n % i == 0)
51+
return false;
52+
}
53+
return true;
54+
}
55+
56+
private void start() {
57+
Long primeCount = 0L;
58+
for (long i = 0; i < max; i++) {
59+
if (isPrime(i)) {
60+
primeCount++;
61+
}
62+
}
63+
System.out.format("Found %d prime numbers%n", primeCount);
64+
}
65+
66+
@Override
67+
public String toString() {
68+
StringBuilder builder = new StringBuilder();
69+
builder.append("App [max=");
70+
builder.append(max);
71+
builder.append("]");
72+
return builder.toString();
73+
}
74+
}

hotmethods/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ By changing LinkedList to a HashSet, you can make the program run a lot faster.
1414

1515
The contains methods in Linked List checks all the items. Algorithm run time is O(n). However the HashSet contains algorithm's run time is O(1).
1616

17+
In the program, change `Collection<Integer> primeNumbers = new LinkedList<>()` to `Collection<Integer> primeNumbers = new HashSet<>()` to use a HashSet for Prime Numbers
18+
1719
Check the runtime using HashSet.
18-
`java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=60s,name=Hotmethods,filename=hotmethods.jfr -XX:FlightRecorderOptions=loglevel=info -jar target/hotmethods.jar --use-set`
20+
`java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=60s,name=Hotmethods,filename=hotmethods-fixed.jfr -XX:FlightRecorderOptions=loglevel=info -jar target/hotmethods.jar`
1921

2022
Use `time` command in Linux to measure the time.

hotmethods/src/main/java/com/github/chrishantha/sample/hotmethods/App.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.beust.jcommander.Parameter;
2020

2121
import java.util.Collection;
22-
import java.util.HashSet;
2322
import java.util.LinkedList;
2423
import java.util.Random;
2524

@@ -31,9 +30,6 @@ public class App {
3130
@Parameter(names = "--max", description = "Maximum limit to generate prime numbers")
3231
private int max = 100000;
3332

34-
@Parameter(names = "--use-set", description = "Use Hash Set for Prime Numbers", arity = 0)
35-
private boolean useSet;
36-
3733
@Parameter(names = "--help", description = "Display Help", help = true)
3834
private boolean help;
3935

@@ -54,7 +50,7 @@ public static void main(String[] args) {
5450
}
5551

5652
private void start() {
57-
Collection<Integer> primeNumbers = useSet ? new HashSet<>() : new LinkedList<>();
53+
Collection<Integer> primeNumbers = new LinkedList<>();
5854
System.out.println("Generating Prime numbers between 1 and " + max);
5955
for (int i = 1; i < max; i++) {
6056
boolean isPrimeNumber = true;
@@ -90,8 +86,6 @@ public String toString() {
9086
builder.append(randomNumbersCount);
9187
builder.append(", max=");
9288
builder.append(max);
93-
builder.append(", useSet=");
94-
builder.append(useSet);
9589
builder.append("]");
9690
return builder.toString();
9791
}

latencies/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ See Threads -> Contention tab to see the Locks and also see Threads -> Lock Inst
1616

1717
Since there is no shared resource, you can avoid the lock and improve the performance.
1818

19+
Remove the `synchronized` keyword in `isEven` method.
20+
1921
Run the program without locks.
20-
`java -jar -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=30s,name=Latencies,filename=latencies-fixed.jfr -XX:FlightRecorderOptions=loglevel=info target/latencies.jar --lock false`
22+
`java -jar -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=30s,name=Latencies,filename=latencies-fixed.jfr -XX:FlightRecorderOptions=loglevel=info target/latencies.jar`

latencies/src/main/java/com/github/chrishantha/sample/latencies/App.java

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ public class App {
2323
@Parameter(names = "--count", description = "Print Count")
2424
private int count = 50;
2525

26-
@Parameter(names = "--lock", description = "Lock", arity = 1)
27-
private boolean lock = true;
28-
2926
@Parameter(names = "--help", description = "Display Help", help = true)
3027
private boolean help;
3128

@@ -45,7 +42,6 @@ public static void main(String[] args) {
4542
app.start();
4643
}
4744

48-
4945
private class EvenThread extends Thread {
5046

5147
public EvenThread() {
@@ -55,7 +51,9 @@ public EvenThread() {
5551
@Override
5652
public void run() {
5753
for (int i = 0; i < count; i++) {
58-
printEven(i);
54+
if (isEven(i)) {
55+
printNumber(i);
56+
}
5957
}
6058
}
6159
}
@@ -69,7 +67,9 @@ public OddThread() {
6967
@Override
7068
public void run() {
7169
for (int i = 0; i < count; i++) {
72-
printOdd(i);
70+
if (!isEven(i)) {
71+
printNumber(i);
72+
}
7373
}
7474
}
7575
}
@@ -79,23 +79,7 @@ private void printNumber(int i) {
7979
System.out.format("Thread: %s, Number: %d%n", Thread.currentThread().getName(), i);
8080
}
8181

82-
private void printEven(int i) {
83-
if ((lock && isEvenLocked(i)) || isEven(i)) {
84-
printNumber(i);
85-
}
86-
}
87-
88-
private void printOdd(int i) {
89-
if ((lock && !isEvenLocked(i)) || !isEven(i)) {
90-
printNumber(i);
91-
}
92-
}
93-
94-
private synchronized boolean isEvenLocked(int i) {
95-
return isEven(i);
96-
}
97-
98-
private boolean isEven(int i) {
82+
private synchronized boolean isEven(int i) {
9983
try {
10084
Thread.sleep(1000);
10185
} catch (InterruptedException e) {

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<module>highcpu</module>
5454
<module>hotmethods</module>
5555
<module>latencies</module>
56+
<module>allocations</module>
5657
</modules>
5758

5859
<dependencyManagement>

0 commit comments

Comments
 (0)