The Garbage Collector


Memory for Java objects is dynamically allocated at runtime. Other languages also support dynamic memory allocation. In C you can dynamically allocate memory to variables , structs, and arrays, but you must take care of deallocating memory when the field is no longer needed. If you don't do it properly, you get the dreaded memory leak.

Java has a very nice built-in system that takes care of memory management for you. The Java runtime environment uses what is called the garbage collector to deallocate resources assigned to objects that are no longer being used. An object is eligible for garbage collection when the system determines there are no longer any references to it. This generally happens when the variable goes out of scope or when the last reference to the variable is set to null . The garbage collector runs automatically in a background execution thread. You can manually invoke the garbage collector by calling the static gc() method from the System class.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net