473,599 Members | 3,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to "free" an object/var ?

If I create a large array of data or class,
how do I destroy it (when not needed anymore) ?

Assign it to an empty list ?

thanks,
Stef Mientki
Jan 30 '07 #1
16 17923
Stef Mientki wrote:
If I create a large array of data or class,
how do I destroy it (when not needed anymore) ?

Assign it to an empty list ?

thanks,
Stef Mientki
It will be gc'd when you leave the scope or you can call del() to
explicitly get rid of the object if its existence bothers you.

James
Jan 30 '07 #2
James Stroud wrote:
Stef Mientki wrote:
>If I create a large array of data or class,
how do I destroy it (when not needed anymore) ?

Assign it to an empty list ?

thanks,
Stef Mientki

It will be gc'd when you leave the scope or you can call del() to
explicitly get rid of the object if its existence bothers you.

James
thanks James,
indeed, large objects, are sometimes bothering me,
and assigning it to an empty list requires comment ;-)
cheers,
Stef
Jan 31 '07 #3
Stef Mientki <S.************ **@mailbox.kun. nlwrites:
If I create a large array of data or class, how do I destroy it
(when not needed anymore) ?
Python comes with garbage collection, which is enabled by default.
Some time after your code stops needing the object, the garbage
collector will clean it up.

To check whether your Python has garbage collection enabled, you can
use the 'gc' interface module:

