Am I creating memory leaks?

C

chris brat

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
 
M

Matt Humphrey

chris brat said:
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?

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 (e-mail address removed) http://www.iviz.com/
 
C

chris brat

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
 
D

Dale King

chris said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top