Results 1 to 6 of 6

Thread: Python Memory management

  1. #1
    Join Date
    Jan 2009
    Posts
    46

    Python Memory management

    Hello all - I come here seek assistance due to a problem . By dint of searching, I managed to bring the problem to a simple example which will serve as a demo easily understandable

    I manage a text object. For this purpose there are various methods call me to get info on it as nb lines, words, etc..
    At one point, I must submit a paragraph of text. I say to myself "a paragraph is a mini-text, if I return a new instance of my object does not contain that paragraph, this body will have all useful methods (number of lines, words, etc.).

    Here is a small simple example showing the problem. For simplicity, I just created an object that stores a table of numbers and created a "spell" that returns an object containing the table sorted Code:
    #! / usr / bin / env python
    # Coding: Latin-1 -*-
    cGestion class:
    Def __init__ (self):
    Print "init:% s"% repr (self)
    Self.tab = []
    Def __del__ (self):
    Print "del% s"% repr (self)
    Del self.tab [:]
    Del self.tab
    Def __str__ (self):
    return "[% s]:% s"% (repr (self), self.tab)
    Def append (self, k):
    Self.tab.append (k)
    Def sort (self):
    N = cGestion ()
    For k in self.tab:
    N.append (k)
    N.tab.sort ()
    Return n
    # Create basic element
    cGestion base = ()
    base.append (2)
    base.append (1)
    base.append (0)
    print "\ nbase:", base
    Test # 1: sorted copy of the basic object - a copy is stored
    cp = base.sort ()
    print "\ ntest 1:", cp, cp.tab
    del cp
    Test # 2: just display a sorted
    print "\ ntest 2:", base.sort ()
    Test # 3: display of an element of the body sorted
    print "\ ntest 3:", base.sort (). tab

    At the base, I create my object. The displays are ok
    In test 1, I get a copy of my item sorted. No worries
    In test 2, I am just copying sorted my object but have not saved. But no worries
    In test 3, where I do a table stored in the copy when the copy and I am aware that the destroyer has made his office and the table is empty.

    What is interesting is that if in the destructor delete the cleaning of the table (del self.tab [:]), is all going well. I know that Python has a memory management and that I should not worry about me release my internal table but I'm from the C and it's stronger than me. If I create a constructor, destructor deletes my element.

    Someone would give me advice?

    Thank you

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Python Memory management

    The behavior is quite normal in the test 3, you have no reference to the object stored rejected by base.sort (). When you posters base.sort (). Tab, Python automatically calls the destructor to the object rejected by base.sort () (since no reference is made to that object), and as you remove the destructive element tab that purpose, by extension, it removes everything before you view it.

  3. #3
    Join Date
    Jan 2009
    Posts
    46

    Re: Python Memory management

    Yo thank you for responding so quickly. You describe actually what I suspected (especially since I see the "del" appear before displaying the "test3"). But how then to
    1) kept clean level memory management
    2) can still recover my base.sort (). Tab "
    ?

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Python Memory management

    1) Do not manage your own memory when it is not necessary.
    2) Respect the 1 ^ ^


    Defining a del on your subject adds nothing to the garbage collector, as you do (or less) that redefine the default behavior (the variation being that it does not work here). If you want to free memory, a simple deletion of the variable referencing the object is enough to have a cleansing "cascading" if necessary, processed by the garbage collector automatically at the most opportune time.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Python Memory management

    It removes things even. This prevents circular references detector to work properly for example, a ring containing an object (or more) with a method defined __del__ will not be detected, thus not deleted, and should be fully cleaned by hand.

    We should never define __del__ when you are not sure exactly what you need (and generally it is only the case of scarce resources: handlers files or sockets, connections, ...).

  6. #6
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Python Memory management

    The GC runs in both cases, there is no reason why there is a significant difference, and the 2nd version has a direction (del) more than the other Code:
    >>> Class A (object):
    def __init__ (self):
    self. tab = []
    >>> Def testref ():
    for i in range (1000):
    A = A ()
    >>> Def testdel ():
    for i in range (1000):
    A = A ()
    del a
    >>> From timeit import Timer
    >>> T1 = Timer ( 'testref ()', 'from __main__ import testref')
    >>> T2 = Timer ( 'testdel ()', 'from __main__ import testdel')
    >>> T1. Repeat (number = 10000)
    [11.701371822396425, 11.629359038648772, 11.613105855632483]
    >>> T2. Repeat (number = 10000)
    [11.716563138611193, 11.681633000842297, 11.773158599766163]
    >>>

    Must stop attempts to con micro optimisation in this genre, especially when you do not know the runtime and you have no evidence of a perf problem (let alone not knowing where is the problem if any)

    And incidentally, the __del__ which is no clearly has a strong impact on this microbench to con (in a real application it would probably be invisible) as the runtime must call a Python function (although the removal of original is fully) and run a slice and two del within: Code:
    >>> Class B (object):
    def __init__ (self):
    self. tab = []
    def __del__ (self):
    del self. tab [:]
    del self. tab
    >>> Def testrefB ():
    for i in range (1000):
    B = B ()
    >>> Def testdelB ():
    for i in range (1000):
    B = B ()
    del b
    >>> T3 = Timer ( 'testrefB ()', 'from __main__ import testrefB')
    >>> T4 = Timer ( 'testdelB ()', 'from __main__ import testdelB')
    >>> T3. Repeat (number = 10000)
    [19.552072400263171, 19.476673152593378, 19.429103419568719]
    >>> T4. Repeat (number = 10000)
    [20.073180809292751, 20.089330068486333, 20.225080003184758]
    >>>


    (exactly the same code as above, I just added your __del__ in the class)

Similar Threads

  1. Efficient memory usage with iterators in Python
    By Sivann in forum Software Development
    Replies: 4
    Last Post: 25-10-2010, 01:53 PM
  2. How to improve SQL Server memory management
    By Tylerrr in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 02:20 AM
  3. Need memory management tools for windows XP
    By Akiraa in forum Windows Software
    Replies: 5
    Last Post: 31-01-2010, 03:29 AM
  4. Memory allocation and management
    By AdityaR in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 08:42 PM
  5. GWT memory management
    By Protector in forum Software Development
    Replies: 7
    Last Post: 15-01-2009, 09:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,589,007.99376 seconds with 17 queries