Garbage Collection

Objects are created via reserved word new, but how are they destroyed? Without some way to destroy objects, they will eventually fill up the heap's available space and the application will not be able to continue.

Java solves this problem by using a garbage collector, code that runs in the background and checks for unreferenced objects. When it discovers an unreferenced object, the garbage collector removes it from the heap, making more heap space available.

An unreferenced object is an object that cannot be accessed from anywhere within an application. For example, new Employee("John", "Doe"); is an unreferenced object because the Employee reference returned by new is thrown away.

In contrast, a referenced object is an object where the application stores at least one reference. For example, Employee emp = new Employee("John", "Doe"); is a referenced object because variable emp contains a reference to the Employee object.

A referenced object becomes unreferenced when the application removes its last stored reference. For example, if emp is a local variable that contains the only reference to an Employee object, this object becomes unreferenced when emp's method terminates.

An application can remove a stored reference by assigning null to its reference variable. For example, emp = null; removes the reference to the Employee object that was previously stored in emp.

Java's garbage collector eliminates a form of memory leakage in C++ implementations that do not rely on a garbage collector. In these implementations, the developer must destroy dynamically created objects before they go out of scope. If they vanish before destruction, they remain in the heap. Eventually, the heap fills and the application halts.

Although this form of memory leakage is not a problem in Java, a related form of leakage is problematic: continually creating objects and forgetting to remove even one reference to each object causes the heap to fill up and the application to eventually come to a halt.

This form of memory leakage is a major problem for applications that run for lengthy periods of time—a web server is one example. For shorter-lived applications, you will normally not notice this form of memory leakage. However, it is a good habit to assign null to reference variables when their referenced objects are no longer required.

EXERCISES

The following exercises are designed to test your understanding of Java's language fundamentals:

1. What does a class declaration contain?

2. Is transient a reserved word? Is delegate a reserved word?

3. What is a variable?

4. Identify Java's only unsigned primitive type.

5. What is the difference between an instance field and a class field?

6. What is an array?

7. How do you declare a one-dimensional array variable? How do you declare a two-dimensional array variable?

8. Define scope.

9. Is string literal "The quick brown fox \jumps\ over the lazy dog." legal or illegal? Why?

10. What is the purpose of the cast operator?

11. Which operator is used to create an object?

12. Can you nest multiline comments?

13. True or false: When declaring a method that takes a variable number of arguments, you must specify the three consecutive periods just after the rightmost parameter's type name.

14. Given a two-dimensional array x, what does x.length return?

15. What is the difference between the while and do-while statements?

16. Initialize the sines and cosines array declarations using the new syntax.

17. Why is it okay for an expression assigned to an instance field to access a class field that is declared after the instance field? In contrast, it is not okay for the expression to access another instance field that is declared after the instance field.

18. What is required to create an array of objects?

19. How do you prevent a field from being shadowed?

20. How do you chain together instance method calls?

21. The factorial() method provides an example of tail recursion, a special case of recursion in which the method's last statement contains a recursive call, which is known as the tail call. Provide another example of tail recursion.

22. Merge the various CheckingAccount code fragments into a complete application.

Was this article helpful?

0 0

Post a comment