Link to home
Start Free TrialLog in
Avatar of jerntat
jerntat

asked on

Is there a memory leak

Hello, if I a code that goes something like this:

import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;


public class garbage
{
  private static Runtime checkMem;
     
  public static void main ( String args[] )
  {    
    checkMem = checkMem.getRuntime();
    try
    {
     Hashtable test = new Hashtable(2);
         
     for(int i=0; i< 30000; i++)
     {
      Car b = new Car(123);
      Integer temp = new Integer(i);
      String dumb = temp.toString();
      test.put(dumb, b);
     }          
     test.clear();
    }
    catch(Exception ex)
    {
     System.out.println(ex.toString());
    }
     
}//ends main

Car is just a class I have constructed. I created Hashtable test to store objects of Car.
Will there be a memory leak if i just clear the content of the hashtable test using the clear() method?



Avatar of Mick Barry
Mick Barry
Flag of Australia image

Shouldn't be.
Avatar of jerntat
jerntat

ASKER

so the next gc should remove the Hastable object and the whole list of Car objects store in the Hashtable?
I don't think there is a guarantee of that.
Certainly if the jvm was short of memory then the memory used by the Car objects will be freed, but the Hastable object itself will remain as there is still reference to it.

The Car objects -and Integer objects and Strings- can all be collected by (any decent) gc.
So you don't have a leak.
Avatar of jerntat

ASKER

What is i just set the Hashtable test to null :

test = null;

will everything including the objects store in the hashtable and the hashtable itself be gc?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial