473,399 Members | 3,401 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,399 software developers and data experts.

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 17896
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**@pythoncraft.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_reference = 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_reference" 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_reference = 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_reference" 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"*1000000 # 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"*1000000 # 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
In article <AA*****************@newssvr29.news.prodigy.net> ,
John Nagle <na***@animats.comwrote:
>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.
Note that exceptions create links.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"I disrespectfully agree." --SJM
Jan 31 '07 #11

"John Nagle" <na***@animats.comwrote in message
news:AA*****************@newssvr29.news.prodigy.ne t...
| If your data structure has no backlinks, it will go away
| as soon as the last reference to it disappears.
| In Python, garbage collection is mostly a backup to
| the reference counting system.

These both are true of the CPython implementation (current and known
future, at least) but not of other implementations and not of the language
itself.

The same comment applies to some other posts in this thread. GC, if any,
is implementation defined.

tjr

Jan 31 '07 #12
On Wed, 31 Jan 2007 09:26:46 -0800, Paddy wrote:
On Jan 31, 7:34 am, Steven D'Aprano <s...@REMOVEME.cybersource.com.au>
wrote:
[snip]
>--
Steven D'Aprano

Thanks Stephen for explaining my answer a bit more.
Ste_ph_en???

I know the ph-form of the name is marginally more popular, but dammit my
name is right there just two lines above where Paddy started typing, how
hard is it to get it right?

It's not like I spell my name with four M's and a silent Q like the famous
author Farles Wickens *wink*
--
Steven D'Aprano
who thinks there can never be too many Monty Python references

Feb 1 '07 #13
On Feb 1, 1:45 am, Steven D'Aprano <s...@REMOVEME.cybersource.com.au>
wrote:
On Wed, 31 Jan 2007 09:26:46 -0800, Paddy wrote:
On Jan 31, 7:34 am, Steven D'Aprano <s...@REMOVEME.cybersource.com.au>
wrote:

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

Ste_ph_en???

I know the ph-form of the name is marginally more popular, but dammit my
name is right there just two lines above where Paddy started typing, how
hard is it to get it right?

It's not like I spell my name with four M's and a silent Q like the famous
author Farles Wickens *wink*

--
Steven D'Aprano
who thinks there can never be too many Monty Python references
Please accept my most humblest of apologies Steven D'Aprano. I can
only think I had a brain seizure between reading, and writing your
name,

Come to think of it... The Stephen/Steven confusion should only apply
when you *hear* the name said? Now I don't read aloud newsgroups to
myself; but I do (still), have a head cold. Maybe I should lie down
and rest....

- Paddy.

Feb 1 '07 #14
Steven D'Aprano wrote:
>
Ste_ph_en???
I knew someone once who referred to the two ways
of spelling Ste{v,ph}en as the "dry way" and
the "wet way"...
It's not like I spell my name with four M's and a silent Q like the famous
author Farles Wickens *wink*
Or Mr. Luxury-Yacht, which as we all know is
pronounced Throatwarbler-Mangrove. With a
silent hyphen.

--
Greg Psmith (pronounced You-ing)
Feb 1 '07 #15
Steven D'Aprano wrote:
Ste_ph_en???

I know the ph-form of the name is marginally more popular, but dammit my
name is right there just two lines above where Paddy started typing, how
hard is it to get it right?

It's not like I spell my name with four M's and a silent Q like the famous
author Farles Wickens *wink*
"I knew a guy whose first name was Ed. He was so cool, he spelled it
with a hyphen." -- George Carlin

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
If the sky should fall, hold up your hands.
-- (a Spanish proverb)
Feb 1 '07 #16
greg <gr**@cosc.canterbury.ac.nzwrites:
Or Mr. Luxury-Yacht, which as we all know is pronounced
Throatwarbler-Mangrove. With a silent hyphen.
You're a very silly person and I'm not going to interview you anymore.

--
\ "When I turned two I was really anxious, because I'd doubled my |
`\ age in a year. I thought, if this keeps up, by the time I'm six |
_o__) I'll be ninety." -- Steven Wright |
Ben Finney

Feb 1 '07 #17

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

Similar topics

33
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...
13
by: Mars | last post by:
Why this error occurs?? typedef char *string; int main(void) { string str; str="ajsdklfajsdf"; printf("%s\n",str);
5
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
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...
1
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.