Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

memory leaks - Does Python GC deal with reference-cycles like this?

Using objgraph, I found a bunch of objects like this:

InstanceState loop

Will Python's garbage collector deal with cycles like this, or will it leak?

A slightly wider view of the loop:

Wider view of InstanceState loop

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Python's standard reference counting mechanism cannot free cycles, so the structure in your example would leak.

The supplemental garbage collection facility, however, is enabled by default and should be able to free that structure, if none of its components are reachable from the outside anymore and they do not have __del__() methods.

If they do, the garbage collector will not free them because it cannot determine a safe order to run these __del__() methods.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...