I have read that one should not have to worry about garbage collection
in modern versions of Python - it 'just works'.
Frank Millman wrote at 2024-1-15 15:51 +0200:
I have read that one should not have to worry about garbage collection
in modern versions of Python - it 'just works'.
There are still some isolated cases when not all objects
in an unreachable cycle are destroyed
(see e.g. step 2 of "https://devguide.python.org/internals/garbage-collector/index.html#destroying-unreachable-objects").
I don't want to rely on that. My app is a long-running server, with
multiple clients logging on, doing stuff, and logging off. They can
create many objects, some of them long-lasting. I want to be sure that
all objects created are gc'd when the session ends.
Also be warned that some modules (particularly if they're based on libraries not written in Python) might not garbage collect, so you may need to use other methods of cleaning up after those objects.
Got any examples of that?
I wrote:of scope, the Python object was automatically garbage collected, but the pixbuf data leaked.
Also be warned that some modules (particularly if they're based on libraries not written in Python) might not garbage collect, so you may need to use other methods of cleaning up after those objects.
Chris Angelico writes:
Got any examples of that?
The big one for me was gdk-pixbuf, part of GTK. When you do something like gtk.gdk.pixbuf_new_from_file(), there's a Python object that gets created, but there's also the underlying C code that allocates memory for the pixbuf. When the object went out
Calling gc.collect() caused the pixbuf data to be garbage collected too.hurt anything. I use pixbufs in a tiled map application, so there are a lot of small pixbufs being repeatedly read and then deallocated.
There used to be a post explaining this on the pygtk mailing list: the link was
http://www.daa.com.au/pipermail/pygtk/2003-December/006499.html
but that page is gone now and I can't seem to find any other archives of that list (it's not on archive.org either). And this was from GTK2; I never checked whether the extra gc.collect() is still necessary in GTK3, but I figure leaving it in doesn't
...Akkana
It's very hard to be sure there are no memory leaks in a system with gc.
On 16 Jan 2024, at 03:49, Thomas Passin via Python-list <python-list@python.org> wrote:
This kind of thing can happen with PyQt, also. There are ways to minimize it but I don't know if you can ever be sure all Qt C++ objects will get deleted. It depends on the type of object and the circumstances.
Hi all
I have read that one should not have to worry about garbage collection
in modern versions of Python - it 'just works'.
I don't want to rely on that. My app is a long-running server, with
multiple clients logging on, doing stuff, and logging off. They can
create many objects, some of them long-lasting. I want to be sure that
all objects created are gc'd when the session ends.
On 16 Jan 2024, at 03:49, Thomas Passin via Python-list <python-list@python.org> wrote:
This kind of thing can happen with PyQt, also. There are ways to minimize it but I don't know if you can ever be sure all Qt C++ objects will get deleted. It depends on the type of object and the circumstances.
When this has been seen in the past it has been promptly fixed by the maintainer.
Where do you tend to "leave a reference dangling somewhere"? How is
this occurring? Is it a result of an incomplete transaction (like an
HTTP request that never finishes), or a regular part of the operation
of the server?
On 16 Jan 2024, at 13:17, Thomas Passin via Python-list <python-list@python.org> wrote:
The usual advice is to call deleteLater() on objects derived from PyQt classes. I don't know enough about PyQt to know if this takes care of all dangling reference problems, though.
On 16 Jan 2024, at 12:10, Frank Millman via Python-list <python-list@python.org> wrote:
My problem is that my app is quite complex, and it is easy to leave a reference dangling somewhere which prevents an object from being gc'd.
I sometimes need to keep a reference from a
transient object to a more permanent structure in my app. To save myself
the extra step of removing all these references when the transient
object is deleted, I make them weak references.
class Form:
def __init__(self):
self.elements = []
class Element:
def __init__(self, form):
self.form = form
form.elements.append(self)
On 17/01/24 1:01 am, Frank Millman wrote:
I sometimes need to keep a reference from a transient object to a more
permanent structure in my app. To save myself the extra step of
removing all these references when the transient object is deleted, I
make them weak references.
I don't see how weak references help here at all. If the transient
object goes away, all references from it to the permanent objects also
go away.
A weak reference would only be of use if the reference went the other
way, i.e. from the permanent object to the transient object.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 453 |
Nodes: | 16 (2 / 14) |
Uptime: | 19:27:09 |
Calls: | 9,280 |
Files: | 13,515 |
Messages: | 6,071,901 |