GeezerVison is showing github.com — Added allocations sample and updated README to mention instructions t… · chrishantha/sample-java-programs@5c01384
File treeExpand file treeCollapse file tree Open diff view settings src/main/java/com/github/chrishantha/sample/allocations
src/main/java/com/github/chrishantha/sample/hotmethods
src/main/java/com/github/chrishantha/sample/latencies
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 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` |
Collapse file Copy file name to clipboard +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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> |
Collapse file Copy file name to clipboard +74Lines changed: 74 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
Collapse file Copy file name to clipboardExpand all lines: hotmethods/README.md +3-1Lines changed: 3 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,9 @@ By changing LinkedList to a HashSet, you can make the program run a lot faster. | 14 | 14 |
| 15 | 15 | 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). | 16 | 16 |
| | 17 | +In the program, change `Collection<Integer> primeNumbers = new LinkedList<>()` to `Collection<Integer> primeNumbers = new HashSet<>()` to use a HashSet for Prime Numbers | | 18 | + | 17 | 19 | 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` | 19 | 21 |
| 20 | 22 | Use `time` command in Linux to measure the time. |
Collapse file Copy file name to clipboardExpand all lines: hotmethods/src/main/java/com/github/chrishantha/sample/hotmethods/App.java +1-7Lines changed: 1 addition & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | import com.beust.jcommander.Parameter; | 20 | 20 |
| 21 | 21 | import java.util.Collection; | 22 | | -import java.util.HashSet; | 23 | 22 | import java.util.LinkedList; | 24 | 23 | import java.util.Random; | 25 | 24 |
| @@ -31,9 +30,6 @@ public class App { | 31 | 30 | @Parameter(names = "--max", description = "Maximum limit to generate prime numbers") | 32 | 31 | private int max = 100000; | 33 | 32 |
| 34 | | - @Parameter(names = "--use-set", description = "Use Hash Set for Prime Numbers", arity = 0) | 35 | | - private boolean useSet; | 36 | | - | 37 | 33 | @Parameter(names = "--help", description = "Display Help", help = true) | 38 | 34 | private boolean help; | 39 | 35 |
| @@ -54,7 +50,7 @@ public static void main(String[] args) { | 54 | 50 | } | 55 | 51 |
| 56 | 52 | private void start() { | 57 | | - Collection<Integer> primeNumbers = useSet ? new HashSet<>() : new LinkedList<>(); | | 53 | + Collection<Integer> primeNumbers = new LinkedList<>(); | 58 | 54 | System.out.println("Generating Prime numbers between 1 and " + max); | 59 | 55 | for (int i = 1; i < max; i++) { | 60 | 56 | boolean isPrimeNumber = true; | @@ -90,8 +86,6 @@ public String toString() { | 90 | 86 | builder.append(randomNumbersCount); | 91 | 87 | builder.append(", max="); | 92 | 88 | builder.append(max); | 93 | | - builder.append(", useSet="); | 94 | | - builder.append(useSet); | 95 | 89 | builder.append("]"); | 96 | 90 | return builder.toString(); | 97 | 91 | } | |
Collapse file Copy file name to clipboardExpand all lines: latencies/README.md +3-1Lines changed: 3 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -16,5 +16,7 @@ See Threads -> Contention tab to see the Locks and also see Threads -> Lock Inst | 16 | 16 |
| 17 | 17 | Since there is no shared resource, you can avoid the lock and improve the performance. | 18 | 18 |
| | 19 | +Remove the `synchronized` keyword in `isEven` method. | | 20 | + | 19 | 21 | 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` |
Collapse file Copy file name to clipboardExpand all lines: latencies/src/main/java/com/github/chrishantha/sample/latencies/App.java +7-23Lines changed: 7 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,9 +23,6 @@ public class App { | 23 | 23 | @Parameter(names = "--count", description = "Print Count") | 24 | 24 | private int count = 50; | 25 | 25 |
| 26 | | - @Parameter(names = "--lock", description = "Lock", arity = 1) | 27 | | - private boolean lock = true; | 28 | | - | 29 | 26 | @Parameter(names = "--help", description = "Display Help", help = true) | 30 | 27 | private boolean help; | 31 | 28 |
| @@ -45,7 +42,6 @@ public static void main(String[] args) { | 45 | 42 | app.start(); | 46 | 43 | } | 47 | 44 |
| 48 | | - | 49 | 45 | private class EvenThread extends Thread { | 50 | 46 |
| 51 | 47 | public EvenThread() { | @@ -55,7 +51,9 @@ public EvenThread() { | 55 | 51 | @Override | 56 | 52 | public void run() { | 57 | 53 | for (int i = 0; i < count; i++) { | 58 | | - printEven(i); | | 54 | + if (isEven(i)) { | | 55 | + printNumber(i); | | 56 | + } | 59 | 57 | } | 60 | 58 | } | 61 | 59 | } | @@ -69,7 +67,9 @@ public OddThread() { | 69 | 67 | @Override | 70 | 68 | public void run() { | 71 | 69 | for (int i = 0; i < count; i++) { | 72 | | - printOdd(i); | | 70 | + if (!isEven(i)) { | | 71 | + printNumber(i); | | 72 | + } | 73 | 73 | } | 74 | 74 | } | 75 | 75 | } | @@ -79,23 +79,7 @@ private void printNumber(int i) { | 79 | 79 | System.out.format("Thread: %s, Number: %d%n", Thread.currentThread().getName(), i); | 80 | 80 | } | 81 | 81 |
| 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) { | 99 | 83 | try { | 100 | 84 | Thread.sleep(1000); | 101 | 85 | } catch (InterruptedException e) { | |
Collapse file Copy file name to clipboardExpand all lines: pom.xml +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 53 | 53 | <module>highcpu</module> | 54 | 54 | <module>hotmethods</module> | 55 | 55 | <module>latencies</module> | | 56 | + <module>allocations</module> | 56 | 57 | </modules> | 57 | 58 |
| 58 | 59 | <dependencyManagement> | |
|