473,756 Members | 9,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2184
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*******@hotm ail.com> wrote in message
news:94******** *************** ***@posting.goo gle.com...
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

"Christophe r T King" <sq******@WPI.E DU> 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************ *************@p osting.google.c om>,
Elbert Lev <el*******@hotm ail.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**@pythoncra ft.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
5291
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 is that when an exception is raised, the destruction of locals appears to be deferred to program exit. Am I missing something? Is this behaviour by design? If so, is there any reason for it? The only rationale I can think of is to speed up...
7
3117
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 pointers in the vector are pointing to. It works fine, IF I comment out the destructors in Base and Inherited. If I leave the explicit destructors in there, it seg faults. Why is there a difference?
1
3252
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 collector, such as the one used in Java, maintains a record of whether or not an object is currentlys being used. An unused object is tagged as garbage,
26
2721
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 (and rarely). This always happens during process shutdown, after some thread has called System.Environment.Exit(). Clearly, some sort of race condition. Note that what follows only applies to destructors that are called when the process shuts...
9
1330
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 'destroyed': ref myClass { public: myClass() {} ~myClass() {} // never called !myClass() {} // never called
4
4196
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) { } double &operator()(int i) { return myX; }
6
7530
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
2278
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
10
3651
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 (if not all) C++ compilers for the MSW use a very similar vtable layout. In COM, this is handled by proxies provided by system dlls,
0
9117
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9894
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9541
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8542
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7078
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4955
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3141
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2508
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.