Solved

Is there a memory leak

Posted on 2001-09-13
6
337 Views
Last Modified: 2012-05-04
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
Comment
Question by:jerntat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6478800
Shouldn't be.
0
 

Author Comment

by:jerntat
ID: 6478816
so the next gc should remove the Hastable object and the whole list of Car objects store in the Hashtable?
0
 
LVL 92

Expert Comment

by:objects
ID: 6478838
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!

 
LVL 1

Expert Comment

by:LDC
ID: 6479172
The Car objects -and Integer objects and Strings- can all be collected by (any decent) gc.
So you don't have a leak.
0
 

Author Comment

by:jerntat
ID: 6481168
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
 
LVL 92

Accepted Solution

by:
objects earned 20 total points
ID: 6481181
Yes, they will be then available for collection.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse apache tomcat admin console 52 153
Java: The Public Class Main 4 45
How to configure empty element in XML Document parser? 15 44
Java array 21 86
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question