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 December 2023
JProfiler
|
Get rid of your performance problems and memory leaks!
|
JProfiler
|
Get rid of your performance problems and memory leaks!
|
|
|
Back to newsletter 277 contents
https://medium.com/javarevisited/java-memory-leaks-silent-killers-in-your-application-3cc9f591ca77
Java Memory Leaks: Silent Killers in Your Application (Page last updated October 2023, Added 2023-12-22, Author Skilled Coder, Publisher Javarevisited). Tips:
- A common cause of memory leaks are static collections that grow indefinitely. To prevent leaks limit the size of the collection or clear it periodically.
- A common cause of memory leaks is not closing resources like streams, connections, or filehandles. To prevent leaks ensure resources are closed by using finally blocks or try-with-resources.
- A common cause of memory leaks is from non-static inner classes that outlive the outer class - as these hold an implicit reference to their outer class. To prevent leaks either use static inner classes or separate classes.
- A common cause of memory leaks is leaving cached objects in the cache for to long. To prevent leaks use a caching library that supports expiration, like EhCache or Guava's Cache.
- A common cause of memory leaks is from ThreadLocal variables in threads that don't terminate like in thread pools. To prevent leaks always remove ThreadLocal variables when they are no longer needed.
- A common cause of memory leaks is registering listeners and not unregistering them. To prevent leaks always provide a mechanism to unregister listeners.
- A common cause of memory leaks are singleton classes that hold onto large objects. To prevent leaks ensure that singletons don't hold onto objects longer than necessary.
- A common cause of memory leaks is not closing database connections. To prevent leaks always close database connections after use.
- A common cause of memory leaks is frequently loading and unloading classes. To prevent leaks be cautious with the lifecycle of class loaders and loaded classes.
https://www.youtube.com/watch?v=HDLEmXH2AwM
Bare metal Java (Page last updated November 2023, Added 2023-12-22, Author Jaroslaw Palka, Publisher GeeCON). Tips:
- malloc is available in Java via DirectByteBuffer, Unsafe, JNI and the foreign function & memory API (with MemorySegment).
- MemorySegment can create memory that is manually managed or automatically GC managed by scope or even manually managed by try-with-resources scope.
- Arena can restrict memory access to specific arenas, eg a specific thread.
- MemoryLayout let's you specify alignment and padding.
https://blog.stackademic.com/optimizing-java-for-low-latency-applications-803e181d0add
Optimizing Java for Low-Latency Applications (Page last updated November 2023, Added 2023-12-22, Author Alexander Obregon, Publisher Stackademic). Tips:
- The key issue with garbage collection for low-latency applications is stop-the-world pauses.
- Selecting the right garbage collector is crucial for optimizing application performance: Serial GC for small applications with low memory footprint and low CPU requirements, but not suitable for low-latency applications due to longer GC pauses; Parallel GC (Throughput Collector) for maximizing application throughput by utilizing multiple threads for garbage collection but also not suitable for low-latency applications due to significant GC pauses; CMS (Concurrent Mark Sweep) designed to minimize pause times by working concurrently with application threads, has lower pause times but suffers from fragmentation ultimately giving long pauses; G1 (Garbage-First Collector) tries to provide a good balance between throughput and pause times, but doesn't give the lowest pause times; ZGC (Z Garbage Collector) and Shenandoah designed for low-latency applications, aiming to achieve pause times of less than 10ms, even on large heaps.
- Best Practices for Garbage Collection Tuning: size the heap optimally, too small a heap causes frequent GC cycles, too large a heap may lead to longer GC pauses; understand the allocation patterns of your application to tune the sizes of the different GC generations, avoiding filling the old generation is critical for avoiding long GCs with most GC algorithms; use GC logging and monitoring to understand the behaviour of GC in your application, identifying issues like frequent or long pauses.
- Efficient coding improves execution speed and reduces garbage collection overhead. Techniques include: choosing the right data structure; avoiding boxed types by using primitive types where appropriate; building custom data structures for specific needs.
- Avoid memory leaks: ensure that resources like streams, connections, and other I/O objects are properly closed after use; use try-with-resources; use weak references for caches and large data structures that can be recreated; profile to identify and fix memory leaks.
- Small changes in algorithmic efficiency can have significant impacts on performance.
- Simple techniques like minimizing work inside loops, avoiding method calls within loops, and loop unrolling can improve performance.
- For performance hotspots, delay computation or object creation until absolutely necessary.
- Use fine-grained locks or lock-free data structures.
- Regularly profile and performance test.
- Set the initial (-Xms) and maximum (-Xmx) heap sizes properly to prevent frequent garbage collections, but note a too-large heap size can lead to longer GC pauses.
- Select the optimal garbage collector for the application, eg -XX:+UseG1GC, -XX:+UseConcMarkSweepGC, or -XX:+UseZGC etc.
- Enable GC logging.
- Advanced JVM tuning techniques include: fine tuning the garbage collector; using off-heap memory; tuning the JVM's code cache (eg -XX:InitialCodeCacheSize and -XX:ReservedCodeCacheSize); other compiler flags like -XX:CompileThreshold.
- When tuning: make single changes and incrementally adjust based on monitoring results; test under realistic load conditions; regularly review and update JVM settings to align with application changes and JVM updates.
- Designing your application to effectively utilize multiple cores and threads is key in low-latency systems. Ensure that threads work efficiently and safely together, striking the right balance between doing things concurrently, to speed up processing, and managing the overhead that concurrency introduces.
- In network and I/O-heavy applications, prevent threads from being idle while waiting for data by using asynchronous patterns.
- Cache effectively: determine what to cache, when to cache it, and for how long to optimise your application's data access needs while ensuring that the cached data remains relevant and up-to-date.
- An event-driven architecture is particularly useful in applications where real-time responsiveness to external changes is critical.
Jack Shirazi
Back to newsletter 277 contents
Last Updated: 2024-10-29
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/newtips277.shtml
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us