JVM - effect of Xms on commited memory and garbage collection - optimization

We have a tomcat with following arguments
Xms 1g
Xmx 4g
Parallel GC
It is installed in Ubuntu machine with JVM 1.8.181
Lately GC is being started with full throttle and doesn't let any other process go on. What I don't understand is this takes place when even total JVM is just 2.8 GB while maximum heap can go is 4GB. Why does full GC run when mamory has not reached to max?
When I dug deep, ii found that there is a sudden change in the used and committed memory; from 1+GB to ~4GB. Does that mean that because I had set the min heap to 1 GB, it goes till 1GB only and as soon as it reaches there it increases to a next step? Because of this the Garbage collection takes place?
If yes that does that mean that in order to avoid this situation, I need to increase the min heap?
More info- this is happening when there almost 0 traffic. No background process is going on. I understand it can build up but without using anything, how can it go up! - I need to figure this out myself.

When you set the min heap to 1 GB, it starts with a 1 GB heap, thought the process itself could be a few 100 MB to a few GB more than this depending on which libraries you use. i.e. the resident size can be larger.
As the pressure on the heap grows from activity, it may decide it needs to increase the heap size. There has to be a significant load to trigger this to happen otherwise it won't change the heap.

Related

How Anypoint monitoring JVM Heap and Runtime manager vCore are related?

I could notice that 0.2vCore is allotted to API in Rruntime Manager, memory utilization is 63%.
When i check Heap utilization in Anypoint Monitoring is fluctuating between 200MB and 810MB and max Heap size mentioned 870MB this raised some doubts.
Runtime manager vCore and Anypoint monitor Heap Size are same ? I allotted 1 GB of vCore but in Heap graphs maximum heap size is showing in between 850-870 MB depends on time. my question is why it is not showing maximum available heap size is 1GB ?
Heap graph is not falling below 200MB, will it reach 0MB at any point ? or any kind of compile code occupies this 200MB space ?
I'm bit confused here, can anybody clarify here please..
Thanks..,
Runtime manager vCore and Anypoint monitor Heap Size are same ? if yes why only 870MB Heap is available instead of 1 GB(0.2vCore) allotted ?
A 0.2 vCore worker has a maximum heap size of 1 GB. It may be that the JVM didn't need to increase the actually used maximum size to the maximum available.
Heap graph is not falling below 200MB, will it reach 0MB at any point ? or any kind of compile code occupies this 200MB space ?
Any Java application will have some objects created, if not by the application by the JVM runtime itself, to execute. That means that it will have a baseline minimum heap usage. I don't think it is possible for any running JVM to have 0 MB usage of heap.
Your application gets loaded into the JVM, objects are created and it will take up some memory. the 200 is your base line in this case. Then as more events gets created, the heap size increases, and as events expire, garbage collection comes into play and free up memory.
More details can be found in the following link.
https://help.mulesoft.com/s/article/Java-JVM-memory-allocation-pattern-explained

JVM behaviour when OS runs out of memory

I am wondering what's the JVM behaviour for the following situation:
JVM minimum heap size = 500MB
JVM maximum heap size = 2GB
OS has 1GB memory
After the JVM started and the program runs for a period of time, it uses more than 1GB memory. I wonder if OOM will happen immediately or it will try to GC first!
It depends on how much swap space you have.
If you don't have enough free swap, the JVM won't start as it can't allocate enough virtual memory.
If you have enough free swap your program could start and run. However once a JVM starts swapping its heap the GC times rise dramatically. The GC assumes it can access the heap somewhat randomly.
If your heap can't fit in main memory, the program, and possibly the machine becomes unusable. In my experience, on Windows, a reboot is needed at this point. On Linux, I usually find I can kill the process.
In short, you might be able to start the JVM, but it's a bad idea.

Performance issue in Websphere8.5

