Hi , A doubt in GC. When ever the JVM starts the garbage collecteor is run as a low priority thread. Then what is the need of System.gc()?? Since Garbage Collector is already running(but donno when it'll garbage collect)what is the need for System.gc()??
Shilpa, GC runs as a low priority thread. i.e. it will be in runnable state (not running). GC is unpredictable when it runs. GC thread gets its chance to run depending on priority or time based system, among all other threads. System.gc() method is forcing GC but no guranteed that GC will run. Hope this helps Ram
Garbage collection NEVER runs automatically.It runs upon recieving a request.Most often the request comes from the runtime environment.When your program starts running, it is allocated memory space on the heap by the runtime.So if your program creates too many objects and does not release references to them, your program will eventually run out memory(Not system memory, but the memory that it was allocated on the heap).When the runtime establishes that your program has run out of its memory on the heap, it sends the request to the Garbage Collector to run so that it frees up some memory for your program.It is at this point that the Garbage Collector registers itself with the thread scheduler and later gets a chance to run as a low priority thread.So as you can see, the runtime will only request for GC to be run when your program has exhausted all its memory.This is probably the reason why we say there is no guarantee that GC will run, because there is no guarantee that your program will use up all its memory. However, as a programmer you may be writing some piece of code which you definitely know that is going to use up a lot of memory.At this point you don't want to wait for your program to run out of memory before GC starts, so you make the request yourself, instead of waiting for the runtime to do it for you.So you invoke System.gc to suggest that you would like the Garbage collector to free up some memory resources in use by your program. Regards, Herbert.
Herbert- garbage collection can indeed run automatically even if there has been no out of memory error and no explicit gc() call. It isn't guaranteed anywhere I'm aware of, but most implementations of Java will create a low-priority thread to call System.gc() whenever no higher-priority threads are taking up system time. Note that this does not mean that all garbage collection is low-priority. In the case of an impending out-of-memory error, I expect it's pretty high priority - the whole system is about to crash after all if memory isn't freed, so in this situation the JVM had better give GC full and immediate attention. And when you call System.gc(), garbage collection should have the same priority as the thread you called it from, or higher. (I.e. once GC does successfully start it may well hold up all other activity in the JVM until it's done.) The reason for System.gc() is so you can, err, encourage the JVM to run at a time that's convenient for you, rather than either (a) when it gets around to it, or (b) when it absolutely has to. Because the latter especially may impact performance.