garbage collection in java

garbage collection


 

Ads

Tutorials   
Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion in java
Ads

Garbage Collection in Java

In java, objects are dynamically allocated using new() keyword. But the key question is what happened to these objects when they have no use. How these objects are destroyed for later reallocation of their memory. In java this is done automatically through garbage collection. In other words it deallocate memory automatically which have no longer use. This technique is known as Garbage Collection.

In this way, java ensures that sufficient memory will available to your program. It (Garbage Collection) is controlled by the configuration parameters of the heap.

Note: Heap resides in the physical memory which is incorporated by the Java Virtual Machine(JVM) for the creation of  objects. The configuration parameter of the heap are Xms & Xmx. You can use these configuration parameter as :

java ?Xms:256m Xmx:1g

It points that the Xms(minimum heap size) is 256Mb & Xmx(Maximum heap size) is !024Mb.

EXAMPLE

In this section, you will learn how to force a garbage collector to work by creating many objects.

The following code of program will help you in forcing the garbage collection. First of all we have created an object for the garbage collector to perform some operation. Then we have used the System.gc(); method to force the garbage collection on that object. Then we have used the System.currentTimeMillis(); method to show the time take by the garbage collector.

import java.util.Vector;

public class GarbageCollector{
public static void main(String[] args) {
int SIZE = 200;
StringBuffer s;
for (int i = 0; i < SIZE; i++) {
}
System.out.println("Garbage Collection started explicitly.");
long time = System.currentTimeMillis();
System.gc();
System.out.println("It took " + (System.currentTimeMillis()-time) + " ms");
}
}

Output

C:\unique>javac GarbageCollector.java                  

C:\unique>java GarbageCollector
Garbage Collection started explicitly.
It took 782 ms

 

Advertisements

Ads
Share on Google+Share on Google+

garbage collection in java

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Nulling variables and garbage collection,garbage collection java,javatutorial,java tutorials  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads