Java Performance Tuning
Java(TM) - see bottom of page
Our valued sponsors who help make this site possible
JProfiler: Get rid of your performance problems and memory leaks!
Training online: Concurrency, Threading, GC, Advanced Java and more ...
Tips November 2018
JProfiler
|
Get rid of your performance problems and memory leaks!
|
JProfiler
|
Get rid of your performance problems and memory leaks!
|
|
|
Back to newsletter 216 contents
https://www.youtube.com/watch?v=Pqf367x-OuA
Production-Time Profiling and Diagnostics on the JVM (Page last updated October 2018, Added 2018-11-28, Author Marcus Hirt, Publisher Oracle). Tips:
- com.sun.tools.attach - dynamically attach to locally running JVMS, and load JVMTI or JPLIS or JMX agents and execute JCMD commands. Try{vm = VirtualMachine.attach(pid); println(vm.startLocalManagementAgent()); (HotSpotVirtualMachine)vm.dumpHeap(f);AttachUtils.readFromStream((HotSpotVirtualMachine)vm.executeJCmd(?Thread.print?));}finally{vm.detach();}
- JMX gives Memory,GC metrics,CPU load, and more. But note there is a lot of marshalling so this can add to memory pressure.
- Internal perf counters - new PerfInstrumentation(Perf.getPerf().attach(...)).getAllCounters().
- Most modern profilers use AsyncGetCallTrace() rather than JVMTI.
- JDI is a Java API for attaching to Java processes as a debug client, includes executing arbitrary code, your last option if nothing else works, but has a severe performance penalty.
- JPLIS - java agents, used for bytecode manipulation. Can be loaded dynamically with attach. Note redefining the class will de-optimize it
- JCMD sends commands to a local Java process. jcmd PID help / jcmd PID help COMMAND. ccmd 0 Thread.print operates on ALL JVMs (same user).
- JFR high performance event recorder built in to the JVM.
- JMC application and a library, including a JFR parser
https://www.youtube.com/watch?v=EoWLLEY04qQ
Ultra Low Latency with Java and Terabytes of Data (Page last updated October 2018, Added 2018-11-28, Author Per Minborg, Publisher IMC Summit). Tips:
- Applications can be slow from: slow datastores, remote data, unnecessary object creation, garbage collection, lack of parallelism, data spread across nodes.
- Low location affinity of data is a latency problem. Data that will be used together but are located on different nodes will need to have the data combined to be used and that will cause additional latency.
- You can't get scale out, low affinity and low latency all at the same time; scaling out will limit affinity, impacting latency.
- To write a single Java object to main memory takes 200 nanoseconds. This means you cannot create shared objects if you have a 200 nanosecond target.
- Main memory read is 100 nanosecond. With a 200 nanosecond target you need to stick to processing using the L1 (0.5ns) L2 (7ns) and L3 (20ns) caches.
- If you have more data than can fit into memory on a single JVM, you need to shard your data, or if a little speed loss is acceptable use memory mapping.
- Keeping data off-heap but in-memory let's you avoid garbage collection overheads while still benefiting from in-memory speeds.
- Speedment is an open-source implementation of these concepts for running data analytics against a database, not that suitable for transactional operations.
https://www.youtube.com/watch?v=Xro4KwoMNJ8
Thread Safety with Phaser, StampedLock, and VarHandle (Page last updated October 2018, Added 2018-11-28, Author Heinz Kabutz, Publisher Oracle). Tips:
- Phaser is a replacement for CyclicBarrier and CountDownLatch, and can be used with ForkJoinPool. Phaser is more flexible than the other two classes.
- StampedLock is a synchronizer, it allows thread-safe optimistic reads (as well as pessimistic reads and writes). You can also pass the lock across threads.
- StampedLock efficient read is to try an optimistic read, validate it, and if it fails the validation do a pessimistic read (and unlock). If it passed the validation, the optimistic read is sufficient and efficient.
- VarHandles are as fast as Unsafe to read and write fields including volatile fields.
http://www.andygibson.net/blog/programming/java-patterns-for-concurrency/
Java Patterns For Concurrency (Page last updated October 2018, Added 2018-11-28, Author Andy Gibson, Publisher Andy Gibson). Tips:
- Write code so that the developers using that code don't need to consider Concurrency, ie threadsafe classes that can be used without explicitly needing to handle the thread safety (eg no need to explicitly synchronize).
- A technique to stay threadsafe is to do all the state changes in a dedicated single thread.
- Locally scoped data is inherently thread safe. So if you copy state into a local variable and reference it locally, this local copy is threadsafe.
- Encapsulate state so that you control all access and update to that state, allowing you to ensure you make it threadsafe.
- If you read from shared state much more often than writing to it, consider using a lock that let's you have shared readlocks and exclusive write locks, such as ReentrantReadWriteLock or StampedLock.
- If you need to provide access to the full content of an encapsulated shared (across threads) data structure, make a full copy of the data in a new structure and return that - never expose the actual updateable shared structure externally.
Jack Shirazi
Back to newsletter 216 contents
Last Updated: 2024-12-27
Copyright © 2000-2024 Fasterj.com. All Rights Reserved.
All trademarks and registered trademarks appearing on JavaPerformanceTuning.com are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. JavaPerformanceTuning.com is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
URL: http://www.JavaPerformanceTuning.com/news/newtips216.shtml
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us