clear memory? how?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • N/A

    clear memory? how?

    Hi all,
    I am learning Python. Just wondering how to clear saved memory in
    Python? Like in Matlab I can simply use "clear all" to clear all saved
    memory.

    Thank u!
  • Diez B. Roggisch

    #2
    Re: clear memory? how?

    N/A wrote:
    [color=blue]
    > Hi all,
    > I am learning Python. Just wondering how to clear saved memory in
    > Python? Like in Matlab I can simply use "clear all" to clear all saved
    > memory.[/color]

    You don't - python does it for you. It is called garbage collection. All you
    have to to is get into granny-mode(tm): forget about things. That means:
    once an object is not referenced by your code anymore, it will be cleaned
    up.

    For example:
    [color=blue][color=green][color=darkred]
    >>> l = range(100000)
    >>> del l[/color][/color][/color]

    will make python garbage collect the list referred to by l. Actually, this
    will work too:

    [color=blue][color=green][color=darkred]
    >>> l = range(100000)
    >>> l = None[/color][/color][/color]

    Because l now refers another object (None in this case), the _list_ that was
    refered beforehand gets freed as it's reference counter is reduced by one.

    Generally speaking: don't worry.

    HTH,

    Diez

    Comment

    • Andrew Gwozdziewycz

      #3
      Re: clear memory? how?

      Glad to see you're trying Python instead of Matlab. Python was never
      meant to be a replacement for Matlab. It's a general purpose
      programming language like Java, or C#. Though it has very little in
      common with either of those languages. Since Python is a general
      purpose language, if you want Matlab like functionality, you need to
      rely on some other libraries that exist out there. Matplotlib, NumPy
      to name a few will make your Matlab- to Python transition go much
      more smoothly. You might also want to check out ipython, which is
      just a different interface to the python toplevel.


      On May 9, 2006, at 4:27 AM, N/A wrote:
      [color=blue]
      > Hi all,
      > I am learning Python. Just wondering how to clear saved memory in
      > Python? Like in Matlab I can simply use "clear all" to clear all saved
      > memory.
      >
      > Thank u!
      > --
      > http://mail.python.org/mailman/listinfo/python-list[/color]

      ---
      Andrew Gwozdziewycz
      apgwoz@gmail.co m





      Comment

      • Ben C

        #4
        Re: clear memory? how?

        On 2006-05-09, Diez B. Roggisch <deets@nospam.w eb.de> wrote:[color=blue]
        > N/A wrote:
        >[color=green]
        >> Hi all,
        >> I am learning Python. Just wondering how to clear saved memory in
        >> Python? Like in Matlab I can simply use "clear all" to clear all saved
        >> memory.[/color]
        >
        > You don't - python does it for you. It is called garbage collection. All you
        > have to to is get into granny-mode(tm): forget about things. That means:
        > once an object is not referenced by your code anymore, it will be cleaned
        > up.[/color]

        I think Matlab's "clear all" is more like what you might call "del all"
        in python.

        You could perhaps define it like this:

        def clearall():
        all = [var for var in globals() if var[0] != "_"]
        for var in all:
        del globals()[var]

        This deletes any global not starting with an _, since it's probably
        inadvisable to delete this lot:

        {'__builtins__' : <module '__builtin__' (built-in)>, '__file__':
        '/etc/pythonstart', '__name__': '__main__', '__doc__': None}

        More correct I suppose might be something like this:

        def clearall():
        all = [var for var in globals() if "__" not in (var[:2], var[-2:])]
        for var in all:
        del globals()[var]

        since I think magic things always start and end with __.

        Looking briefly at GNU octave which is similar to MatLab, clear all may
        also del all the locals; so you can do something similar with the
        builtin function locals().

        Comment

        • Terry Reedy

          #5
          Re: clear memory? how?


          "Diez B. Roggisch" <deets@nospam.w eb.de> wrote in message
          news:4cb49uF158 11rU1@uni-berlin.de...[color=blue]
          > You don't - python does it for you. It is called garbage collection. All
          > you
          > have to to is get into granny-mode(tm): forget about things.[/color]

          In general, especially for beginners, this is good advice.
          Forget about memory management until one actually runs into a problems.
          [color=blue]
          >That means: once an object is not referenced by your code anymore,
          > it will be cleaned up.[/color]

          The language spec only says that it *may* be cleaned up. Even with
          CPython, it will not be immediately deleted if caught in a circular
          reference loop with other objects no longer referenced by the code. One
          may then want to explicitly invoke the circular ref garbage collector. And
          read the gc module docs for details.
          [color=blue]
          > For example:[color=green][color=darkred]
          >>>> l = range(100000)
          >>>> del l[/color][/color]
          > will make python garbage collect the list referred to by l.[/color]

          No, it only *allows* the interpreter to gc the list and (most of -
          depending on the implementation) the ints in the list. When, if ever, is
          implementation dependent.
          Even with CPython, ranges much larger than that are better done as xranges
          unless the explicit all-at-once list is really needed.
          [color=blue]
          > Generally speaking: don't worry.[/color]

          Terry Jan Reedy



          Comment

          • Ben C

            #6
            Re: clear memory? how?

            On 2006-05-09, Ben C <spamspam@spam. eggs> wrote:[color=blue]
            > def clearall():
            > all = [var for var in globals() if "__" not in (var[:2], var[-2:])]
            > for var in all:
            > del globals()[var]
            >
            > since I think magic things always start and end with __.[/color]

            Oops, got that wrong anyway:

            should be:

            all = [var for var in globals() if (var[:2], var[-2:]) != ("__", "__")]

            Comment

            • Grégoire Dooms

              #7
              Re: clear memory? how?

              Ben C wrote:[color=blue]
              > On 2006-05-09, Ben C <spamspam@spam. eggs> wrote:[color=green]
              >> def clearall():
              >> all = [var for var in globals() if "__" not in (var[:2], var[-2:])]
              >> for var in all:
              >> del globals()[var]
              >>
              >> since I think magic things always start and end with __.[/color]
              >
              > Oops, got that wrong anyway:
              >
              > should be:
              >
              > all = [var for var in globals() if (var[:2], var[-2:]) != ("__", "__")][/color]

              You can also add
              and var != "clearall"

              :-)

              --
              Grégoire

              Comment

              • Ben C

                #8
                Re: clear memory? how?

                On 2006-05-10, Grégoire Dooms <doomsAinfoDucl DacDbe@AisATand DisDOT.com> wrote:[color=blue]
                > Ben C wrote:[color=green]
                >> On 2006-05-09, Ben C <spamspam@spam. eggs> wrote:[color=darkred]
                >>> def clearall():
                >>> all = [var for var in globals() if "__" not in (var[:2], var[-2:])]
                >>> for var in all:
                >>> del globals()[var]
                >>>
                >>> since I think magic things always start and end with __.[/color]
                >>
                >> Oops, got that wrong anyway:
                >>
                >> should be:
                >>
                >> all = [var for var in globals() if (var[:2], var[-2:]) != ("__", "__")][/color]
                >
                > You can also add
                > and var != "clearall"
                >
                >:-)[/color]

                Good point :)

                I've heard of "write once run anywhere", but this is "run once".

                Comment

                Working...