473,240 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

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 2150
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: David Turner | last post by:
Hi all I noticed something interesting while testing some RAII concepts ported from C++ in Python. I haven't managed to find any information about it on the web, hence this post. The problem...
7
by: joevandyk | last post by:
Below, I have a class Container that contains a vector. The vector contains pointers to a Base class. When the Container destructor is called, I would like to delete all the objects that the...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
26
by: Michi Henning | last post by:
I've been having problem with destructors in the context of having ported C# code developed under .NET to Mono. What happens is that, on a dual-CPU machine, various parts of the code crash randomly...
9
by: Peter Oliphant | last post by:
I would assume in the following that 'instance' being set to 'nullptr' should cause the instance of myClass that was created to no longer have any pointers pointing to it, and therefore be...
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
6
by: mlw | last post by:
Could someone explain why there is no destructor in Java classes? There are many times you need to be called WHEN an object goes out of scope and not when it will eventally be freed.
1
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
10
by: Ole Nielsby | last post by:
James Kanze <james.kanze@gmail.comwrote: COM does rely on vtable layout. COM interfaces are declared as pure virtual classes, all methods using stdcall convention, and this works because most...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.