Deleting objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • user@domain.invalid

    Deleting objects

    Say I have a database object 'db',
    that contains table objects, that contain field
    objects so that I can do things like this:

    print db.table.field. value()


    Now, after plundering the database for a while,
    the db objects heads a tree of lots of data.

    If I want to "reset" the object so that I get
    all of my memory back, can I just do:

    del db

    or maybe:

    for t in db.tables:
    del t

    and expect that I have cleaned up all of my
    memory?

    After writing this out, I see that the answer
    is clearly yes, but I will post anyway.

    Thanks

    Toby

  • Jeff Epler

    #2
    Re: Deleting objects

    On Mon, Jan 26, 2004 at 08:04:16PM +0000, user@domain.inv alid wrote:
    [Can I execute][color=blue]
    > for t in db.tables:
    > del t
    >
    > and expect that I have cleaned up all of my
    > memory?[/color]

    The above code clearly doesn't "free" anything, because there are still
    references to each of the values 't' has at deletion---namely, the entry
    in db.tables!

    Jeff

    Comment

    • Gerrit Holl

      #3
      Re: Deleting objects

      user@domain.inv alid wrote:[color=blue]
      > If I want to "reset" the object so that I get
      > all of my memory back, can I just do:
      >
      > del db[/color]

      This may work, if you don't have other references.
      [color=blue]
      > or maybe:
      >
      > for t in db.tables:
      > del t
      >
      > and expect that I have cleaned up all of my
      > memory?
      >
      > After writing this out, I see that the answer
      > is clearly yes, but I will post anyway.[/color]

      The answer is no, really :-)

      'del' only removes a certain name from the current namespace. An object
      is only destroyed when *all* references to it have been destroyed.
      When you are looping over a certain collection, each time the body of
      the loop is executed, the loop variable (here 't') gets redefined. So if
      the referencecount to a certain object (db.tables[x]) is N, it will
      become temporarily N+1 when looping over it: with 'del t', you destroy
      the local name, so it becomes N again. Conclusion: This aint gonna work.

      Because of this, it is generally better to explicitly kill/destroy/close
      something. I don't know anything about databases, but maybe you want to
      ..close() it? 'del db' may work, but can be tricky if other objects, like
      dicts, lists, sets or others, contain (nameless) references to the
      object.

      yours,
      Gerrit.

      --
      This is my signature.
      --
      PrePEP: Builtin path type

      Asperger's Syndrome - a personal approach:


      Comment

      • Terry Reedy

        #4
        Re: Deleting objects


        <user@domain.in valid> wrote in message
        news:a33a46fe50 e2ab07720a1118a a22d576@news.te ranews.com...[color=blue]
        > Say I have a database object 'db',
        > that contains table objects, that contain field
        > objects so that I can do things like this:
        >
        > print db.table.field. value()
        >
        >
        > Now, after plundering the database for a while,
        > the db objects heads a tree of lots of data.
        >
        > If I want to "reset" the object so that I get
        > all of my memory back, can I just do:
        >
        > del db
        >
        > or maybe:
        >
        > for t in db.tables:
        > del t
        >
        > and expect that I have cleaned up all of my
        > memory?
        >
        > After writing this out, I see that the answer
        > is clearly yes, but I will post anyway.[/color]

        Why?

        Actually, the issue is not so simple. 'del db' deletes the association
        between name db and the database object. If there is another reference to
        the object, that is all that happens. If not, memory cleanup depends on
        the implementation. CPython will 'free' memory immediately (and
        recursively delete tables). Jython waits for next garbage collection
        cycle. 'Free' may just mean available for reuse by Python but not by other
        programs.

        Db objects often have a close method. You may get more of what you want by
        calling that before del.

        Terry J. Reedy




        Comment

        • Terry Reedy

          #5
          Re: Deleting objects

          [color=blue][color=green]
          > > for t in db.tables:
          > > del t[/color][/color]

          As Gerrit said, this is equivalent to 'pass'.

          db.tables = [] # or
          db.tables[:] = [] # or
          db.cleartables( )

          is more likely to work. But a db.close() method should contain what will
          work.

          TJR






          Comment

          Working...