We migrated web application from jsf1.0 to 1.2 and deployed in Websphere 8.5. EArlier application was deployed in Websphere6.0. We are facing performance issue during SOAK testing. Got some thread hung message in sysout logs also i observe lot of blocking thread in thread dump file and its released on time.
Application performance degrades on time. i can see the performance issue remains same even the application is idle for 1 day .
Main issue is with the High CPU usage and high JVM memory even the application is idle for 1 day. Application is fast after the restart of server. Does the GC will not clear the JVM memory for 1 day or why this CPU is high ?
High cpu with low/declining app throughput is typical of java heap exhaust, when the JVM spends most of its time running GC trying to clear space in the heap to do real work. You should enable verbose GC logging, the GC log will show the heap state and GC activity. If the heap is below 10% tenure/OldGen free (assuming using default gencon collector) after a global/full GC, you are in heap exhaust state.
You could try increasing the heap size, maybe it just needs more space than currently provided. If the heap use (used tenure after global) continues to climb over time, when the workload offered is steady/constant, then the app probably has a memory leak. The objects accumulating in the heap can be seen by taking a core/system dump when the server is near heap exhaust state, and examining the dump with e.g. Eclipse Memory Analyzer.

Is there any advantage in setting Xms and Xmx to the same value?

Usually I set -Xms512m and -Xmx1g so that when JVM starts it allocates 512MB and gradually increases heap to 1GB as necessary. But I see these values set to same say 1g in a dedicated server instance. Is there any advantage for the having both set to the same value?
Well there are couple of things.
Program will start with -Xms value and if the value is lesser it will eventually force GC to occur more frequently
Once the program reaches -Xms heap, jvm request OS for additional memory and eventually grabs -Xmx that requires additional time leading to performance issue, you might as well set it to that at the beginning avoiding jvm to request additional memory.
It is very nicely answered here - https://developer.jboss.org/thread/149559?_sscc=t
From Oracle Java SE 8 docs:
https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/sizing.html
By default, the virtual machine grows or shrinks the heap at each
collection to try to keep the proportion of free space to live objects
at each collection within a specific range. This target range is set
as a percentage by the parameters -XX:MinHeapFreeRatio=<minimum> and
-XX:MaxHeapFreeRatio=<maximum>, and the total size is bounded below by -Xms<min> and above by -Xmx<max>. Setting -Xms and -Xmx to the same value increases predictability by removing the most important sizing
decision from the virtual machine. However, the virtual machine is
then unable to compensate if you make a poor choice.
if the value of -Xms and -Xmx is same JVM will not have to adjust the heap size and that means less work by JVM and more time to your application. but if the chosen value is a poor choice for -Xms then some of the memory allocated will never be used because the heap will never shrink and if it is a poor choice for -Xmx you will get OutOfMemoryError.
AFAIK One more reason, is that expansion of heap is a stop-the-world event; setting those to the same value will prevent that.
There are some advantages.
if you know the size is going to grow to the maximum, e.g. in a benchmark, you may as well start with the size you know you need.
you can get better performance giving the program more memory that it might naturally give itself. YMWV
In general, I would make the Xms a value I am confident it will use, and the double this for head room for future use cases or situations we haven't tested for. i.e. a size we don't expect but it might use.
In short, the maximum is the point you would rather the program fail than use any more.
Application will suffer frequent GC with lower -Xms value.
Every time asking for more memory from OS with consume time.
Above all, if your application is performance critical then you would certainly want to avoid memory pages swapping out to/from disk as this will cause GC consuming more time. To avoid this, memory can be locked. But if Xms and Xmx are not same then memory allocated after initial allocation will not be locked.

Websphere heap size 4GB. Will memory exceed heap running two threads each consuming 3 GB memory in parallel or will websphere pause one thread?

Sorry for the vagueness, but I'm just trying to understand websphere memory management at a high level.
This is really a question about JVM behavior. As far as I know, there are no JVMs that will block a thread waiting for another thread to finish if it is holding a large amount of memory. I expect both threads to continuously consume memory, and if both are able to allocate memory at the same rate, I would expect them both to get OutOfMemoryError as soon as their combined allocations exceed the max heap size.