Results 1 to 7 of 7

Thread: How to Force Garbage Collection In Java?

  1. #1
    Join Date
    Nov 2009
    Posts
    877

    How to Force Garbage Collection In Java?

    Hello, I am learning java programming and while learning it, I have got one problem as How to Force Garbage Collection In Java? and now I am not able to get solution for it. If you are having any method or program which can solve my problem then please provide that to me. It will be helpful for me. So, please reply me as soon as possible.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to Force Garbage Collection In Java?

    Hello, you can simply make use of the code below for your forcefully garbage collection :
    Code:
    public void GarbageCollection  () 
    {
    this.threatId = null ;
    this.elPosition = null ;
    this.kinematics = null ;
    if (this.iff != null)
    {
    this.iff.nullify();
    this.iff = null ;
    }
    }

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to Force Garbage Collection In Java?

    Hello, as far as I am having knowledge about the java, it is necessary to make use of the code below for forcing garbage collection in java. It will be helpful to you.

    Code:
    Runtime run = Runtime.getRuntime();
    run.gc();

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to Force Garbage Collection In Java?

    Hello, I am not having more knowledge about it, but as I know about java, I don't think that it is possible to forcefully make the garbage collection by making call to the the method of the System.gc(). It is known as the hint to the runtime engine that can run the Garbage collection. It depends on the Java virtual machine to call the garbage collection when it want. So, it may be possible that if you call the System.gc(), it wont call to the garbage collection at that time.

  5. #5
    Join Date
    May 2008
    Posts
    2,012

    Re: How to Force Garbage Collection In Java?

    Hello, if you make use of the main method below in your class then you can able to force the garbage collection in java:

    Code:
    public static void main (String[] args) 
    {
          testing test = new testing();
          Runtime rtime = Runtime.getRuntime();
          long available = rtime.availableory();
          System.out.println("free memory before creating array: " + available);
          test.used();
          available = rtime.availableory();
          System.out.println("free memory after creating array:  " + available);
          rtime.gc();
          available = rtime.availableory();
          System.out.println("free memory after running gc():    " + available);
    }

  6. #6
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to Force Garbage Collection In Java?

    Hello, I have the code below which can Force Garbage Collection In Java and get your problem solved for searching for the code:
    Code:
    public class GarbageCollection 
    {
       int size = 1000000;
       void useMemory() 
    {
          int[] intA = new int[size];
          for (int i=0; i<size; i++) 
    {
            intA[i] = i*2;
          }
       }
       public static void main (String[] args) 
    {
          GarbageCollection GarbageC = new GarbageCollection();
          Runtime rtime = Runtime.getRuntime();
          rtime.gc();
          long present = rtime.freeMemory();
          System.out.println("At program start we have : " + present + " bytes");
          GarbageC.useMemory();
          long present1 = rtime.freeMemory();
          System.out.println("After running the program, we have :  " + present1 + " bytes");
          rtime.gc();
          long present2 = rtime.freeMemory();
          System.out.println("After collecting garbage we have :    " + present2 + " bytes");
          long freedMem = present2 - present1;
          System.out.println("Garbage collection freed :    " + freedMem + " bytes");
       }
    }

  7. #7
    Join Date
    Dec 2009
    Posts
    7

    Re: How to Force Garbage Collection In Java?

    To force garbage collection in java can be done by using 2 classess.
    1. System
    2. Runtime

    1. System:
    * System is a class available under the package java.lang.
    * System class consists of static members.
    * One of the static member in System class is gc() method used to perform
    the garbage collection manually.

    Code: System.gc();

    2.Runtime:
    * Runtime class is also available under the package java.lang.
    * Runtime class contains gc() non static method.
    * To call up the non static method, we need to create an object.. But for
    Runtime class u cannot create an object becoz Runtime class has
    private constructor.
    * For that reason, u r assigning the information of the Runtime object
    through the static member(getruntime()) of the Runtime class.

    code:
    Runtime rt=Runtime.getRuntime();
    rt.gc();

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    illegal code:
    Runtime rt=new Runtime();// not possible becoz, has private constructors


    Information about the Runtime Class:

    public class Runtime extends Object

    Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.

    An application cannot create its own instance of this class.

    Information about getRuntime() static member :

    public static Runtime getRuntime()

    Returns the runtime object associated with the current Java application. Most of the methods of class Runtime are instance methods and must be invoked with respect to the current runtime object.

    Returns:
    the Runtime object associated with the current Java application.



    Last edited by javainfo; 28-04-2011 at 01:33 PM.

Similar Threads

  1. JAVA - What is garbage collection
    By Bhim in forum Software Development
    Replies: 5
    Last Post: 28-04-2011, 01:13 PM
  2. Does C Sharp provide automatic garbage collection ?
    By kALAMATHI in forum Software Development
    Replies: 3
    Last Post: 08-01-2011, 08:33 AM
  3. Java - help with Garbage Collection
    By Messenger in forum Software Development
    Replies: 4
    Last Post: 23-07-2010, 10:18 AM
  4. What is the Garbage Collection in PHP?
    By Emma.J in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 01:40 AM
  5. Delta Force 10th Anniversary Collection
    By Wouter in forum Video Games
    Replies: 3
    Last Post: 24-06-2009, 06:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,255,629.66180 seconds with 17 queries