Closed GoogleCodeExporter closed 9 years ago
It's not clear why softKeys is appropriate for this use case at all.
Generally speaking, using weak or soft references indicates that GC'ing an
element is a significant event for that key -- which more or less means that it
has to use reference equality, or else why would it matter?
It sounds like you just want to use softKeys to respond to memory pressure,
which can cause significantly more trouble than it is worth -- soft references
can add significant overhead. I believe we recommend using maximumSize instead
for these cases.
Original comment by lowas...@google.com
on 20 Oct 2014 at 8:20
Original comment by kevinb@google.com
on 20 Oct 2014 at 8:24
I use a lot of caches and most of the time, I have no way to compute a
meaningful value for "maximumSize". Yes I have to respond to memory pressure
but not too aggressively as some objects are expensive to retrieve. Soft
references are a good compromise in my case despite the overhead. Actually, I
only always know a right way of computing a minimal value for "maximumSize" but
then caching becomes totally inefficient and useless.
For example, imagine that the keys and the values are in a table in a scroll
pane. I can approximately guess that I won't show more than 100 lines at a
time, I can set maximumSize to at least 100 but I have no way of computing a
better (greater but not too big) fixed value based on the memory available for
the JVM if I have no clue about the memory required for a single line and the
memory available for the JVM evolves. That's why setting maximumSize is enough
for more simple cases but not in mine. When a key is unused for a few minutes,
it is garbage collected only if there is some memory pressure. It helps me to
keep the applications much more responsive when an end user goes back and
forth, scrolls up and down.
Original comment by goues...@gmail.com
on 21 Oct 2014 at 1:53
What does softKeys get you in this situation that softValues does not?
Original comment by lowas...@google.com
on 21 Oct 2014 at 3:30
I need both soft keys and soft values because the objects that I store into the
"soft map" are both heavy, the keys and the values. Using only soft values is
better than nothing, it's better than using a plain WeakHashMap because the
values are garbage collected only on memory pressure which is what I want. The
soft keys allow me to have the same benefit for the keys.
Original comment by goues...@gmail.com
on 21 Oct 2014 at 5:49
IIRC, I'm not actually sure that soft references prioritize GC of bigger
objects, or anything: they just start getting GC'd in the presence of memory
pressure at all. So if you start experiencing GC pressure and you're using
softValues, then your values will start getting GC'd, and your keys will start
getting evicted (and made eligible for GC) when cache cleanup occurs.
Original comment by lowas...@google.com
on 21 Oct 2014 at 6:47
I meant that using soft references for both the keys and the values isn't
stupid in most of my cases because both don't have a negligible memory
footprint. Do you suggest to use strong keys with soft values?
Original comment by goues...@gmail.com
on 21 Oct 2014 at 8:45
That's exactly what I'm saying, and I'm arguing that that gets you essentially
the behavior you're looking for.
Original comment by lowas...@google.com
on 21 Oct 2014 at 8:47
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<issue id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:08
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:07
Original issue reported on code.google.com by
goues...@gmail.com
on 5 Sep 2014 at 2:07Attachments: