<bdo id='qtbBB'></bdo><ul id='qtbBB'></ul>
        <tfoot id='qtbBB'></tfoot>
      1. <legend id='qtbBB'><style id='qtbBB'><dir id='qtbBB'><q id='qtbBB'></q></dir></style></legend>

        <small id='qtbBB'></small><noframes id='qtbBB'>

        <i id='qtbBB'><tr id='qtbBB'><dt id='qtbBB'><q id='qtbBB'><span id='qtbBB'><b id='qtbBB'><form id='qtbBB'><ins id='qtbBB'></ins><ul id='qtbBB'></ul><sub id='qtbBB'></sub></form><legend id='qtbBB'></legend><bdo id='qtbBB'><pre id='qtbBB'><center id='qtbBB'></center></pre></bdo></b><th id='qtbBB'></th></span></q></dt></tr></i><div id='qtbBB'><tfoot id='qtbBB'></tfoot><dl id='qtbBB'><fieldset id='qtbBB'></fieldset></dl></div>

      2. 在 Python 中强制垃圾收集以释放内存

        时间:2023-07-05

          • <legend id='psKnH'><style id='psKnH'><dir id='psKnH'><q id='psKnH'></q></dir></style></legend>
              <tbody id='psKnH'></tbody>

                <i id='psKnH'><tr id='psKnH'><dt id='psKnH'><q id='psKnH'><span id='psKnH'><b id='psKnH'><form id='psKnH'><ins id='psKnH'></ins><ul id='psKnH'></ul><sub id='psKnH'></sub></form><legend id='psKnH'></legend><bdo id='psKnH'><pre id='psKnH'><center id='psKnH'></center></pre></bdo></b><th id='psKnH'></th></span></q></dt></tr></i><div id='psKnH'><tfoot id='psKnH'></tfoot><dl id='psKnH'><fieldset id='psKnH'></fieldset></dl></div>

                <small id='psKnH'></small><noframes id='psKnH'>

              1. <tfoot id='psKnH'></tfoot>
                • <bdo id='psKnH'></bdo><ul id='psKnH'></ul>
                  本文介绍了在 Python 中强制垃圾收集以释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 Python2.7 应用程序,它使用了很多 dict 对象,这些对象主要包含键和值的字符串.

                  I have a Python2.7 App which used lots of dict objects which mostly contain strings for keys and values.

                  有时不再需要这些字典和字符串,我想将它们从内存中删除.

                  Sometimes those dicts and strings are not needed anymore and I would like to remove those from memory.

                  我尝试了不同的东西,del dict[key]del dict 等.但是应用程序仍然使用相同的内存量.

                  I tried different things, del dict[key], del dict, etc. But the App still uses the same amount of memory.

                  下面是一个我希望为内存付费的示例.但它没有:(

                  Below a example which I would expect to fee the memory. But it doesn't :(

                  import gc
                  import resource
                  
                  def mem():
                      print('Memory usage         : % 2.2f MB' % round(
                          resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024.0/1024.0,1)
                      )
                  
                  mem()
                  
                  print('...creating list of dicts...')
                  n = 10000
                  l = []
                  for i in xrange(n):
                      a = 1000*'a'
                      b = 1000*'b'
                      l.append({ 'a' : a, 'b' : b })
                  
                  mem()
                  
                  print('...deleting list items...')
                  
                  for i in xrange(n):
                      l.pop(0)
                  
                  mem()
                  
                  print('GC collected objects : %d' % gc.collect())
                  
                  mem()
                  

                  输出:

                  Memory usage         :  4.30 MB
                  ...creating list of dicts...
                  Memory usage         :  36.70 MB
                  ...deleting list items...
                  Memory usage         :  36.70 MB
                  GC collected objects : 0
                  Memory usage         :  36.70 MB
                  

                  我希望在这里收集"一些对象并释放一些内存.

                  I would expect here some objects to be 'collected' and some memory to be freed.

                  我做错了吗?任何其他删除未使用对象或至少查找意外使用对象的位置的方法.

                  Am I doing something wrong? Any other ways to delete unused objects or a least to find where the objects are unexpectedly used.

                  推荐答案

                  Frederick Lundh 解释,

                  如果你创建一个大对象并再次删除它,Python 可能已经释放了内存,但所涉及的内存分配器不一定返回内存到操作系统,所以它看起来好像 Python 进程使用比实际使用的虚拟内存多得多.

                  If you create a large object and delete it again, Python has probably released the memory, but the memory allocators involved don’t necessarily return the memory to the operating system, so it may look as if the Python process uses a lot more virtual memory than it actually uses.

                  Alex Martelli 写道:

                  唯一真正可靠的方法可以确保大型但内存的临时使用确实在完成后将所有资源返回给系统,是让这种使用发生在一个子进程中,它完成了需要大量内存的工作然后终止.

                  The only really reliable way to ensure that a large but temporary use of memory DOES return all resources to the system when it's done, is to have that use happen in a subprocess, which does the memory-hungry work then terminates.

                  因此,您可以使用 multiprocessing 生成子进程,执行内存占用计算,然后确保在子进程终止时释放内存:

                  So, you could use multiprocessing to spawn a subprocess, perform the memory-hogging calculation, and then ensure the memory is released when the subprocess terminates:

                  import multiprocessing as mp
                  import resource
                  
                  def mem():
                      print('Memory usage         : % 2.2f MB' % round(
                          resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024.0,1)
                      )
                  
                  mem()
                  
                  def memoryhog():
                      print('...creating list of dicts...')
                      n = 10**5
                      l = []
                      for i in xrange(n):
                          a = 1000*'a'
                          b = 1000*'b'
                          l.append({ 'a' : a, 'b' : b })
                      mem()
                  
                  proc = mp.Process(target=memoryhog)
                  proc.start()
                  proc.join()
                  
                  mem()
                  

                  产量

                  Memory usage         :  5.80 MB
                  ...creating list of dicts...
                  Memory usage         :  234.20 MB
                  Memory usage         :  5.90 MB
                  

                  这篇关于在 Python 中强制垃圾收集以释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:复合对象上的python垃圾收集器行为 下一篇:python什么时候删除变量?

                  相关文章

                • <i id='5Ssbl'><tr id='5Ssbl'><dt id='5Ssbl'><q id='5Ssbl'><span id='5Ssbl'><b id='5Ssbl'><form id='5Ssbl'><ins id='5Ssbl'></ins><ul id='5Ssbl'></ul><sub id='5Ssbl'></sub></form><legend id='5Ssbl'></legend><bdo id='5Ssbl'><pre id='5Ssbl'><center id='5Ssbl'></center></pre></bdo></b><th id='5Ssbl'></th></span></q></dt></tr></i><div id='5Ssbl'><tfoot id='5Ssbl'></tfoot><dl id='5Ssbl'><fieldset id='5Ssbl'></fieldset></dl></div>
                  1. <tfoot id='5Ssbl'></tfoot>

                  2. <legend id='5Ssbl'><style id='5Ssbl'><dir id='5Ssbl'><q id='5Ssbl'></q></dir></style></legend>
                    1. <small id='5Ssbl'></small><noframes id='5Ssbl'>

                      • <bdo id='5Ssbl'></bdo><ul id='5Ssbl'></ul>