Solved

Is there a memory leak

Posted on 2001-09-13
6
319 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
  • 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now