Bitter Java by Bruce Tate

Bitter Java by Bruce Tate

Author:Bruce Tate [Tate, Bruce]
Language: eng
Format: epub, pdf
Tags: General, Computers, Programming Languages, Java (Computer Program Language), Java, Programming, Software Development & Engineering
ISBN: 9781930110434
Publisher: Manning
Published: 2002-03-01T17:40:44+00:00


Shooting memory leaks

165

Iterate

When you find a problem, fix it and start over. Patching one leak can solve others as well. Many leaks are interrelated—sometimes in surprising ways—

and you’ll find it much easier to fix links one at a time than to try to solve them all with a single pass.

Fixing leaks usually turns out to be relatively easy after you’ve identified a problem. You must make the object unreachable by using one of the techniques described in this chapter. These techniques can make an object unreachable, and thus a candidate for garbage collection:

Fixing Java application memory leaks

When an object with a long life cycle maintains a reference to an object with a short life cycle, we have the potential for a memory leak. These possible fixes remove references to make an object unreachable and eligible for garbage collection.

I

Remove the object reference directly by setting it to another value.

I

Remove an object from a collection.

I

Weaken the references using Java reference objects.

I

Shorten the life cycle of the referent.

I

Shorten the life cycle of the object.

I

Remove the object from the code.

I

Refactor the code.

6.5.5

Protect against the problem for the future

When coding problems occur, you should examine the cause. If the problem is an isolated case of programming error, no further action may be necessary.

These steps will help keep the problem from occurring in the future: I

Add to the test suite to make sure that the new case is covered. This will ensure that if the problem crops up again through poor change control or cut and paste, it will be found.

I

If the problem has occurred in the past, an antipattern should be documented and shared.

I

If the antipattern or solution lends new significant insight, it should be published at some level. Publishing paths can be as focused as emails to peers or presentations in department meetings, and as broad as writing a book or speaking at a conference.

Antipatterns work best when we establish a pattern, solve a problem in a general way, and disseminate the wisdom.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.