GeezerVison is showing github.com — Updated README instructions · chrishantha/sample-java-programs@149a704
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +9-8Lines changed: 9 additions & 8 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | Sample to show allocations in Java Flight Recording | 2 | | -================================================= | | 2 | +=================================================== | 3 | 3 |
| 4 | 4 | This program checks whether a number is prime. | 5 | 5 |
| 6 | | -Run the program without any arguments and also make a profiling recording. | | 6 | +Run the program and also make a profiling recording. | 7 | 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` | | 8 | +### 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` | | 10 | + | | 11 | +### Analyzing Java Flight Recording | 10 | 12 |
| 11 | 13 | In Memory -> Allocation tab, you should see a high allocation rate. | 12 | 14 |
| 13 | | -Also see Memory -> Garbage Collections tab and check the frequency of | | 15 | +Also see Memory -> Garbage Collections tab and check the frequency of GC events. | 14 | 16 |
| 15 | | -Try the program again after changing `Long` types to primitive `long` | | 17 | +### Improving Performance | 16 | 18 |
| 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` | | 19 | +Try the program again after changing `Long` types to primitive `long` |
Collapse file Copy file name to clipboardExpand all lines: highcpu/README.md +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -3,3 +3,5 @@ Sample program consuming high CPU usage | 3 | 3 |
| 4 | 4 | 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/)" | 5 | 5 |
| | 6 | +### 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` |
Collapse file Copy file name to clipboardExpand all lines: hotmethods/README.md +8-5Lines changed: 8 additions & 5 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -3,20 +3,23 @@ Sample to show hot methods in Java Flight Recording | 3 | 3 |
| 4 | 4 | This program checks whether a given random number is primitive or not. | 5 | 5 |
| 6 | | -Run the program without any arguments and also make a profiling recording. | | 6 | +Run the program and also make a profiling recording. | 7 | 7 |
| 8 | | -For example: | 9 | | -`java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=60s,name=Hotmethods,filename=hotmethods.jfr -XX:FlightRecorderOptions=loglevel=info -jar target/hotmethods.jar` | | 8 | +### 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` | | 10 | + | | 11 | +### Analyzing Java Flight Recording | 10 | 12 |
| 11 | 13 | In Code -> Hot Methods tab, you should see that the program has spent a lot of time in the LinkedList.indexOf(Object) method, which is called from contains method. | 12 | 14 |
| | 15 | +### Improving Performance | | 16 | + | 13 | 17 | By changing LinkedList to a HashSet, you can make the program run a lot faster. | 14 | 18 |
| 15 | 19 | 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 | 20 |
| 17 | 21 | In the program, change `Collection<Integer> primeNumbers = new LinkedList<>()` to `Collection<Integer> primeNumbers = new HashSet<>()` to use a HashSet for Prime Numbers | 18 | 22 |
| 19 | 23 | Check the runtime using HashSet. | 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` | 21 | 24 |
| 22 | | -Use `time` command in Linux to measure the time. | | 25 | +Use `time` command in Linux to measure the time. |
Collapse file Copy file name to clipboardExpand all lines: latencies/README.md +8-7Lines changed: 8 additions & 7 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -3,20 +3,21 @@ Sample to show latencies in Java Flight Recording | 3 | 3 |
| 4 | 4 | This program has two threads: Even and Odd to print Even and Odd numbers. | 5 | 5 |
| 6 | | -Run the program without any arguments and also make a profiling recording. | | 6 | +Run the program and also make a profiling recording. | 7 | 7 |
| 8 | | -For example: | 9 | | -`java -jar -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=settings=profile,duration=30s,name=Latencies,filename=latencies.jfr -XX:FlightRecorderOptions=loglevel=info target/latencies.jar` | | 8 | +### 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` | | 10 | + | | 11 | +### Analyzing Java Flight Recording | 10 | 12 |
| 11 | 13 | In Threads -> Latencies tab, you should see that the program has many blocked events. | 12 | 14 |
| 13 | 15 | You should also see red blocks in Events -> Graph tab. Red blocks are not good as the thread is just waiting to acquire a lock. | 14 | 16 |
| 15 | 17 | See Threads -> Contention tab to see the Locks and also see Threads -> Lock Instances to check whether both threads share the same lock instances. | 16 | 18 |
| 17 | | -Since there is no shared resource, you can avoid the lock and improve the performance. | | 19 | +### Improving Performance | 18 | 20 |
| 19 | | -Remove the `synchronized` keyword in `isEven` method. | | 21 | +Since there is no shared resource to protect, you can avoid the synchronized keyword and improve the performance. | 20 | 22 |
| 21 | | -Run the program without locks. | 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` | | 23 | +Try the program again after removing the `synchronized` keyword in `isEven` method. |
|