• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A canonicalizing WeakHashMap

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What a canonicalizing container is?
Maybe one that stores unique objects. Unique objects in the sense that their equivalence is only established via ==, not based on the state?
In the API for WeakHashMap :
" This class is intended primarily for use with key objects whose equals methods test for object identity using the == operator. Once such a key is discarded it can never be recreated, so it is impossible to do a lookup of that key in a WeakHashMap at some later time and be surprised that its entry has been removed. This class will work perfectly well with key objects whose equals methods are not based upon object identity, such as String instances. With such recreatable key objects, however, the automatic removal of WeakHashMap entries whose keys have been discarded may prove to be confusing."
What sort of confussion is this?
Only that the programmer may be expecting that a recreatable object could be still in the map after having been discarded and recreated?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what a "canonicalizing container" is - where did you encounter the term? I don't find it anywhere in the API you ask about.
> What sort of confussion is this?
> Only that the programmer may be expecting that a recreatable
> object could be still in the map after having been discarded
> and recreated?
I think so. Consider the following:
<pre>
Map map = new WeakHashMap();
map.put(new Integer(1), new Integer(10));
System.gc();
Object value = map.get(new Integer(1));
System.out.println(value);
</pre>
Some people might expect that the 1->10 mapping should still be in the map after gc, not realizing how a WeakHashMap works. The problem should be obvious with an explicit call to gc such as I make above - but even without this, gc could occur at any point in between the put() and the get(), so people might not understand why their mappings sometimes vanish for no apparent reason. Of course, such people should not be using a WeakHashMap anyway I supppose - or they should make sure they hold on to copies of their keys somewhere, rather than simply re-creating them.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim thanks for answering

This is from the chapter 9 of Thinking in Java by Bruce Eckel.

"The containers library has a special Map to hold weak references: the WeakHashMap. This class is designed to make the creation of canonicalized mappings easier. In such a mapping, you are saving storage by making only one instance of a particular value. When the program needs that value, it looks up the existing object in the mapping and uses that (rather than creating one from scratch). The mapping may make the values as part of its initialization, but it?s more likely that the values are made on demand.
Since this is a storage-saving technique, it?s very convenient that the WeakHashMap allows the garbage collector to automatically clean up the keys and values. You don?t have to do anything special to the keys and values you want to place in the WeakHashMap; these are automatically wrapped in WeakReferences by the map.
"
I still don't understand two points:
Why it would be needed to create several copies of an object? I mean more than one copy.
is Bruce referring really to the value stored in a WeakHashMap? why not using a simple WeakReference object?
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella:
"[...] With such recreatable key objects, however, the automatic removal of WeakHashMap entries whose keys have been discarded may prove to be confusing."
What sort of confussion is this?


In a WeakHashMap, a key and its associated value may be removed as soon as the application no longer holds any hard references to the key. Now assume the key is, say, a recreatable one such as an Integer, and I store a value: weakMap.put(key, value).
Because the Integer key is recreatable, a developer unaware of the WeakHashMap behind the scenes may well use an intermediate integer primitive at some stage, or serialize it, or send it over RMI or do something else that will cause my original Integer object to get lost. At that point, although I still hold the key to my value (in the sense that newKey.equals(key)), the WeakHashMap will sooner or later remove the entry.
That is why a WeakHashMap may give unexpected results for recreatable keys.
- Peter
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella:
Why it would be needed to create several copies of an object? I mean more than one copy.

It often isn't - which is exactly why a WeakHashMap can be useful. You might think, why can't you realise the association with an instance field on the key object? Well, that's not always desirable or possible - the objects may come from a library, for instance.

is Bruce referring really to the value stored in a WeakHashMap? why not using a simple WeakReference object?

A WeakReference to what? To the key? - That's an entirely different behaviour, with a WeakHashMap the key is subject to the normal garbage collection rules. To the value? - that would allow the value to be garbage collected at any time, while with a WeakHashMap it can be garbage collected only if you lost all your references to the key.
- Peter
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to Peter


You might think, why can't you realise the association with an instance field on the key object? Well, that's not always desirable or possible - the objects may come from a library, for instance.


Sorry, but what kind of library are you meaning? a native one? or some class downloaded from the network for which we should use reflective methods? I think the last one could be assigned to an Objet type variable. Couldn't it?


A WeakReference to what? To the key? - That's an entirely different behaviour, with a WeakHashMap the key is subject to the normal garbage collection rules. To the value? - that would allow the value to be garbage collected at any time, while with a WeakHashMap it can be garbage collected only if you lost all your references to the key.



If we are not holding map-outside references to the values: the diference between using a WeakReference to hold a reference to the object, instead of using a WeakHashMap that holds the object as the value, would be the shifting, from the value to the key, of the resposability from being the object to which the strong references have to point . Is this of importance or useful?
But rereading the parragraph by Bruce, all has to do with memory savings. I think that for the matter, a WeakReference array does the work also like a WeakHasMap; unless the point in using the last one would be a kind of "unique relation" between the key and the value. Again what this would be used for?
Thank for your attention
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic