SiteMap  | About    

Does garbage collection guarantee that a program will not run out of memory?

No, Garbage collection does not guarantee that a program will not run out of memory.

The purpose of garbage collection (GC) is to identify and discard objects that are no longer needed by a Java program, so that their resources can be reclaimed and reused. An object is subjected to garbage collection when it becomes unreachable to the program in which it is used. If there is insufficient memory remaining to satisfy the amount needed for a new object, then the garbage collector will attempt to reclaim as much memory as possible by releasing memory used by objects to which there are no longer any references. However, if there's still insufficient memory, an 'out of memory' exception will be thrown.

It is perfectly possible for a developer to mistakenly create objects which never go out of scope, thus consuming more and more memory until all heap is exhausted. It is the developer's responsibility to ensure that objects no longer in use are no longer referenced by the application. That way the garbage collector can do its job and reclaim memory used by these objects. So to conclude, garbage collection (GC) handles most of the memory management and makes all possible attempts to make memory available for the application but it does not guarantee that it will be able to provide it. (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.