query regarding garbage collector behavior - java

As I read in the Sun Memory management white paper:
When stop-the-world garbage collection is performed, execution of the application is completely
suspended during the collection
So if a request happens while the garbage collector is running then how is it handled by the application? If the garbage collector takes too long will the application throw an exception? I have not come across such an issue but wanted to know is this possible and what exception gets thrown?

All (almost) Java garbage collectors has some sort of a Stop-the-world phase where all the Java threads are suspended waiting for a exclusive system operations to complete. This state is sometimes referred to as a safepoint.
The modern garbage collectors are concurrently running together with the applications threads, which means that the garbage collector perform its work at the same time as the application. During the garbage collector process there are phases where exclusive access memory is needed, the application threads goes into this safepoint state.
An exception is thrown if the garbage collector cannot recover enough memory to meet the applicationĀ“s allocation demands.
One alternative to get rid of the stop-the-world garbage collections is to go for the Zing JVM with the C4 collector from Azul systems. The implementation has a low pause approach with no stop-the-world collections at all. Instead it is using a concurrent compacting approach with no stop-the-world phase.

This garbage collector is not in use anymore and replaced by better garbage collectors.
The stop-the-world garbage collector really stopped the complete application (all threads) and cleaned up the heap.
When the garbage collector would take too long (which almost never happens) then an Error would be thrown.
Incomming traffic on network sockets is buffered for the time the collector runs.

Related

What is the difference between G1GC options -XX:ParallelGCThreads vs -XX:ConcGCThreads

when configuring the G1GC
we have 2 kinds of thread count
-XX:ParallelGCThreads and -XX:ConcGCThreads
what is the difference, how they are going to impact,
any reference is appreciated.
G1 algorithm has phases which some of them are "stop the world" phases that stops the application during garbage collection, and it also has phases which happens concurrently while application is running(candidate marking etc..), with that information in mind:
ParallelGCThreads option affects the number of threads used for phases when application threads are stopped, and the ConcGCThreads flag affects the number of threads used for concurrent phases.
It is the setting or precisely say JVM tuning settings... we inform JVM to use how many threads in that particular type of Garbage Collection.
I hope you are already aware of what is Garbage Collection, so when JVM runs Garbage Collection, it depends on what algorithm is set for your JVM as default collector.
You might already be knowing that there are various kind of Garbage collectors available, like G1, CMS, etc.
So, based on your setting (here, number of threads) GC algorithm will try to use that many threads for heap cleanup. While JVM runs FULL GC, it halts other threads processing.
Now suppose, your application is live and performing very heavy tasks, multiple users using it for multiple purpose (say very busy app), and JVM running FULL GC now, then in that case, all worker threads will come to a pause and GC will clean up. In this period, if all threads are acquired by JVM, then user will see delay in response. So, you can tell JVM, hey Use only that much (number) thread on that type (CMS or Parallel) of garbage collection run.
To get more on GC types and how they differ, whats suits your needs, refer some good article and docs from oracle.
Here is one reference for the options you mentioned.
-XX:ParallelGCThreads: Sets the number of threads used during parallel phases of the garbage collectors. The default value varies with the platform on which the JVM is running.
-XX:ConcGCThreads: Number of threads concurrent garbage collectors will use. The default value varies with the platform on which the JVM
is running.

Java heap full of java finalize() calls

