ANN.: Beta 1.0 of Weak Reference Extension Module is now avai lable

Delaney, Timothy tdelaney at avaya.com
Mon Nov 20 23:16:57 EST 2000


This is one of the major advantages of weak references ...

You grab your data, and store it in the cache. Next time you need it, you go
look in the cache. If it's there, use it. If it's not, then the weak
reference was garbage collected because we were running out of memory, and
you then go and get it from wherever again.

Weak references can give you a qucik-and-dirty cache which will *not* grow
forever, but will occasionally chuck out some data, forcing you to go get it
again. It's a very useful technique.

A good example of where weak references are used now is in Java images. In
earlier versions of Java, a Hashtable was used to store references to every
image loaded, in order to be able to quickly redisplay it.

Unfortunately, this meant the memory requirements of an application could
grow enormously. So instead the implementation now used weak references in
the Hashtable (actually, is uses a WeakHashMap). This means that *most* of
the time the image will already be in memory, but sometimes it may need to
be reloaded from disk.

Weak references should nearly always be hidden behind a facade which
*always* gives you the data it needs, and behind the scenes it takes care of
whether that data is in memory already or not. It's basically lazy
evaluation, where the evaluation *may* need to occur multiple times.

Tim Delaney
Avaya Australia
+61 2 9532 9079

> -----Original Message-----
> From: Johann Hibschman [mailto:johann at physics.berkeley.edu]
> Sent: Friday, 17 November 2000 12:07 AM
> To: python-list at python.org
> Subject: Re: ANN.: Beta 1.0 of Weak Reference Extension Module is now
> available
> 
> 
> Courageous  writes:
> 
> >>> Looks neat!  But maybe you could answer me a quick 
> question, since I
> >>> really don't understand weak references.
> 
> > Suppose you are designing a cache.
> 
> It's still not obvious to me.  Say I had a database that I was
> caching.  Then
> 
>   x = database.get('RECORD_ID')
> 
> would only get the file from disk once, etc.  But the cache itself is
> invisible; it's entirely under the hood.  Once you have the data
> object, you have it.
> 
> Are you saying that once you have the object, something weird might
> happen elsewhere that means you no longer have it?  That seems like a
> sign that you don't really have the object at all, or that you don't
> have synchronization working quite right.
> 
> -- 
> Johann Hibschman                           johann at physics.berkeley.edu
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list