Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

Garbage Collection in Java
Previous Home Next

An application running on a system computer uses some memory, which makes memory management a significant issue for any programming language. As Java is comparatively high-level language, the memory management in Java is automatic. To make it more efficient, we need to understand garbage collection, i.e. freeing memory from objects that are no longer in use.

Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. This frees a programmer from having to keep track of when to free allocated memory, thus preventing many potential bugs and problems.

What is a Garbage Collector ?

When we create an object by instantiating a class, the object is put on the heap, that is, it uses some memory. A Java application creates and uses objects. After an object in memory has been used and is no longer needed, it is sensible to free memory from that object.The Garbage Collector in Java initiates the memory management by freeing up the memory from objects that are no longer referenced.

The importance of this is that you do not need to code the memory management into your application program, also you have no control over when the garbage collector runs. While coding in some application we can do following things in order to help the memory management through the garbage collector :

  1. Firstly, make an object eligible for the garbage collection, as garbage collector will only free memory from an eligible object.

  2. Also make a request for garbage collection by making a system call to the garbage collector via, System.gc();.

 

We can also invoke the gc() method by using an instance of the Runtime class that a running program always has. It is contained in java.lang package, we can get hold of this instance by calling the static method getRuntime() of the Runtime class. The following example will show how we can invoke gc() method via Runtime class and some other runtime tasks,

Example


Java Tutorialslass garc
{
   public static void 
   main(String 
   args[])
{
 Runtime runtimeobject = 
 Runtime.getRuntime();  
	 // creating an object of Runtime class
 
 // this statement returns the total memory  available in bytes to jvm
 System.out.println(
 "The value of total memory of jvm is :"
 +runtimeobject.totalMemory());  
 
 // this statement returns the free memory available in bytes to jvm
 System.out.println(
 "The value of free memory availabe to jvm is :"
 +runtimeobject.freeMemory());  
 runtimeobject.gc();   
 // calling gc method on Runtime object
 System.out.println(
 "The amount of memory allocated to jvm after calling gc is :"
 +runtimeobject.freeMemory());
}
}

It should be noted that a call to the garbage collector gives no guarantee that the memory will be free. There can be a situation that the JVM in which your program is running did not even implement the gc() method. The Java language also allows a dummy gc() method, the basic requirement for garbage collection is that you must make your object eligible for garbage collection. An object is considered eligible for garbage collection if there is no reference pointing to it. We can remove the references to an object in two ways:

  1. By setting the object reference variable pointing to the object to null, such as,

    GCclass object = new GCclass();
    object = null;
    
  2. Secondly by reassign a reference variable of an object to another object i.e. if a reference variable object is pointing to an object of the GCclass, you can free this object from this reference by pointing the reference to another object by the following statement

    myObject = new GC2class();
    

    Now, the object reference GCobject is pointing to an object of the class GC2class and not to an object of GCclass, to which it was pointing earlier.

Previous Home Next