Problem Statement: On increasing our JBoss heap-size to 2GB from 1GB, JBoss was crashing every 30-60 mins.
On further analysis, it was found that the issue here was with number of threads.
Solution:
> Well, best solution is to check why your application is creating so many threads. You can check out thread-dumps for the same or use some profiling tool to find out what all threads are created by your application and if you can do something about it at application level.
> Second solution is to go for thread-pooling.
> Third solution, you can tune JVM thread stack space.
Of course, ideally, all the 3 solutions should be used. However, third one is the quickest one to start with.
Tuning JVM thread stack space:
Every thread created in a Java program has its own stack space outside the heap. So if you look at the OS report on the memory used by your JVM, you may notice that it is more than what -Xmx parameter specifies.
Default value of this stack size varies based on JVM version and OS version ranging up to 2MB.
Normally 128k is enough for all the applications. If your application requires more stack space, you will get StackOverflowError at some-point, so you need to observer application logs for this error.
You can assign thread-stack-space value with JVM option –Xss. (e.g. –Xss128k)
Following is the result of a sample program that I executed to find how many threads can be created with different stack-space (Xss) values.
(Please note: The results will be different on 64-bit system)
Important points to note:
- For same "Thread-Stack-Size (Xss)" value, supported thread-count increases as we reduce max-heap-size
- For same "Max-Heap-Size (Xmx)" value, supported thread-count increases as we reduce thread-stack-size
OS (32-bit)
|
Java-Version
|
Max-Heap-Size
|
Thread-Stack-Size
|
Thread-count
|
RHEL
|
1.5
|
1024m
|
Default
|
3636
|
RHEL
|
1.5
|
1250m
|
Default
|
3196
|
RHEL
|
1.5
|
1536m
|
Default
|
2632
|
RHEL
|
1.5
|
1750m
|
Default
|
2206
|
RHEL
|
1.5
|
2g
|
Default
|
1615
|
RHEL
|
1.5
|
1024m
|
128k
|
13947
|
RHEL
|
1.5
|
1250m
|
128k
|
12245
|
RHEL
|
1.5
|
1536m
|
128k
|
10083
|
RHEL
|
1.5
|
1750m
|
128k
|
8466
|
RHEL
|
1.5
|
2g
|
128k
|
6215
|
RHEL
|
1.5
|
1024m
|
256k
|
7183
|
RHEL
|
1.5
|
1250m
|
256k
|
6301
|
RHEL
|
1.5
|
1536m
|
256k
|
5190
|
RHEL
|
1.5
|
1750m
|
256k
|
4365
|
RHEL
|
1.5
|
2g
|
256k
|
3198
|
WinXP
|
1.5
|
1024m
|
Default
|
3388
|
WinXP
|
1.5
|
1250m
|
Default
|
2495
|
WinXP
|
1.5
|
1536m
|
Default
|
NA*
|
WinXP
|
1.5
|
1750m
|
Default
|
NA*
|
WinXP
|
1.5
|
2g
|
Default
|
NA*
|
WinXP
|
1.5
|
1024m
|
128k
|
3388
|
WinXP
|
1.5
|
1250m
|
128k
|
2495
|
WinXP
|
1.5
|
1536m
|
128k
|
NA*
|
WinXP
|
1.5
|
1750m
|
128k
|
NA*
|
WinXP
|
1.5
|
2g
|
128k
|
NA*
|
WinXP
|
1.5
|
1024m
|
256k
|
855
|
WinXP
|
1.5
|
1250m
|
256k
|
631
|
WinXP
|
1.5
|
1536m
|
256k
|
NA*
|
WinXP
|
1.5
|
1750m
|
256k
|
NA*
|
WinXP
|
1.5
|
2g
|
256k
|
NA*
|
*NA – My machine does not have enough memory to allocate to JVM
Cheers,
Sarang
Hey Sarang this is good stuff , thanks
ReplyDelete