相关疑难解决方法(0)

在Python中释放内存

在下面的示例中,我有一些关于内存使用的相关问题.

  1. 如果我在翻译中跑,

    foo = ['bar' for _ in xrange(10000000)]
    
    Run Code Online (Sandbox Code Playgroud)

    我机器上使用的真实内存最多80.9mb.然后,我

    del foo
    
    Run Code Online (Sandbox Code Playgroud)

    真正的记忆力下降,但仅限于30.4mb.解释器使用4.4mb基线,那么不26mb向OS 释放内存的优势是什么?是因为Python"提前规划",认为你可能会再次使用那么多内存吗?

  2. 为什么它会50.5mb特别释放- 基于此发布的金额是多少?

  3. 有没有办法强制Python释放所有使用的内存(如果你知道你不会再使用那么多内存)?

注意 这个问题不同于我如何在Python中明确释放内存? 因为这个问题主要处理从基线增加内存使用量,即使在解释器通过垃圾收集(使用gc.collect或不使用)释放对象之后.

python memory-management

122
推荐指数
4
解决办法
12万
查看次数

为什么在django中进行大量查询(或一系列查询)后内存不会释放到系统中?

首先,DEBUG = False在settings.py中,所以不,connections['default'].queries不会增长和增长,直到它耗尽所有内存.

让我们从我用10000个用户加载User表的事实开始django.contrib.auth.models.User(每个用户名为'test#',其中#是1到10000之间的数字).

这是观点:

from django.contrib.auth.models import User
from django.http import HttpResponse

import time

def leak(request):
    print "loading users"

    users = []
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users += list(User.objects.all())
    users …
Run Code Online (Sandbox Code Playgroud)

python django memory-leaks

24
推荐指数
2
解决办法
5390
查看次数

python postgres我可以fetchall()100万行吗?

我在python中使用psycopg2模块从postgres数据库中读取,我需要对列中的所有行进行一些操作,该行有超过100万行.

我想知道会cur.fetchall()失败或导致我的服务器出现故障?(因为我的RAM可能不会那么大,无法保存所有数据)

q="SELECT names from myTable;"
cur.execute(q)
rows=cur.fetchall()
for row in rows:
    doSomething(row)
Run Code Online (Sandbox Code Playgroud)

更聪明的方法是什么?

python postgresql fetchall

18
推荐指数
3
解决办法
2万
查看次数

python - 内存没有被回馈给内核

我有一个非常简单的脚本,它分配内存,dels唯一引用一个相当大的对象,一直打印heapypidstat报告.在运行脚本之后,heapy告诉我,当pidstat告诉我相反时,不应该使用太多内存:

from guppy import hpy
import time
import sys
import os

'''
1) print heapy and pidstat report after starting and before actually doing any work
2) allocate some memory in a simple 2d array
3) print heapy and pidstat report
4) del the d2 array (attempt at garbage collection)
5) print heapy and pidstat report
6) sleep so pidstat can continue to be run to check on memory
'''

def pidstat(msg):
    print '===============================' …
Run Code Online (Sandbox Code Playgroud)

python memory-leaks memory-management python-2.7

6
推荐指数
1
解决办法
2177
查看次数