Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107060

Re: Python garbage collection: not releasing memory to OS!

Path csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail
From Sam <python@net153.net>
Newsgroups comp.lang.python
Subject Re: Python garbage collection: not releasing memory to OS!
Date Fri, 15 Apr 2016 09:27:44 -0500
Lines 53
Message-ID <mailman.27.1460731107.6324.python-list@python.org> (permalink)
References <215cf2a4-c2fa-41d0-8c49-23b9494234d4@googlegroups.com> <5710FA60.4070704@net153.net>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de K+Q1EaN32clsX/Pdabn7IQewpIgX7C6uCaq2l/5VnhCQ==
Return-Path <python@net153.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'sys': 0.05; '__name__': 0.07; 'thats': 0.07; 'works.': 0.07; '"but': 0.09; 'mem': 0.09; 'objects)': 0.09; 'spawn': 0.09; 'thread': 0.10; 'python': 0.10; 'python.': 0.11; 'itself.': 0.11; 'subject:not': 0.11; 'output': 0.13; 'def': 0.13; "'__main__':": 0.16; 'allocates': 0.16; 'f.read()': 0.16; 'from:addr:python': 0.16; 'gc.collect()': 0.16; 'googled': 0.16; 'heap': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'released.': 0.16; 'task.': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'config': 0.18; 'linux,': 0.18; 'input': 0.18; 'load': 0.20; 'releasing': 0.22; 'file.': 0.22; 'am,': 0.23; 'wrote': 0.23; 'finished': 0.23; 'import': 0.24; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'discussion': 0.24; 'script': 0.25; 'testing': 0.25; 'header:User- Agent:1': 0.26; 'allocated': 0.27; 'function': 0.28; 'fork': 0.29; 'skip:( 50': 0.29; 'sleep': 0.29; 'windows,': 0.29; 'objects': 0.29; 'print': 0.30; 'normally': 0.30; 'task': 0.30; 'checked': 0.31; 'noticed': 0.32; 'says': 0.32; 'run': 0.33; 'running': 0.34; 'returning': 0.35; 'skip:* 20': 0.35; 'stopped': 0.35; 'tasks': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'child': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'done.': 0.37; 'sure': 0.39; 'application': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'email addr:gmail.com': 0.62; 'total': 0.62; 'charset:windows-1252': 0.62; '(that': 0.63; 'completing': 0.72; 'article': 0.77; 'url:wordpress': 0.79; '**is': 0.84; 'perspective,': 0.84; 'killed': 0.91; 'received:10.36': 0.91; 'url:2013': 0.91
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.7.0
In-Reply-To <215cf2a4-c2fa-41d0-8c49-23b9494234d4@googlegroups.com>
X-Mailman-Approved-At Fri, 15 Apr 2016 10:38:25 -0400
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <5710FA60.4070704@net153.net>
X-Mailman-Original-References <215cf2a4-c2fa-41d0-8c49-23b9494234d4@googlegroups.com>
Xref csiph.com comp.lang.python:107060

Show key headers only | View raw


On 04/15/2016 05:25 AM, cshintov@gmail.com wrote:
> I have written an application with flask and uses celery for a long running task. While load testing I noticed that the celery tasks are not releasing memory even after completing the task. So I googled and found this group discussion..
>
> https://groups.google.com/forum/#!topic/celery-users/jVc3I3kPtlw
>
> In that discussion it says, thats how python works.
>
> Also the article at https://hbfs.wordpress.com/2013/01/08/python-memory-management-part-ii/ says
>
> "But from the OS's perspective, your program's size is the total (maximum) memory allocated to Python. Since Python returns memory to the OS on the heap (that allocates other objects than small objects) only on Windows, if you run on Linux, you can only see the total memory used by your program increase."
>
> And I use Linux. So I wrote the below script to verify it.
>
>      import gc
>      def memory_usage_psutil():
>          # return the memory usage in MB
>          import resource
>          print 'Memory usage: %s (MB)' % (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1000.0)
>
>      def fileopen(fname):
>          memory_usage_psutil()# 10 MB
>          f = open(fname)
>          memory_usage_psutil()# 10 MB
>          content = f.read()
>          memory_usage_psutil()# 14 MB
>
>      def fun(fname):
>          memory_usage_psutil() # 10 MB
>          fileopen(fname)
>          gc.collect()
>          memory_usage_psutil() # 14 MB
>
>      import sys
>      from time import sleep
>      if __name__ == '__main__':
>          fun(sys.argv[1])
>          for _ in range(60):
>              gc.collect()
>              memory_usage_psutil()#14 MB ...
>              sleep(1)
>
> The input was a 4MB file. Even after returning from the 'fileopen' function the 4MB memory was not released. I checked htop output while the loop was running, the resident memory stays at 14MB. So unless the process is stopped the memory stays with it.
>
> So if the celery worker is not killed after its task is finished it is going to keep the memory for itself. I know I can use **max_tasks_per_child** config value to kill the process and spawn a new one. **Is there any other way to return the memory to OS from a python process?.**
>


With situations like this, I normally just fork and do the mem intensive 
work in the child and then kill it off when done. Might be  able to use 
a thread instead of a fork. But not sure how well all that would work 
with celery.

--Sam

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Python garbage collection: not releasing memory to OS! cshintov@gmail.com - 2016-04-15 03:25 -0700
  Re: Python garbage collection: not releasing memory to OS! Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-15 08:24 -0400
  Re: Python garbage collection: not releasing memory to OS! Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-04-15 14:02 +0100
  Re: Python garbage collection: not releasing memory to OS! Michael Torrie <torriem@gmail.com> - 2016-04-15 07:59 -0600
  Re: Python garbage collection: not releasing memory to OS! Sam <python@net153.net> - 2016-04-15 09:27 -0500

csiph-web