469,568 Members | 1,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

Destructors and reference counting

Please correct me if I'm wrong.

Python (as I understand) uses reference counting to determine when to
delete the object. As soon as the object goes out of the scope it is
deleted. Python does not use garbage collection (as Java does).

So if the script runs a loop:

for i in range(100):
f = Obj(i)

Every time f = Obj(i) is executed it is preceded by f.__del__() call.

Is the same true for functions like:

def A():
f1 = Obj(1)
f2 = Obj(2)

Are f1 and f2 deleted before returning from A?

If my assumptions are correct, tell me please: can I expect that no
garbage collection will be used in the future and Python will use
reference counting?
Jul 18 '05 #1
6 1987
On 6 Jul 2004, Elbert Lev wrote:
Please correct me if I'm wrong.
Python (as I understand) uses reference counting to determine when to
delete the object. As soon as the object goes out of the scope it is
deleted. Python does not use garbage collection (as Java does).
True, except Python uses garbage collection in the sole case of reference
cycles (which you will likely not encounter).
So if the script runs a loop:

for i in range(100):
f = Obj(i)

Every time f = Obj(i) is executed it is preceded by f.__del__() call.

Is the same true for functions like:

def A():
f1 = Obj(1)
f2 = Obj(2)

Are f1 and f2 deleted before returning from A?
True on all counts.
If my assumptions are correct, tell me please: can I expect that no
garbage collection will be used in the future and Python will use
reference counting?


The short answer is, yes, in CPython, for the immediate future, reference
counting will be used and can be relied upon.

The long answer is, no. It's not guaranteed by the language
specifications, and it's not used in Jython (where Java's GC is used
instead).

For a *very* thorough discussion, see the recent thread entitled "python
has useless destructors?"

Jul 18 '05 #2

"Elbert Lev" <el*******@hotmail.com> wrote in message
news:94**************************@posting.google.c om...
Please correct me if I'm wrong.
Ok. This is a good way to learn...
Python (as I understand) uses reference counting to determine when to
delete the object.
The Python definition intentionally does not specify memory management.
The CPython implementation does use reference counting as its primary
method.
As soon as the object goes out of the scope it is deleted.
When there are no references to an object, it *may* be deleted. In
CPython, when the reference count does reach 0, it is deleted. 'Scope' is
a bit vague in the Python context.
Python does not use garbage collection (as Java does).
CPython uses generational gc to collect unreachable objects whose ref
counts remain above 0 due to reference cycles. Jython uses Java's native
gc for all memory reclamation. For human rather than computer
interpreters, memory management is probably completely different. There
are other computer implementations and compilers that I cannot speak about
in this regard.
So if the script runs a loop:

for i in range(100):
f = Obj(i)

Every time f = Obj(i) is executed it is preceded by f.__del__() call.
No. What happens *in CPython* when a name is bound is that if it was
previously bound to something else, the ref count of that something else is
decreased by 1. If that decrease decreases the ref count of that object to
0, then yes, the object is deleted.
Is the same true for functions like:

def A():
f1 = Obj(1)
f2 = Obj(2)

Are f1 and f2 deleted before returning from A?
When a function returns, local names are unbound from the objects they are
bound to. In CPython, this means the same as above (decrement and delete
if 0).
If my assumptions are correct, tell me please: can I expect that no
garbage collection will be used in the future
As I already said, it already is used in both the main computer
implementations
and Python will use reference counting?


CPython (again, not Python) uses ref counting today because efforts some
years ago to replace it with 'true' gc failed to produce better binaries.
It seems that the episodic gc overhead was, averaged over time, about as
high as the constant overhead of ref counting. As long as running speed is
roughly even, the plus of r.c. predictability will continue to tip the
decision in its favor. I know of no efforts today to change the current
status.

Terry J. Reedy

Jul 18 '05 #3

"Christopher T King" <sq******@WPI.EDU> wrote in message
news:Pi**************************************@ccc4 .wpi.edu...
On 6 Jul 2004, Elbert Lev wrote:
Please correct me if I'm wrong.
Python (as I understand) uses reference counting to determine when to
delete the object. As soon as the object goes out of the scope it is
deleted. Python does not use garbage collection (as Java does).


True, except Python uses garbage collection in the sole case of reference
cycles (which you will likely not encounter).


As I mentioned in my response to the OP, this is all specific to the
CPython implementation and not true of Python the language.
So if the script runs a loop:

for i in range(100):
f = Obj(i)

Every time f = Obj(i) is executed it is preceded by f.__del__() call.

Is the same true for functions like:

def A():
f1 = Obj(1)
f2 = Obj(2)

Are f1 and f2 deleted before returning from A?


True on all counts.


Wrong on both counts, at least in the sense of 'not always true and
misleadingly stated', as I explained in my main response, even for
CPython.

Terry J. Reedy

Jul 18 '05 #4
On Tue, 6 Jul 2004, Terry Reedy wrote:
Wrong on both counts, at least in the sense of 'not always true and
misleadingly stated', as I explained in my main response, even for
CPython.
I wrote, but you didn't quote:
The long answer is, no. It's not guaranteed by the language
specifications, and it's not used in Jython (where Java's GC is used
instead).


Jul 18 '05 #5
Thanks all.

I want to explain why I do not like garbage collection. gc works great
for memory. But one can't expects real resources (files, sockets,
handles etc.) be released by gc. Reference counting is a little bit
slower but predictable.
Jul 18 '05 #6
In article <94*************************@posting.google.com> ,
Elbert Lev <el*******@hotmail.com> wrote:

I want to explain why I do not like garbage collection. gc works great
for memory. But one can't expects real resources (files, sockets,
handles etc.) be released by gc. Reference counting is a little bit
slower but predictable.


That's true; however, reference counting fails in the presence of
cycles, and it can be difficult even for Python experts to determine
what will cause a cycle in Python.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

Barbara Boxer speaks for me:
http://buffaloreport.com/2004/040713....marriage.html
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by David Turner | last post: by
7 posts views Thread by joevandyk | last post: by
1 post views Thread by Tony Johansson | last post: by
26 posts views Thread by Michi Henning | last post: by
9 posts views Thread by Peter Oliphant | last post: by
4 posts views Thread by aaronfude | last post: by
6 posts views Thread by mlw | last post: by
1 post views Thread by oec.deepak | last post: by
10 posts views Thread by Ole Nielsby | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.