tags:

views:

194

answers:

5

What keyword or function is used in Java to release memory?

+9  A: 

You don't release memory in Java. it is garbage collected by the JVM.

Objects are created by Java's "new" operator, and memory for new objects is allocated on the heap at run time. Garbage collection is the process of automatically freeing objects that are no longer referenced by the program.

Byron Whitlock
+4  A: 

Java is garbage collected, which means that you don't explicitly release memory. Instead, the JVM automatically frees memory when it is no longer referenced. You explicitly de-reference something by setting any variables you had that were referencing it to null, but that doesn't necessarily guarantee it'll be garbage-collected immediately.

Amber
I wonder how people who first found out about this would react. ("Wait, they can do this... automatically???" `*HEAD EXPLODES*`)
polygenelubricants
My reaction was "Cool!! Now I can concentrate on implementing useful stuff.". (My first major hands-on experience with garbage collection was in the 1980's with CLU.)
Stephen C
A: 

Perhaps you want System.gc()?

trinithis
`System.gc()` is generally a bad idea. It's equivalent to `Runtime.getRuntime().gc()` and runs what I've seen termed a "stop the world" garbage collect, which can be devastating in a threaded program (and not great in a non-threaded one either).
JBirch
Apart from being a REALLY BAD IDEA, `System.gc()` is not analogous to `free` or `delete`. The best answer is "there is no analogue" ...
Stephen C
It may be useful in a very limited number of scenarios to call `System.gc()`, but it is important to remember that it is only a request, which may be ignored by the VM.
Yuval
@JBirch, no, gc() just hints that a garbage collection could be done. There is no guaranteee, and notably the gc does not necessarily stop the world.
Thorbjørn Ravn Andersen
@Thorbjørn: Ah, I guess you are correct in that it's really just a suggestion to the JVM and doesn't guarantee that anything would happen, although the Javadoc notes `When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.`Regarding "stop the world" garbage collection - I'll have to read up about it. I was under the impression that in most instances it did, but I think I've found something to read up on tonight. Thanks for your insight!
JBirch
+1  A: 

It is garbage collected by JVM. So we don't need to explicitly release the memory.

Anand Devaraj
+1  A: 

In java, the JVM, memory is automatically reclaimed when objects are garbage collected. Objects in java become eligible for garbage collection when they are no longer referenced (or not strongly referenced (weakly referenced)). That does not mean there will be no memory leaks in java programs. If you are not careful there can still be memory leaks, as given in classic example:

public class Stack {
   private Object[] elements;
   private int size = 0;

   public Stack(int initialCapacity) {
      this.elements = new Object[initialCapacity];
   }

   public void push(Object e) {
      ensureCapacity();
      elements[size++] = e;
   }

   /**
    * This method leaks memory
    */
   public Object leakyPop() {
      if (size == 0)
         throw new EmptyStackException();
      // here is the leak, This object will never be GC'd because its still
      // referenced in the elements array
      return elements[--size];                               
   }

   /**
    * This has a fix to avoid memory leak
    */
   public Object pop() {
      if (size == 0)
         throw new EmptyStackException();
      Object result = elements[--size];
      // To avoid the leak you need to eliminate the reference here explicitely
      elements[size] = null; 
      return result;
   }

  /**
   * Ensure space for at least one more element, 
   */
   private void ensureCapacity() {
      //...
   }
}
naikus