[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Is there a memory leak

Posted on 2001-09-13
6
Medium Priority
?
359 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 60 total points
ID: 6481181
Yes, they will be then available for collection.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

649 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