Trainers' thoughts on Java and related technologies

In C/C++ memory management is handled by the programmer. Even after a programmer has put in a lot of effort to ensure the code is free of memory management problems, the more likely it is that they are likely to exist. The risk of these bugs increases with code size and complexity.

On the other hand, the Java programming language and runtime environments go a long way to eliminate these problems. Here’s how:

  • In Java, memory is allocated only to objects. There is no explicit allocation of memory, only the creation of new objects.
  • The Java Virtual Machine (JVM) employs a garbage collector that reclaims the memory occupied by an object once it determines that it is no longer accessible. This automatic process makes it safe to throw away unneeded object references because the garbage collector does not collect the object if it is still needed elsewhere.
  • Java makes it easy to let go of an entire “tree” of objects by setting the reference to the tree’s root to null; the garbage collector will then reclaim all the objects (unless some of the objects are needed elsewhere).

So what about memory leaks caused by poor program design? Unfortunately, this can still be a problem.

In these programs unnecessary object references linger in long-lived parts of the application and prevent the garbage collector from reclaiming objects that are no longer needed. Most often, properly encapsulating object references in various parts of the code solves the problem.

Another design flaw occurs at the algorithm level. Not closing JDBC connections properly when using connection pools, threads left running after they have finished their work, and objects retaining their reference to legacy objects are typical examples. The use of Collection objects will magnify this problem.

Programmers should design applications with an object’s lifecycle in mind, rather than only rely on the features of a JVM implementation. Memory problems can be mitigated by letting go of object references to legacy classes as soon as possible. The KL Group’s JProbe tool can help identify possible sources of memory leaks by showing which objects are holding on to references passed their prime.

Interested in more? Check out our upcoming Java training classes!

No TweetBacks yet. (Be the first to Tweet this post)

2 Responses to “Java Memory Management”

  1. Would be nice to see some examples of the code that demonstrates a memory leak with possible ways to counter it.

  2. One of the tools available for analyzing garbage collection is IBM’s Diagnostic Tool designed for the IBM Java™ Virtual Machine.

Tweetbacks

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

© Webucator, Inc. All rights reserved. | Toll Free: 877-932-8228 | UK: 0808-101-3484 | From outside the USA: 315-849-2724 | Fax: 315-849-2723