Re: creating garbage collectable objects (caching objects)



En Mon, 29 Jun 2009 08:01:20 -0300, Dave Angel <davea@xxxxxxxx> escribió:
News123 wrote:

What I was more concerned is a group of output images depending on TWO
or more input images.

Depending on the platform (and the images) I might not be able to
preload all two (or more images)

So, as CPython's garbage collection takes always place immediately,
then I'd like to pursue something else.
I can create a cache, which caches input files as long as python leaves
at least n MB available for the rest of the system.

As I said earlier, I think weakref is probably what you need. A weakref is still a reference from the point of view of the ref-counting, but not from the point of view of the garbage collector. Have you read the help on weakref module? In particular, did you read Pep 0205? http://www.python.org/dev/peps/pep-0205/

You've misunderstood something. A weakref is NOT "a reference from the point of view of the ref-counting", it adds zero to the reference count. When the last "real" reference to some object is lost, the object is destroyed, even if there exist weak references to it. That's the whole point of a weak reference. The garbage collector isn't directly related.

py> from sys import getrefcount as rc
py> class X(object): pass
....
py> x=X()
py> rc(x)
2
py> y=x
py> rc(x)
3
py> import weakref
py> r=weakref.ref(x)
py> r
<weakref at 00BE56C0; to 'X' at 00BE4F30>
py> rc(x)
3
py> del y
py> rc(x)
2
py> del x
py> r
<weakref at 00BE56C0; dead>

(remember that getrefcount -as any function- holds a temporary reference to its argument, so the number it returns is one more than the expected value)

Object cache is one of the two reasons for the weakref module.

....when you don't want the object to stay artificially alive just because it's referenced in the cache. But the OP wants a different behavior, it seems. A standard dictionary where images are removed when they're no more needed (or a memory restriction is fired).

--
Gabriel Genellina

.



Relevant Pages

  • Re: creating garbage collectable objects (caching objects)
    ... or more input images. ... Depending on the platform (and the images) I might not be able to ... weakref is still a reference from the point of view of the ref-counting, but not from the point of view of the garbage collector. ...
    (comp.lang.python)
  • Re: C++ sucks for games
    ... > own garbage collector, but i've done it better than lisp could have. ... have a reference to some level data, and you make that reference go out of ... trivial memory management problem. ... except that you can know exactly when those deallocations ...
    (comp.lang.lisp)
  • Re: C++ sucks for games
    ... > own garbage collector, but i've done it better than lisp could have. ... have a reference to some level data, and you make that reference go out of ... trivial memory management problem. ... except that you can know exactly when those deallocations ...
    (comp.lang.lisp)
  • Re: arrays = pointers?
    ... references, that means that updating a reference is perhaps twice as efficient in .NET as in an environment that uses for example reference counting. ... I never once brought up the question of "reference counting", nor do I find it relevant to the discussion I'm participating in. ... This is WAY faster than .NET running through all of the data structures in the memory manager, finding those that refer to a given object and modifying them. ... It's only because each and every variable "knows" what it's doing that the garbage collector can avoid having to store any additional data. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: WM_COPYDATA
    ... and well-suited for sending images. ... Delphi does not handle objects and pointers much at all. ... Passing an object to a function passes it by reference. ... orthogonal commands. ...
    (comp.lang.pascal.delphi.misc)