Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #6308

Re: HashSet keeps all nonidentical equal objects in memory

From markspace <-@.>
Newsgroups comp.lang.java.programmer
Subject Re: HashSet keeps all nonidentical equal objects in memory
Date 2011-07-20 08:22 -0700
Organization A noiseless patient Spider
Message-ID <j06rr5$iu0$1@dont-email.me> (permalink)
References <2f8556b7-4d08-4adb-a455-7997fcff0829@m10g2000yqd.googlegroups.com> <c6149318-c1f3-46f4-907e-2b126cc82361@a10g2000yqn.googlegroups.com>

Show all headers | View raw


On 7/20/2011 4:09 AM, Frederik wrote:
> With that, I cannot
> reproduce the problem. So in my other code I must be doing something
> very specific which triggers this problem. I don't know what it is,
> but never mind.


Ayup.  As usual it's in the code we are not shown.  Good job working out 
the answer yourself though.

Hmm, looking at your problem description again, can you verify that when 
the problem occurs, there are much more than 7000 strings in the set? 
I.e., the set is really retaining all of the objects, or is there a 
memory leak somewhere else?

It's possible to make strings such that they hold memory and cannot 
release it.  Are you calling substring() a lot to create the strings? 
If so they could be holding memory from a larger string.  If the 
substrings are equal and not retained, but the large string is held in 
memory because the smaller string will contain a pointer to it, this 
could occur.

If so, try calling "new String( substr )" on the sub-strings to make an 
entirely new object instead of intern().  If this also works, that was 
likely your problem.

Try running the code under a profiler, with memory profiling enabled 
(and the problem verify to occur first), and post the results.



Other ideas that are probably not as workable:

Make sure it's really HashSet and not some derived (and broken) 
implementation.  There are also things called IdentityHashSet out there 
in the wild that will give you the behavior are seeing.


Make sure it's really String and not something else.  I don't know how 
this could happen but it is pretty common when putting user defined 
objects to mess up the hashcode() and equals() methods, which will break 
containers like HashSet and HashMap.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

HashSet keeps all nonidentical equal objects in memory Frederik <landcglobal@gmail.com> - 2011-07-20 02:43 -0700
  Re: HashSet keeps all nonidentical equal objects in memory Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-20 07:30 -0400
  Re: HashSet keeps all nonidentical equal objects in memory Frederik <landcglobal@gmail.com> - 2011-07-20 04:09 -0700
    Re: HashSet keeps all nonidentical equal objects in memory markspace <-@.> - 2011-07-20 08:22 -0700
  Re: HashSet keeps all nonidentical equal objects in memory Robert Klemme <shortcutter@googlemail.com> - 2011-07-20 08:38 -0700
    Re: HashSet keeps all nonidentical equal objects in memory lewbloch <lewbloch@gmail.com> - 2011-07-20 09:31 -0700

csiph-web