• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

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?



0
jerntat
Asked:
jerntat
  • 3
  • 2
1 Solution
 
objectsCommented:
Shouldn't be.
0
 
jerntatAuthor Commented:
so the next gc should remove the Hastable object and the whole list of Car objects store in the Hashtable?
0
 
objectsCommented:
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.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LDCCommented:
The Car objects -and Integer objects and Strings- can all be collected by (any decent) gc.
So you don't have a leak.
0
 
jerntatAuthor Commented:
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?
0
 
objectsCommented:
Yes, they will be then available for collection.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now