Results 1 to 8 of 8
  1. #1
    gabel8 is offline Member
    Join Date
    Jan 2014
    Posts
    27
    Rep Power
    0

    Default HashMap memory leak

    I had a question regarding hashmaps and memory issues. If I have a hashmap such as HashMap<String, MyObject> myMap. I have a method that populates this map and then another method iterates through the map. My question is, will this map be garbage collected when the method finishes and the map is not referenced again? The MyObjects that are stored in the map are referenced throughout the remainder of the program, will that prevent the map from being garbage collected? If so, what would be the proper way to assure that the hashmap is garbage collected? Thanks in advance for the help.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: HashMap memory leak

    As long as there is a hard reference to the objects, the objects will not be garbage collected. The easiest way for them to be GC'd is to
    set the hash map reference to null. If that is the only reference to the hash map then the objects will be reclaimed. Another way is to set
    each hash map entry to null. But any other lingering hard references will prevent the objects from being GC'd. You may also want to read about Soft and Weak references.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: HashMap memory leak

    Unless the MyObjects in question reference the HashMap then you won't have an issue.
    It's references to the Map that count, not things the Map references.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: HashMap memory leak

    Quote Originally Posted by Tolls View Post
    Unless the MyObjects in question reference the HashMap then you won't have an issue.
    Sounds like you are talking about circular references. But I believe the the GC handles that so even if the objects referenced the hashmap it shouldn't be a problem. Or were you talking about something else?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: HashMap memory leak

    In the OP scenario (at least some of) the MyObjects live beyond the life of the Map, so it wouldn't be circular.
    "The MyObjects that are stored in the map are referenced throughout the remainder of the program."

    So I was just pointing out that only in the (unlikely) case that there was a reference back to the Map would the Map not be eligible.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    gabel8 is offline Member
    Join Date
    Jan 2014
    Posts
    27
    Rep Power
    0

    Default Re: HashMap memory leak

    So just to be clear, the map is never referenced again, it is only used for a small method. The MyObjects live throughout the program though. So I should not have to set the map to null, correct? It should be available for garbage collection.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: HashMap memory leak

    Nope:
    Java Code:
    public void someMethod() {
        Map<String, Object> map = new Map<>();
        map.put("foo", someObject);
    }
    At the end of the above the map is eligible for gc.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    gabel8 is offline Member
    Join Date
    Jan 2014
    Posts
    27
    Rep Power
    0

Similar Threads

  1. Memory Leak Issue
    By viren2727in in forum Advanced Java
    Replies: 7
    Last Post: 01-10-2013, 06:44 AM
  2. avoid memory leak
    By Sergey Livanov in forum Advanced Java
    Replies: 10
    Last Post: 12-20-2011, 04:10 PM
  3. Memory leak in swing
    By smeshram in forum AWT / Swing
    Replies: 7
    Last Post: 12-08-2011, 03:52 AM
  4. Replies: 1
    Last Post: 07-18-2011, 04:24 PM
  5. Help I have a memory leak...
    By cdman52 in forum Java Applets
    Replies: 10
    Last Post: 09-28-2009, 10:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •