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 April 2025
|
JProfiler
|
|
Get rid of your performance problems and memory leaks!
|
|
JProfiler
|
|
Get rid of your performance problems and memory leaks!
|
|
|
Back to newsletter 293 contents
https://www.youtube.com/watch?v=Qo-g2SK5MGo
Observability for developers (Page last updated April 2025, Added 2025-04-28, Author Melvin Visch, Publisher Devoxx). Tips:
- Observability for developers is about providing data in context, with context-relevant telemetry data immediately accessible.
- Observability used by IT Ops and DevOps teams focuses on production health (uptime, latency, errors) who apply configuration changes to address issues. But a cloud-native focus and increasing system complexity makes observability crucial for developers to fix bugs, understand feature usage, and analyze the impact of code changes.
- Key Features of Observability for developers: Logs, Traces, and Metrics - these remain fundamental, providing runtime code insights, it is important to view them in context and correlate them; Snapshots, Exceptions, and Profiling - continuous profiling helps understand application behavior, exceptions pinpoint error sources, and snapshots capture the full context of the application state at specific points in time; Dashboards - still essential for visualizing code behavior and identifying anomalies.
- Snapshots enable inspecting production code without halting production execution, significantly speeding debugging by eliminating the need to reproduce issues locally.
https://medium.com/@trek007/advanced-java-performance-optimisation-a-deep-dive-into-memory-threading-and-modern-jvm-0aba5bbae93d
Advanced Java Performance Optimisation: A Deep Dive into Memory, Threading, and Modern JVM (Page last updated November 2024, Added 2025-04-28, Author Trek, Publisher Trek). Tips:
- JVM performance has significantly evolved since Java 8. Key improvements include G1GC, ZGC, Shenandoah, virtual threads and better vectorization support. Traditional optimization techniques might now be anti-patterns.
- Optimizations must consider CPU registers, L1-L3 caches, main memory, and SSD access times. Rearranging fields can significantly reduce padding. Minimizing memory padding within objects improves cache line utilization and reduces memory footprint, leading to better GC performance and memory locality. Better cache utilization means fewer CPU stalls. Reduced memory footprint leads to better GC performance and improved memory locality for frequently accessed fields.
- Object Pooling reduces GC pressure by reusing objects. This is important for frequently created and destroyed objects. Prefer primitive types over wrapper objects (e.g., `int` over `Integer`) to avoid object allocation overhead. Custom value types can improve performance (example: `PricePoint` instead of `BigDecimal`).
- Virtual threads simplify concurrent programming.
- Prioritize tasks based on their importance using a custom scheduler.
- Use lock-free data structures (eg queues) for critical paths to minimize contention and improve performance.
- Utilize CPU-specific features by setting JVM flags (e.g., -XX:+UseAVX -XX:+UseCLMUL -XX:+UseFMA -XX:+UseXMMForArrayCopy). Create a system to detect CPU capabilities and dynamically set JVM options.
- Understand GC behavior and tune accordingly.
- Always measure before and after optimizing. Use profilers. Consider the cost-benefit.
- Implement proper resource cleanup, try-catch, Cleaner. Use weak references. Monitor memory usage.
- Use asynchronous operations. Implement backpressure. Consider non-blocking alternatives.
https://skilledcoder.medium.com/how-your-code-structure-is-causing-java-memory-leaks-e035fbb1b85e
How Your Code Structure Is Causing Java Memory Leaks (Page last updated April 2025, Added 2025-04-28, Author Skilled Coder, Publisher Skilled Coder). Tips:
- Memory leak anti-pattern: Using a singleton holding mutable state. Eg storing per-user or per-request state in it. Fix by: don't store per-user/request state in singletons; use a proper cache with TTL/size limits; for manual maps, use WeakHashMap or add cleanup logic; use scoped storage (e.g., per-request, per-session beans).
- Memory leak anti-pattern: Builder patterns where each chained method returns a new modified object. You intend to be immutable, but with many builder implementations, you're actually holding references to the entire chain. Fix by: null out heavy fields after build(); don't reuse builders that hold references; prefer stateless builders or immutables; or don't reuse the builder at all.
- Memory leak anti-pattern: Decorator/Composite chains, eg layer multiple decorators over an object for logging, tracing, auth, etc. Each decorator holds a strong reference to the inner one. If the outer decorator is stored in a registry or injected into a singleton bean, every layer underneath stays in memory. Fix by: introduce a dispose()/close() methods in your decorators to release references and ensure calling them; avoid storing decorated services, decorate at call time; use dynamic proxies or AOP instead of manual decorators.
- Memory leak anti-pattern: Dependency injection extending lifetimes. Fix by: making beans have request scope, or loading them lazily; don't inject heavyweight beans into long-lived components; explicitly destroy or release buffers.
- Memory leak anti-pattern: Observer/Event bus missing unsubscriptions. Fix by: always unregister listeners when done; use weak references or custom buses with auto-cleanup; if using reactive APIs, ensure completion/error handlers unsubscribe.
- Memory leaks in Java don't always come from bad code, the garbage collector only cleans what's unreachable. Your job is to design for reachability. Know your lifecycles. Watch your references. Fix your architecture to eliminate implementations that use leaky anti-patterns, eg. singleton holding mutable state, builder patterns where build() holds chains of built objects, decorators that keep alive the objects they decorate, dependency injection extending object lifetime, missing unsubscriptions.
Jack Shirazi
Back to newsletter 293 contents
Last Updated: 2025-12-25
Copyright © 2000-2025 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/newtips293.shtml
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us