how to "free" an object/var ?

S

Stef Mientki

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
 
J

James Stroud

Stef said:
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
 
S

Stef Mientki

James said:
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
 
B

Ben Finney

Stef Mientki said:
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)
[...]
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.
 
A

Aahz

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.
 
S

Steven D'Aprano

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.
 
P

Paddy

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.

The folowing will make the data available for garbage collection no
matter what references it:
['data', 'data', 'data', 'data', 'data', 'data', 'data', 'data',
'data', 'data']

- Paddy.
 
J

John Nagle

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
 
S

Steven D'Aprano

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.

The folowing will make the data available for garbage collection no
matter what references it:
['data', 'data', 'data', 'data', 'data', 'data', 'data', 'data', 'data',
'data']
[]

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.
 
P

Paddy

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.

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.
 
A

Aahz

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.
 
T

Terry Reedy

| 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
 
S

Steven D'Aprano

On Jan 31, 7:34 am, Steven D'Aprano <[email protected]>
wrote:
[snip]

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*
 
P

Paddy

On Jan 31, 7:34 am, Steven D'Aprano <[email protected]>
wrote:
[snip]
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*

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.
 
G

greg

Steven said:
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.
 
E

Erik Max Francis

Steven said:
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
 
B

Ben Finney

greg said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top