Overwriting / reassigning instance / garbage collection?

Ken Godee ken at perfect-image.com
Fri Oct 31 10:22:13 EST 2003


 >
 > > try:
 > >     while 1:
 > >         q1.get(0)
 > > except Queue.Empty:
 > >     pass
[snip]
 > >
 > yes, I understand this.
 > and that's why I'm wondering if de-referencing the instance
 > as below would work and that the old address space would
 > get garabage collected. This would then happen in a fraction
 > of the time that it might take the while statement to empty
 > the queue, if ever.

 >Ah, there's your problem.  You're trying to optimize before you have
 >a working solution *or* actual measurements showing a performance
 >problem.

You're right I don't have the actual measurements yet, but
when it goes live the queue will be receiving possibly 100's+
of events per minute, while my program may just sit there
waiting to send a command anywhere from minutes to hours.
Meanwhile the queue if not limited in size or zero'ed out just prior
to sending a command could grow to thousands+ of items.
Once the command is sent, I'm only interested in filtering out
the responses from the command sent. Multiple responses from the command 
sent return almost instantly, mixed in with other responses from the
system.
Unfortunately the back end of this system doesn't allow one to only 
receive responses from the user who sent it, but receives all responses 
from all events on the system.

I experimented last night, with just limiting the size of the queue
and then just before issuing my command reassigning the instance of
the queue. ie....

q1 = Queue.Queue(200)

blah..
program running, q1 filled
q1.qsize()
200
(top of loop)
wait for command input
...getting ready to run my command
q1 = Queue.Queue(200)
q1.qsize()
0
run command
process q1
loop back

Whichs brings me back to original post....
I was worried about the inner workings of
how python would garbage collect the previous instance
of q1, since the first q1 vs 2nd q1 had dif. reference
addresses.
After monitoring the memory usage with the above
code it would appear python handles it quite well
and reuses the memory it already allocated.
Now, I've never had to do this type of
thing with a class instance before, so I
was trying to find out any problems.









More information about the Python-list mailing list