---------- Forwarded message ----------
From: iulian dragos <
jagu...@gmail.com>
Date: Tue, Sep 25, 2012 at 2:16 PM
Subject: Re: A report on leaks in reflection
To: Paolo Giarrusso <
p.gia...@gmail.com>
Cc: Eugene Burmako <
eugene....@epfl.ch>
Hi Paolo,
On Mon, Sep 24, 2012 at 1:10 PM, Paolo Giarrusso <
p.gia...@gmail.com> wrote:
>
> On Mon, Sep 24, 2012 at 10:07 AM, Eugene Burmako <
eugene....@epfl.ch> wrote:
> > Isn't WeakHashSet leaking? It looks like weak references will remain there
> > forever, even though their contents will be collected.
I didn't see the previous message, so I don't know if there is any
ongoing discussion on our implementation of WeakHashSet.
>
>
> I believe you're right, so it seems WeakHashSet does _not_ implement
> the intended specification - the one suggested by its name. It should
> get fixed, right? Isn't then WeakHashSet a (quite small) disaster
> waiting to happen, since it's used (see
> 568546ef3f1073febc9bfc6baf63eceaf92213b6)? It's used in a perRunCache,
> but the commit strongly suggests that at least when it was introduced
> the cache was not cleaned often enough, so a working weak hash set is
> still required and there is probably a (smaller) memory leak (maybe
> restricted to the presentation compiler).
AFAIK it leaks entries in the backing hashset, but they are indeed
much smaller the original leak. PerRunCaches wasn't working well, I
believe it does what it says it does, but we still needed the weak set
or otherwise a single run would need too much memory.
>
> Iulian, since you wrote the code, could you confirm or deny our suspects?
>
> For the existing use case (but not for Eugene's one, I fear) the
> safest implementation for WeakHashSet would be to wrap this Java code
> [1]:
>
> Set<T> weakHashSet = Collections.newSetFromMap(
>         new WeakHashMap<T, Boolean>());
>
> [1] 
http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#newSetFromMap%28java.util.Map%29
>
I totally agree. This is the correct way to do it, I should have read
up on it before rolling my own.
>
> For Eugene's use case, I guess that at this point in the release cycle
> your original idea is the safest - this isn't the right time to write
> a new implementation for efficiency (if efficiency is needed at all),
> so I retract my original suggestion.
> WeakHashMap[T, WeakReference[T]] sounds fine for your case. Be careful
> not to expose it as a too general utility: if the key and the value
> pointed to different values, entries and keys would be leaked when
> values are collected; if that were a problem for you (it shouldn't
> be), you might want to look into Guava's or Apache Commons maps
> (
http://stackoverflow.com/a/264591/53974).
Not sure what's the use case at hand, but make sure you can recover
when the key is in the cache, but it's value has been collected.
iulian
>
>
> Best,
> Paolo
>
> > On 24 September 2012 02:23, Paolo G. Giarrusso <
p.gia...@gmail.com>
> > wrote:
> >>
> >>
> >>
> >> Il giorno domenica 23 settembre 2012 19:05:27 UTC+2, Eugene Burmako ha
> >> scritto:
> >>>
> >>> 2) What standard data structure would be the best for implementing
> >>> `uniques`? Currently all I can think of is WeakHashMap[Type,
> >>> WeakReference[Type]]. I need the data structure to support a single
> >>> operation: findEntryOrUpdate(x: T), which finds and returns entry by
> >>> x.##, or updates the map with x and returns x.
> >>
> >> You seem to be describing WeakHashSet, which according to Google has been
> >> written many times (in Java&Scala).
> >> I'd add findEntryOrUpdate to
> >> src/reflect/scala/reflect/internal/util/WeakHashSet.scala. Probably you'd
> >> need the same method in FlatHashTable (without exposing it to the world,
> >> unlike existing methods which are part of the public API of HashSet even if
> >> they shouldn't); it seems you could mix addEntry and containsEntry,
> >> following src/reflect/scala/reflect/internal/util/HashSet.scala as a
> >> template.
> >>
> >> Best,
> >> Paolo
> >
> >
>
>
>
> --
> Paolo G. Giarrusso - Ph.D. Student, Philipps-University Marburg
> 
http://www.informatik.uni-marburg.de/~pgiarrusso/
--
« Je déteste la montagne, ça cache le paysage »
Alphonse Allais