how to "free" an object/var ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stef Mientki

    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
  • James Stroud

    #2
    Re: how to "free&quot ; an object/var ?

    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

    Comment

    • Stef Mientki

      #3
      Re: how to "free&quot ; an object/var ?

      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

      Comment

      • Ben Finney

        #4
        Garbage collection (was: how to "free&quot ; an object/var ?)

        Stef Mientki <S.Mientki-nospam@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

        Comment

        • Aahz

          #5
          Re: how to &quot;free&quot ; an object/var ?

          In article <dfacb$45bfd788 $d443bb3a$7010@ news.speedlinq. nl>,
          Stef Mientki <S.Mientki-nospam@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 (aahz@pythoncra ft.com) <* http://www.pythoncraft.com/

          "I disrespectfully agree." --SJM

          Comment

          • Steven D'Aprano

            #6
            Re: how to &quot;free&quot ; an object/var ?

            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

            Comment

            • Paddy

              #7
              Re: how to &quot;free&quot ; an object/var ?

              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.

              Comment

              • John Nagle

                #8
                Re: how to &quot;free&quot ; an object/var ?

                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

                Comment

                • Steven D'Aprano

                  #9
                  Re: how to &quot;free&quot ; an object/var ?

                  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

                  Comment

                  • Paddy

                    #10
                    Re: how to &quot;free&quot ; an object/var ?

                    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.

                    Comment

                    • Aahz

                      #11
                      Re: how to &quot;free&quot ; an object/var ?

                      In article <AAXvh.1465$zH1 .1114@newssvr29 .news.prodigy.n et>,
                      John Nagle <nagle@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 (aahz@pythoncra ft.com) <* http://www.pythoncraft.com/

                      "I disrespectfully agree." --SJM

                      Comment

                      • Terry Reedy

                        #12
                        Re: how to &quot;free&quot ; an object/var ?


                        "John Nagle" <nagle@animats. comwrote in message
                        news:AAXvh.1465 $zH1.1114@newss vr29.news.prodi gy.net...
                        | 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



                        Comment

                        • Steven D'Aprano

                          #13
                          Re: how to &quot;free&quot ; an object/var ?

                          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

                          Comment

                          • Paddy

                            #14
                            Re: how to &quot;free&quot ; an object/var ?

                            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.

                            Comment

                            • greg

                              #15
                              OT: Variant name spellings (Re: how to &quot;free&quot ; an object/var ?)

                              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)

                              Comment

                              Working...