Laophatgia / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

MapMaker with weak keys: keySet() doubles reference counts in map #295

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
There is a bug in implementation MapMaker or CustomConcurrentHashMap. When 
iterate through keySet() on created map with weak keys, references to all 
keys in map are doubled because of caching keySet in 
CustomConcurrentHashMap, so auto-removed entries from map doesn't work.

Test case included (based on assertCleanup in 
test/com/google/common/collect/MapMakerTestSuite.java):

Martin Schayna

Original issue reported on code.google.com by mschayna on 12 Nov 2009 at 1:06

Attachments:

GoogleCodeExporter commented 8 years ago
Very same with CustomConcurrentHashMap.entrySet()

:( no workaround for me, I'm crying...

Martin Schayna

Original comment by mschayna on 12 Nov 2009 at 1:19

GoogleCodeExporter commented 8 years ago
Thank you for reporting this!  I've reproduced this on our side.  We are 
looking into 
why this happens.  It's true that the Map retains a strong reference to its 
keySet, 
but that keySet should not have a reference to anything (except the Map).

Original comment by kevin...@gmail.com on 12 Nov 2009 at 6:56

GoogleCodeExporter commented 8 years ago
Looks like my entire long, detailed response was eaten because I'd submitted 
the 
"Thank you.." response in another tab.

Basically, the VM's determination of when an object is reachable is imperfect.  
With 
your test code, for instance, all I have to do is extract the code that deals 
with 
weakKey into a separate method (so the test method just creates the map, passes 
the 
map to that method, then calls assertCleanup) and it suddenly starts working.

MapMaker is definitely not keeping a strong reference to your key.

Original comment by kevin...@gmail.com on 12 Nov 2009 at 9:18

GoogleCodeExporter commented 8 years ago
OK, thank you for quick response. I was too defensive and relying on my unit 
tests.
Life needs some imperfection :)

Original comment by mschayna on 12 Nov 2009 at 9:32