In our everyday Java programming, we rely on familiar APIs without fully realizing their hidden performance impacts. This session aims to unveil the concealed performance aspects of common Java APIs and shed light on how they can influence your application's performance.
Join us to explore the unnoticed performance effects of these APIs and learn strategies to mitigate their impact. Whether you're a seasoned developer or new to Java, this paper equips you with essential knowledge to optimize your applications.
7. Thread dump analysis report – stack trace
STATE : BLOCKED
java.security.SecureRandom.nextBytes(SecureRandom.java:433)
java.util.UUID.randomUUID(UUID.java:159)
com.buggycompany.jtm.bp.<init>(bp.java:185)
com.buggycompany.jtm.a4.f(a4.java:94)
com.buggycompany.agent.trace.RootTracer.topComponentMethodBbuggycompanyin(RootTracer.java:439)
weblogicx.servlet.gzip.filter.GZIPFilter.doFilter(GZIPFilter.java)
weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3730)
weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3696)
weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2273)
weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1490)
weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
Checking Entropy in Linux
cat /proc/sys/kernel/random/entropy_avail
If < 1000, it’s a problem
8. Solution
• RHEL
• Upgrade to RHEL 7 or above version
• If < RHEL 7, follow recommendations given here
• Install Haveged Library - Unpredictable Random number generator
• Use /dev/urandom instead of /dev/random
• ‘/dev/random’ serve as pseudorandom number generators
• ‘/dev/urandom’ is another special file that is capable of generating random
numbers. Downside: reduced security due to less randomness
• -Djava.security.egd=file:/dev/urandom
10. System.getProperty()
• ‘java.lang.System.getProperty()’ API underlyingly uses
‘java.util.Hashtable.get()’ API.
public synchronized V get(Object key) {
:
:
}
• If used in critical code path, can significantly affect application
performance
12. Victim Thread Stack trace
http-nio-8080-exec-293
Stack Trace is:
java.lang.Thread.State: BLOCKED (on object monitor)
at java.util.Hashtable.get(Hashtable.java:362)
- waiting to lock <0x0000000080f5e118> (a java.util.Properties)
at java.util.Properties.getProperty(Properties.java:969)
at java.util.Properties.getProperty(Properties.java:988)
at java.lang.System.getProperty(System.java:756)
at net.java.ao.atlassian.ConverterUtils.enforceLength(ConverterUtils.java:16)
at net.java.ao.atlassian.ConverterUtils.checkLength(ConverterUtils.java:9)
:
13. Culprit Thread Stack trace
Camel Thread #6 – backboneThreadPool
Stack Trace is:
at java.util.Hashtable.get(Hashtable.java:362)
- locked <0x0000000080f5e118> (a java.util.Properties)
at java.util.Properties.getProperty(Properties.java:969)
at java.util.Properties.getProperty(Properties.java:988)
at java.lang.System.getProperty(System.java:756)
at net.java.ao.atlassian.ConverterUtils.enforceLength(ConverterUtils.java:16)
at net.java.ao.atlassian.ConverterUtils.checkLength(ConverterUtils.java:9)
:
14. Solution
• Upgrade to JDK 11 or above
Synchronized HashTable has been replaced with ConcurrentHashMap
• Cache the values:
public static String getAppName() {
String app = System.getProperty("appName");
return app;
}
private static String app = System.getProperty("appName");
public static String getAppName() {
return app;
}
16. Interview question
• What is the difference between HashMap and HashTable?
• But what happens when you do concurrent put() and get() on
HashMap -
• How to diagnose CPU spike?
top –H –p <PROCESS_ID> + Thread dump
27. Real world example – Trading app
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
30. 1 million threads
for (int i = 0; i < 1_000_000; i++) {
new Thread(new Runnable() {
@Override
public void run() {
TimeUnit.HOURS.sleep(1);
}
}).start();
}
31. Performance Comparison
Thread Count Memory Size Thread Analysis Heap Analysis
Platform Threads 1599.
After that
OutOfMemoryErr
or
1.85 MB https://tinyurl.co
m/ntfastthread
https://tinyurl.co
m/ntheaphero
Virtual Threads 1 million.
No issues
401 MB https://tinyurl.co
m/vtfastthread
https://tinyurl.co
m/vtheaphero
39. Real world – Long GC Pause in Top Cloud Provider
https://blog.gceasy.io/2022/03/04/garbage-collection-tuning-success-story-reducing-young-gen-size/
40. What is GC throughput?
How does 96% GC Throughput sound?
1 day = 1440 Minutes (i.e., 24 hours x 60 minutes)
96% GC Throughput means app pausing for 57.6
minutes/day
Amount of time application spends in processing customer
transactions
vs
Amount of time application spends in processing garbage
collection activity
44. How to tune GC Performance?
Free Video: https://www.youtube.com/watch?v=6G0E4O5yxks
Online Training: https://ycrash.io/java-performance-training
45. Thank you friends!
Ram Lakshmanan
ram@tier1app.com
@tier1app
linkedin.com/company/ycrash
This deck will be published in: https://blog.fastthread.io