Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Am I creating memory leaks?

Thread view: 
chris brat - 08 May 2006 14:35 GMT
Hi

I'm doing something with Sets and Maps that I think is valid code but
I'd like an outside opinion on whether or not I am creating a memory
leak with the temporary HashMap that I am using.

My uncertainty is with the following lines in my example (the last
three of the method) :

result = new HashSet();
result.addAll(myMap.values());
return result;

My example :
~~~~~~~~~~

I have a method that does quite a bit of processing and I use a map to
do lookups for already processed data. The method must return a Set
instance.

private static final Set getSomeDataAndUpdate(String [] mapIds){

  Set result = null;
  // map that is used to simplify lookups
  Map<String, String> myMap = new HashMap();

  ...
  Some JDBC code which will populate entries in the the map
  with a database key (as the map key)
  and a string (as the map value for the key)
  ...

  // by this stage the map is populated - process the
  // supplied String array parameter
  // and update the value in the map if the key exists
  for (String mapId : mapIds){

     if (myMap.containsKey(mapId)){

      // update the data - the text is not important
       myMap.put(mapId,"found you");

     }

  }

  result = new HashSet();              // the result I want
  result.addAll(myMap.values());    // dont want the map keys
  return result;
}

Should I be emptying the HashMap and then setting it to null or will
the garbage collector handle this?

Thanks
Chris
Matt Humphrey - 08 May 2006 15:02 GMT
> Hi
>
[quoted text clipped - 49 lines]
> Should I be emptying the HashMap and then setting it to null or will
> the garbage collector handle this?

The garbage collector handles it all.  When the reference to your HashMap
disappears at the end of the method, it becomes eligible for collection.  As
long as a reference can be reached either directly or by reference to
reference (to reference etc) from some global anchor (static variable,
variable still in scope, etc special rules.) the object remains accessible.
It will only be eligble for collection when it cannot be reached.  All the
keys in your HashMap which do not otherwise have references can be collected
also, and so forth.  The values are retained because they are still
accessible from the HashSet you returned.

Cheers,
Matt Humphrey matth@ivizNOSPAM.com  http://www.iviz.com/
chris brat - 08 May 2006 15:11 GMT
Thanks Matt,

Thats what I thought - but for some reason I started to
doubt what I thought I thought I knew .

... too much coffee maybe?

Chris
Dale King - 08 May 2006 17:35 GMT
> My uncertainty is with the following lines in my example (the last
> three of the method) :
>
> result = new HashSet();
> result.addAll(myMap.values());
> return result;

While Matt answered your question about garbage collection, I just
wanted to point out that the number of items returned in the HashSet can
be less than the number of values in the map. The HashSet only stores
unique values. While the keys are guaranteed to be unique in a map, the
values are not.

This may be what you intend to have happen, but in case you weren't
aware I thought I would point that out.
Signature

 Dale King

chris brat - 09 May 2006 06:54 GMT
Good point.

Thanks.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.