CS202 Java-49
Garbage Collection and Objects
lRemember the problems of returning local objects in C++ where the lifetime has ended?
•We don’t have this type of problem in Java because objects created with new exist for as long as we need them and we don’t have to worry about destroying them
•Java has a garbage collector, which looks at all of the objects created with new and determines which ones are not being referenced anymore – then it can release the memory for those objects at that point so the memory can be used for new objects.
•Please keep in mind that although the garbage collector can release the memory when no more references point to the memory, it may not if the memory is not needed elsewhere
•On the other hand, this means that you never need to worry about reclaiming memory yourself
•Simply create objects, and when you no longer need them they will go away by themselves whenever necessary
•There are no memory leaks!
•