Memory Management

ven though the Java Virtual Machine performs automatic garbagecollection, you can’t completely forget about memory managementissues. How and when garbage collection is performed isimplementation dependent. Some JVMs will wait until allavailable memory is used up before kicking in the garbage collector,while others will run it incrementally. That is why Runtime.gc() isincluded in the core APIs. Invoking this method will ask the JVMto run the garbage collector. Unfortunately, it is only a hintto the JVM that you would like the garbage collector to run, andthere is no guarantee that it will actually run at all. However,many JVMs will run the garbage collector shortly after this call.

The finalize() method is another tricky spot. You should not thinkof this as the Java equivalent of a destructor. C++ makes guaranteesabout when destructors are executed. Java makes no hard promisesabout when finalize() will be invoked, other than that it will run at some point before the memory used by the object is reused. That is why Runtime.runFinalization() is provided, but, again, no guarantee ismade that finalization will actually run immediately after the call.It is only a hint or request to run all the finalizers of eligibleobjects.

Java 2 provides a greater ability to manage memory in thejava.lang.ref package. Contrary to popular belief, Java programs canleak memory. If you stop using an object, but retain a strong referenceto it, the object will never be garbage-collected. It is common forcertain data structures, like caches, to unintentionally leak memoryin this manner. Java 2 provides Weak References to alleviate thisproblem. If an object is only referenced by weak references, it maybe garbage-collected.

The Java 2 reference classes include a non-instantiable Reference baseclass and three subclasses that define different types of weakreferences: SoftReference, WeakReference, and PhantomReference. AReference is a wrapper around a Java object that does not maintaina strong reference to the object. The garbage collector uses animplementation-dependent algorithm to determine whether or not to garbage-collect a SoftReference, whereas a WeakReference is alwaysreclaimed. A PhantomReference can be associated with a ReferenceQueuewhich allows you to perform post-finalization operations on areference. The following example program gives a brief introductionto these concepts:

// $RCSfile:,v $ $Revision: 1.1 $$Date: 1999/09/09 23:48:05 $ import java.lang.ref.*;public class References {  public static void main(String[] args) {    Object weakObj, phantomObj;    Reference ref;    WeakReference weakRef;    PhantomReference phantomRef;    ReferenceQueue weakQueue, phantomQueue;    weakObj    = new String("Weak Reference");    phantomObj = new String("Phantom Reference");    weakQueue    = new ReferenceQueue();    phantomQueue = new ReferenceQueue();    weakRef    = new WeakReference(weakObj, weakQueue);    phantomRef = new PhantomReference(phantomObj, phantomQueue);    // Print referents to prove they exist.  Phantom referents    // are inaccessible so we should see a null value.    System.out.println("Weak Reference: " + weakRef.get());    System.out.println("Phantom Reference: " + phantomRef.get());    // Clear all strong references    weakObj    = null;    phantomObj = null;    // Invoke garbage collector in hopes that references    // will be queued    System.gc();    // See if the garbage collector has queued the references    System.out.println("Weak Queued: " + weakRef.isEnqueued());    // Try to finalize the phantom references if not already    if(!phantomRef.isEnqueued()) {      System.out.println("Requestion finalization.");      System.runFinalization();    }    System.out.println("Phantom Queued: " + phantomRef.isEnqueued());    // Wait until the weak reference is on the queue and remove it    try {      ref = weakQueue.remove();      // The referent should be null      System.out.println("Weak Reference: " + ref.get());      // Wait until the phantom reference is on the queue and remove it      ref = phantomQueue.remove();      System.out.println("Phantom Reference: " + ref.get());      // We have to clear the phantom referent even though       // get() returns null      ref.clear();    } catch(InterruptedException e) {      e.printStackTrace();      return;    }  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.