Deleting objects on the fly

Discussion in 'Python' started by Godzilla, Aug 10, 2007.

  1. Godzilla

    Godzilla Guest

    Hello,

    I wish to know whether I should delete objects created on the fly via
    the "del obj" statement. I noticed the RAM usage increased whenever
    the application is being run for a long time. I am creating lots of
    objects (messages) on the fly for communication between threads.

    Rather than having python's gc to do the work, does it make a
    difference if I force a deletion?

    Thanks.
     
    Godzilla, Aug 10, 2007
    #1
    1. Advertisements

  2. On Aug 10, 2:25 am, Godzilla <> wrote:
    > Hello,
    >
    > I wish to know whether I should delete objects created on the fly via
    > the "del obj" statement. I noticed the RAM usage increased whenever
    > the application is being run for a long time. I am creating lots of
    > objects (messages) on the fly for communication between threads.
    >
    > Rather than having python's gc to do the work, does it make a
    > difference if I force a deletion?
    >
    > Thanks.


    Probably not, 'del x' just decrements the reference count, but
    it is the gc who does the real job. See http://docs.python.org/ref/customization.html#l2h-175

    Do you have reference cycles in your application? You should
    tell us something more.

    Michele Simionato
     
    Michele Simionato, Aug 10, 2007
    #2
    1. Advertisements

  3. Michele Simionato wrote:
    > On Aug 10, 2:25 am, Godzilla <> wrote:
    >> Hello,
    >>
    >> I wish to know whether I should delete objects created on the fly via
    >> the "del obj" statement. I noticed the RAM usage increased whenever
    >> the application is being run for a long time. I am creating lots of
    >> objects (messages) on the fly for communication between threads.
    >>
    >> Rather than having python's gc to do the work, does it make a
    >> difference if I force a deletion?
    >>
    >> Thanks.

    >
    > Probably not, 'del x' just decrements the reference count, but
    > it is the gc who does the real job. See http://docs.python.org/ref/customization.html#l2h-175
    >
    > Do you have reference cycles in your application? You should
    > tell us something more.
    >
    > Michele Simionato
    >


    del x will remove x from memory if nothing else is refering to it, but
    this dosnt really take the load off the GC since the GC will still check
    the references and remove if there are none.

    In some cases you could use to save ram...

    a = 'really big string'
    ....do stuff with a...
    del a

    b = 'another really big string'
    ....do stuff with b...
    del b


    in the case that 'a' is not needed, after its used, and you dont want to
    re-use the variable name. then del will free some ram since they both
    wont need to exist at the same time.

    WHen I say free ram, python its self will probably keep the ram for
    later use but at least it wont need a and b in memory at once, so its
    likely not to use as much ram.

    But be careful using del in a loop since it can slow things down, its
    like adding and removing an item from a dictionary many times. your
    better off just redefining that variable or using del after the loops
    finished.
     
    Campbell Barton, Aug 10, 2007
    #3
  4. Godzilla

    Terry Reedy Guest

    "Campbell Barton" <> wrote in message
    news:...
    | Michele Simionato wrote:
    | > Probably not, 'del x' just decrements the reference count,

    Or as
    http://docs.python.org/ref/del.html
    puts it, " Deletion of a name removes the binding of that name from the
    local or global namespace,"

    | del x will remove x from memory if nothing else is refering to it,

    This is implementation dependent: true for CPython, not for Jython, ??? for
    IronPython.

    tjr
     
    Terry Reedy, Aug 10, 2007
    #4
  5. Godzilla

    Dustan Guest

    On Aug 10, 1:49 pm, "Terry Reedy" <> wrote:
    > "Campbell Barton" <> wrote in message
    >
    > news:...| Michele Simionato wrote:
    >
    > | > Probably not, 'del x' just decrements the reference count,
    >
    > Or ashttp://docs.python.org/ref/del.html
    > puts it, " Deletion of a name removes the binding of that name from the
    > local or global namespace,"
    >
    > | del x will remove x from memory if nothing else is refering to it,
    >
    > This is implementation dependent: true for CPython, not for Jython, ??? for
    > IronPython.


    Wait a second; do you mean to say that in Jython, del x will never
    remove x from memory? How do you free up RAM?
     
    Dustan, Aug 11, 2007
    #5
  6. Godzilla

    Paul Rubin Guest

    Dustan <> writes:
    > > | del x will remove x from memory if nothing else is refering to it,
    > > This is implementation dependent: true for CPython, not for Jython, ??? for
    > > IronPython.

    > Wait a second; do you mean to say that in Jython, del x will never
    > remove x from memory? How do you free up RAM?


    It stays around until the GC picks it up.
     
    Paul Rubin, Aug 11, 2007
    #6
  7. Godzilla

    Steve Holden Guest

    Dustan wrote:
    > On Aug 10, 1:49 pm, "Terry Reedy" <> wrote:
    >> "Campbell Barton" <> wrote in message
    >>
    >> news:...| Michele Simionato wrote:
    >>
    >> | > Probably not, 'del x' just decrements the reference count,
    >>
    >> Or ashttp://docs.python.org/ref/del.html
    >> puts it, " Deletion of a name removes the binding of that name from the
    >> local or global namespace,"
    >>
    >> | del x will remove x from memory if nothing else is refering to it,
    >>
    >> This is implementation dependent: true for CPython, not for Jython, ??? for
    >> IronPython.

    >
    > Wait a second; do you mean to say that in Jython, del x will never
    > remove x from memory? How do you free up RAM?
    >

    Because the exact method of garbage collection is independent of the
    language definition, Jython uses the Java garbage collector, which works
    (approximately) as follows.

    In that language memory is allocated until a request cannot be
    satisfies, then a scan is performed for unreferenced objects whose space
    is reclaimed. Only if this doesn't yield enough space to allocate the
    new object is more memory requested from the OS by the process.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
     
    Steve Holden, Aug 11, 2007
    #7
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Um9tYW4gUGF3bGln?=

    Creating Objects ( response for example ) On The Fly

    =?Utf-8?B?Um9tYW4gUGF3bGln?=, Dec 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    410
    Karl Seguin
    Dec 18, 2004
  2. Replies:
    6
    Views:
    438
    Ingo R. Homann
    Dec 11, 2006
  3. Harry Barker
    Replies:
    2
    Views:
    613
    Alf P. Steinbach
    Apr 19, 2006
  4. Brian

    Fly outmenu on the fly

    Brian, Apr 8, 2005, in forum: Javascript
    Replies:
    0
    Views:
    227
    Brian
    Apr 8, 2005
  5. crea
    Replies:
    2
    Views:
    530
    Nobody
    Dec 28, 2012
Loading...

Share This Page