My application works fine in two environments but in acceptance test environment there is a memory leak. JVM heap dump shows that the heap is 98% full of java.object.finalize() (if I remember correctly. GC logs state that GC is doing a Full GC more and more often, until the JVM gives Out of Memory -error.
What could be causing this? The application is the same in all environments but the OS and networking is slightly different in the problem environment. Java is the same on all environments. What does this output even mean, that GC has called Object Finalize() but for some reason it does not complete? I am using JBoss EAP 5.
It is possible to create garbage that requires finalization faster than it can be finalized. When an object is determined to be unreachable (i.e., eligible for collection) and requires finalization, it is added to the finalizer queue rather than being collected immediately. On the next GC following the completion of the finalizer, the object can be collected. In other words, finalization delays the recovery of memory.
There is a single thread that executes all finalize methods and by default it runs at the same priority as all the other threads. If you have many threads creating garbage that requires finalization, they can produces garbage faster than the one finalizer thread can execute the finalize() methods. Also, if you finalize methods are not fast, you just make this problem worse.
To summarize, you probably have too many and/or too slow finalize() methods to execute. It might be that in the one problem environment the finalize methods are slower? Or you have more processing power so you create garbage faster there?
I have have run into this problem in the past. To mitigate it, I raised the priority on the finalizer thread. The trick to doing this was to create a piece of garbage during initialization that overrides finalize() and changes the thread priority in the current thread during finalization. Changing the finalizer thread's priority was enough to solve the problem in that case, but is not guaranteed to do the trick.

Garbage collector for young generation

Have sort question - is it true that all GC in JDK 7 (other than G1) always use stop-the-world for young generation collection?
thanks
For OpenJDK, JRockit, IBM JVM, and Sun/Oracle JDK, the young collection is always stop the world for every available collector.
The only JVM I know of which does not have a stop the world collector is Azul's Zing. (Not free)
While OpenJDK/Hotspot has CMS this is mostly concurrent. There is still stop the world portions and in some cases CMS will fall back to a Full GC which is stop-the-world.
AFAIK, It is hard to find real world examples where G1 is faster in terms of pause time than CMS, however it is improving all the time.
Do your GC logs speak to you
All (almost) Java garbage collectors has some sort of a Stop-the-world phase where all the Java threads (not native threads) are suspended waiting for exclusive system operations to complete. This state is sometimes referred to as a safepoint.
The modern garbage collectors are concurrently running together with the applications threads, which means that the garbage collector perform its work at the same time as the application threads are running. During the garbage collector process there are phases where exclusive access memory is needed, in that phase the application Java threads goes into the safepoint state.
One alternative to get rid of the stop-the-world garbage collections is to go for the Zing JVM with the C4 collector from Azul systems. The implementation has a low pause approach with no stop-the-world collections at all. Instead it is using a concurrent compacting approach with no stop-the-world phase.
No it is not true. Java 7 also supports the older Concurrent Mark Sweep (CMS) collector. CMS is a low pause collector, just like G1.
UPDATE
Apparently CMS is only for the tenured generation ... according to the blog posting that you found at http://blogs.oracle.com/jonthecollector/entry/our_collectors
So that means that your proposition is in fact true.
One could argue that all of the low-pause collectors:
- need to stop the mutator threads to do some phases of their work, and
- may fall back to a Full GC using the mark/sweep collector when they can't keep up.
However, there is a qualitive difference between "mostly concurrent" collectors like G1 and CMS, and other collectors that suspend non-GC threads for the entire duration of the collection process. That is what is normally meant by a "stop the world" strategy.

Can we call the Garbage Collector explicitly? [duplicate]

This question already has answers here:
Why is it bad practice to call System.gc()?
(13 answers)
Closed 9 years ago.
My application has lot of iterations. Till now I haven't faced any memory issues. But from code level I can suspect there are few places, which causes memory leaks and out of memory problem. I am thinking of calling garbage collector manually. Is it good practice to call Garbage Collector manually?
You can call Garbage collector using:
System.gc();
But this does not mean that it'll be executed immediately. The JVM decides when to execute it. In general if the JVM is about to throw an OutOfMemoryError, calling System.gc() won't prevent it. Better investigate why you're leaking so much memory and clean it up along the way.
JavaDoc:
Calling the gc method suggests that the Java Virtual Machine expend
effort toward recycling unused objects in order to make the memory
they currently occupy available for quick reuse. When control returns
from the method call, the Java Virtual Machine has made a best effort
to reclaim space from all discarded objects
Is it good practice to call Garbage Collector manually?
No, it is definitely not good practice.
You can use System.gc(). Note that this is not guaranteed to call the garbage collector - it only gives a hint to the system that it might be a good idea to do garbage collection.
The garbage collector in Oracle's JVM contains a lot of sophisticated logic to determine when and what to cleanup. Tuning it requires knowledge about details on how it works. Just putting a System.gc() somewhere in your program is not likely to help a lot, and in fact, it can even make it worse.
See Java SE 6 HotSpot Virtual Machine Garbage Collection Tuning for details on how to tune garbage collection with Java SE 6.
Yes you can explicitly call garbage collector using
System.gc();
But what happens is that you can't order JVM to do garbage collection immediately. JVM decides on its own when to garbage collect.Hence its not a good idea of calling it manually.
Moreover regarding OutOfMemoryException doing manual garbage collection won't help you prevent the exception, since JVM throws this exception after reclaiming all the memory it can. It has some very sophisticated algorithms to determine when and how to do perform the garbage collection.
So what i suggest is that if you are getting OutOfMemoryException then recheck your program make it more efficient or increase heap space.
You can call Garbage Collector explicitly, but JVM decides whether to process the call or not.
Ideally, you should never write code dependent on call to garbage collector.
JVM internally uses some algorithm to decide when to make this call. When you make call using System.gc(), it is just a request to JVM and JVM can anytime decide to ignore it.
Regardless of whether you can or cannot manually trigger the garbage collector (and the different levels of collection), and what impact this has on performance (which indeed is a topic worth discussion), it will not prevent OutOfMemoryErrors, because when the JVM is about to run out of memory, it does the most thorough collection it can anyway. Only if after that collection not enough memory is available, will it error out. Even if you trigger the collection earlier yourself, the result (the amount of memory reclaimed) is the same.
Memory leaks cannot be fixed by running garbage collection more often.
They have to be fixed in your program (stop referencing things you don't need anymore earlier), or (worst case, if it is a "real" leak) in the JVM or runtime library itself (but genuine memory management bugs should not exist anymore after so many years of service).
Calling System.gc() does not guarantee any GC. Garbage is collected if there is a real need for memory.
You can have a look at different garbage collectors here.
http://javarevisited.blogspot.in/2011/04/garbage-collection-in-java.html
You can include any of these GCs in your command line params as per your needs.

Does the Garbage collector in Java work automatically?

I need to to know whether the Garbage Collector in Java works on its own or we do need to start it.
It works on its own. You can "suggest" that it run, but that's about it.
The GC is a deamon thread that's started with your JVM and ends when your JVM ends (JVM is stoped if no more non-deamon threads exist).
It runs in background and kicks into action when/if needed. The JVM decides when it runs and you can "request" it to run with System.gc().
But I should mention that you must not write your code to depend on the GC to run (finalizers in Java are not like destructors in C++). People tend to count to much on the GC and then forget about it which is a no-no and leads to memory leaks and hard to find bugs.
What you can count on is that before you get a java.lang.OutOfMemoryError, the GC kiked into action and did its best.
It works on its own according to an optimized algorithm to get the optimal performance. You can perform a force garbage collection but it is not recommended because it can block the normal Garbage collection pattern reducing the actual performance.
You should read this Old SO Discussion on a related topic.
Yes, garbage collection is automatically handelled by Java.
When an object is no longer referred to by any variable, Java automatically reclaims memory used by that object. This is known as garbage collection.
Still the method
System.gc() method may be used to call it explicitly.
The only occasion I know of where you have to call System.gc() is when you are creating lots of Direct ByteBuffers. For heap memory, an OutOfMemoryError will only occur after a Full GC, however for direct memory, if you run out (even though there might be buffers which are not referenced), no GC is called and so you might have to call it yourself before trying again.
I would hope this is something which is fixed in future versions of Java.
(This is probably just repeating what other answers were trying to say ... However, it is worth saying this clearly.)
The Java garbage collector runs automatically as required, and there is no need to start it.
There is a static method (System.gc()) that an application can call to request that the garbage collector run "now". However:
The JVM can be configured to pay no attention to this request.
It is generally a bad idea to make this request because it can significantly degrade garbage collector performance. Generally speaking, the best time to run the garbage collector is when there is lots of garbage to be collected, and only the JVM knows when that is likely to be.
EDIT - the cure for large garbage collection latencies is to change the JVM's garbage collector properties to select the low latency collector. Calling System.gc() is not the way to deal with this in a modern JVM.

Categories

Resources