Python 2.4.4 (#2, Jan 13 2007, 17:50:26)
[...]
>>import gc
gc.isenabled( )
True
>>help(gc)
This module is only needed if you want to *interact* with the garbage
collector, which you won't unless you want to tune it or turn it off.

--
\ "Immorality : The morality of those who are having a better |
`\ time." -- Henry L. Mencken |
_o__) |
Ben Finney

Jan 31 '07 #4
In article <df************ **************@ news.speedlinq. nl>,
Stef Mientki <S.************ **@mailbox.kun. nlwrote:
>
If I create a large array of data or class, how do I destroy it (when
not needed anymore) ?
You should be aware that releasing memory may not cause the size of your
process to shrink -- many OSes keep memory assigned to an application for
its lifetime.
--
Aahz (aa**@pythoncra ft.com) <* http://www.pythoncraft.com/

"I disrespectfully agree." --SJM
Jan 31 '07 #5
On Tue, 30 Jan 2007 15:48:37 -0800, James Stroud wrote:
Stef Mientki wrote:
>If I create a large array of data or class,
how do I destroy it (when not needed anymore) ?

Assign it to an empty list ?

thanks,
Stef Mientki

It will be gc'd when you leave the scope or you can call del() to
explicitly get rid of the object if its existence bothers you.
That is not quite correct.

big_list = ["data"]*1000000
another_referen ce = big_list
del big_list

At this point, the list of one million "data" strings still exists.

del big_list doesn't delete the list object, it removes the name
"big_list". Then, only if the list has a reference count of zero, Python
will dispose of the object and free the memory (if your OS allows that).
If there are still references to it, like "another_refere nce" above, it
will not be disposed of.

As far as I know there is no way to force the deletion of an object even
if it is in use. This is a Good Thing.
--
Steven D'Aprano

Jan 31 '07 #6
On Jan 31, 6:52 am, Steven D'Aprano <s...@REMOVEME. cybersource.com .au>
wrote:
On Tue, 30 Jan 2007 15:48:37 -0800, James Stroud wrote:
Stef Mientki wrote:
If I create a large array of data or class,
how do I destroy it (when not needed anymore) ?
Assign it to an empty list ?
thanks,
Stef Mientki
It will be gc'd when you leave the scope or you can call del() to
explicitly get rid of the object if its existence bothers you.

That is not quite correct.

big_list = ["data"]*1000000
another_referen ce = big_list
del big_list

At this point, the list of one million "data" strings still exists.

del big_list doesn't delete the list object, it removes the name
"big_list". Then, only if the list has a reference count of zero, Python
will dispose of the object and free the memory (if your OS allows that).
If there are still references to it, like "another_refere nce" above, it
will not be disposed of.

As far as I know there is no way to force the deletion of an object even
if it is in use. This is a Good Thing.

--
Steven D'Aprano
The folowing will make the data available for garbage collection no
matter what references it:
>>l = ["data"] *10
l
['data', 'data', 'data', 'data', 'data', 'data', 'data', 'data',
'data', 'data']
>>l2 = l
l[:] = []
l2
[]
>>>
- Paddy.

Jan 31 '07 #7
Steven D'Aprano wrote:
On Tue, 30 Jan 2007 15:48:37 -0800, James Stroud wrote:

>>Stef Mientki wrote:
>>>If I create a large array of data or class,
how do I destroy it (when not needed anymore) ?
If your data structure has no backlinks, it will go away
as soon as the last reference to it disappears. If your
data structure has backlinks, it will hang around until
garbage collection runs. If your backlinks are
weak references (see "weakref"), the data structure will
be released much sooner.

If you generate structures with backlinks, like
parse trees, use weak references for all links that point
backwards toward the root, and you'll use less memory.

In Python, garbage collection is mostly a backup to
the reference counting system. If your app really
needs periodic GC to run, you're leaking memory.
On servers, this sometimes matters.

John Nagle
Jan 31 '07 #8
On Tue, 30 Jan 2007 23:22:52 -0800, Paddy wrote:
>As far as I know there is no way to force the deletion of an object
even if it is in use. This is a Good Thing.

--
Steven D'Aprano

The folowing will make the data available for garbage collection no
matter what references it:
>>>l = ["data"] *10
l
['data', 'data', 'data', 'data', 'data', 'data', 'data', 'data', 'data',
'data']
>>>l2 = l
l[:] = []
l2
[]
Sort of.

What happens is that both l and l2 are names referring to the same list.
After executing l[:] the *contents* of the list change, from lots of
"data" to nothing. Naturally both l and l2 see the change, because they
both point to the same list. But the original contents of the list still
exist until the garbage collector sees that they are no longer in use,
and then frees them.

You can prove this by doing something like this:

data = "\0"*100000 0 # one (decimal) megabyte of data
L = [data] # put it in a list
L[:] = [] # "free" the contents of the list
assert len(data) == 1000000
# but the megabyte of data still there

Again, you can't force Python to free up memory that is still in use.
If you could, that would be a bug.

--
Steven D'Aprano

Jan 31 '07 #9
On Jan 31, 7:34 am, Steven D'Aprano <s...@REMOVEME. cybersource.com .au>
wrote:
On Tue, 30 Jan 2007 23:22:52 -0800, Paddy wrote:
As far as I know there is no way to force the deletion of an object
even if it is in use. This is a Good Thing.
--
Steven D'Aprano
The folowing will make the data available for garbage collection no
matter what references it:
>>l = ["data"] *10
l
['data', 'data', 'data', 'data', 'data', 'data', 'data', 'data', 'data',
'data']
>>l2 = l
l[:] = []
l2
[]

Sort of.

What happens is that both l and l2 are names referring to the same list.
After executing l[:] the *contents* of the list change, from lots of
"data" to nothing. Naturally both l and l2 see the change, because they
both point to the same list. But the original contents of the list still
exist until the garbage collector sees that they are no longer in use,
and then frees them.

You can prove this by doing something like this:

data = "\0"*100000 0 # one (decimal) megabyte of data
L = [data] # put it in a list
L[:] = [] # "free" the contents of the list
assert len(data) == 1000000
# but the megabyte of data still there

Again, you can't force Python to free up memory that is still in use.
If you could, that would be a bug.

--
Steven D'Aprano
Thanks Stephen for explaining my answer a bit more.

The [:] = [] trick should be of most use when you go on to allocate
large amounts of data in your program once again, when the gc coud
then make some of the same memory available for the new objects.
As others have said, getting Python to give back memory to the
underlying OS is murder - The best way I have of doing that is to
split your program into two and chain their execution, which allows
the OS to reclaim the memory used by the first Python process when it
finishes.

- Paddy.

Jan 31 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
2947
by: Darren Dale | last post by:
I love the language. I love the community. My only complaint is that Python for Windows is built with Visual Studio. It is too difficult to build python, or a module, from source. This is what open source is all about, isnt it? I even have a copy of visual studio, and I still cant build modules from source, because my academic copy is version 7. As a scientist funded by the NSF, I feel compelled to do all my work using free software (I...
13
1667
by: Mars | last post by:
Why this error occurs?? typedef char *string; int main(void) { string str; str="ajsdklfajsdf"; printf("%s\n",str);
5
5757
by: Michael | last post by:
How can I determine a users PC Total system memory and memory available or free at the time of my program loading. Thanks
5
2038
by: prashantkhoje | last post by:
hi ppl, i am developing an C application in Microsoft visual C++ 6.0. i get following error while running it in debug mode: /* here's the error message */ program: my_application.exe DAMAGE: after Block number (#41) at 0x002F51C0
1
1732
by: Deyan Hadzhiev | last post by:
This is the full code of my program,which I need to write for a homework assignment. It worked without the "free()" function, but I wanted to optimize it. Adding the "free()" function led to crashes while it calculates the result, and I couldn't find the reason why.The problematic fraction is on lines 91 to 94. Any help would be appreciated. #include <stdio.h> #include <stdlib.h> #include <math.h> #define MAXDIM 200 /*This program...
0
7992
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7904
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,...
1
8051
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8267
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
6725
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...
0
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3940
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1505
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
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.