- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在下面显示的函数中,我想在使用后从内存中清除 x
的值。
def f(x, *args):
# do something
y = g(x) # here i want to use x as argument and clear value of x from ram
# do something
我尝试了以下方法并使用 memory_profiler
检查了内存使用情况,但没有任何效果:
del x
x = 无
我尝试过的示例代码:
%%file temp.py
import lorem
@profile
def f(x, use_none=True):
# do something
y = g(x)
if use_none:
x = None
else:
del x
# do something
def g(x):
n = len(x)
return [lorem.paragraph() * i for i in range(n)]
if __name__ == '__main__':
x = g([1] * 1000)
# f(x, True)
f(x, False)
memory_profiler
命令:
python -m memory_profiler temp.py
结果(使用None
):
Filename: temp.py
Line # Mem usage Increment Line Contents
================================================
3 187.387 MiB 187.387 MiB @profile
4 def f(x, use_none=True):
5 # do something
6 340.527 MiB 153.141 MiB y = g(x)
7 340.527 MiB 0.000 MiB if use_none:
8 340.527 MiB 0.000 MiB x = None
9 else:
10 del x
结果(使用del
):
Filename: temp.py
Line # Mem usage Increment Line Contents
================================================
3 186.723 MiB 186.723 MiB @profile
4 def f(x, use_none=True):
5 # do something
6 338.832 MiB 152.109 MiB y = g(x)
7 338.832 MiB 0.000 MiB if use_none:
8 x = None
9 else:
10 338.832 MiB 0.000 MiB del x
编辑从全局和 gc.collect() 中删除不起作用
Filename: temp.py
Line # Mem usage Increment Line Contents
================================================
4 188.953 MiB 188.953 MiB @profile
5 def f(x, use_none=True):
6 # do something
7 342.352 MiB 153.398 MiB y = g(x)
8 342.352 MiB 0.000 MiB if use_none:
9 x = None
10 globals()['x'] = None
11 gc.collect()
12 else:
13 342.352 MiB 0.000 MiB del x
14 342.352 MiB 0.000 MiB del globals()['x']
15 342.352 MiB 0.000 MiB gc.collect()
另外,我写这段代码只是为了引用,在我的实际代码中,我多次从一个函数调用另一个函数,有时在某些操作后基于某些参数值和 x 的值从内部调用同一个函数。
每次调用后,我想在一些操作后删除x。
最佳答案
假设您使用的是 CPython(也可能是其他实现),当对象的引用计数降为零时会触发垃圾回收。即使对象没有立即被垃圾回收,这也不是您看到结果的原因。原因是您不能对仍然具有强引用的对象进行垃圾回收。
del
取消绑定(bind)当前 namespace 中的名称,将引用计数减一。它实际上并没有删除任何东西。 del
是 =
的逆运算,而不是 __new__
。
将 None
或任何其他对象分配给该名称也会减少原始绑定(bind)的引用计数。唯一的区别是重新分配将名称保留在 namespace 中。
x = g([1] * 1000)
行在全局模块命名空间中创建了一个对象。然后调用 f
并将该对象绑定(bind)到 f
的本地 namespace 中的名称 x
。此时,有两个引用:一个在本地 namespace 中,一个在全局中。
您的对象在正常情况下不会消失,直到您的模块被卸载。您还可以在 f
中尝试类似以下内容:
del x
del globals()['x']
另一种方法是使用临时变量来避免在全局命名空间中赋值:
f(g([1] * 1000), False)
您传递给 f
的临时变量将在 f
返回后立即消失,即使没有 del
,因为它没有在其他地方被引用.
任何一个选项都可能需要在之后调用 gc.collect()
,但在 CPython 中不应该。
关于python - 从 RAM 中删除变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56574567/
如果我们想访问数组的元素 i,我们会这样做:i *(每个元素的大小)+ 数组的起始地址 = 我们需要访问的元素的内存位置。然后 CPU 直接访问 RAM 中的该位置。这是怎么发生的? 假设我要定位的内
考虑以下 python 代码 with open(sys.argv[2], 'r') as fin, \ open(sys.argv[3], 'w') as fout:
基本上我在 Centos 6.2 16GB 系统上运行我的世界服务器 我通过 rtoolkit 引导来启动我的服务器,现在我分配它使用 12GB,但它使用的 muc 比这多,我不知道为什么。我运行此命
我正在开发一个小型 x86 内核。我正在访问并尝试读取 GRUB 在多重引导 header 中提供的内存映射。我有一个 Intel i3 cpu 和 4 GiB 的 RAM。在这台机器上运行时,我正在
我有 3 GB 内存。有没有办法只为 C++ 应用程序分配 512MB 的 RAM? 否则 有没有办法暂时将我的 RAM 减少到 512MB 以进行测试? 谢谢,阿肖克 最佳答案 使用SetProce
我的一个 friend 告诉我,在 x86 架构上,DMA Controller 无法在两个不同的 RAM 位置之间传输。它只能在RAM和外设(如PCI总线)之间传输。 这是真的吗? 因为 AFAIK
做一些分析(内存和速度) 我一直被 win7 似乎分配的 RAM 正好是我要求的两倍这一事实所困扰...注意这是我第一次在 win7 上进行这样的主动分析,所以我真的不知道会发生什么。 我在 win7
我经常玩 retrofit 版的 Minecraft。这样做的一个缺点是,每当我启动 Minecraft 时,编译所有模组都需要花费大量时间。这可能需要大约 15 分钟左右的时间,在我看来,这太长了。
我想编写一个内核模块,可以通过 DMA 将数据从 RAM 传输到 RAM。有一些帖子讨论这个,但我真的不明白。有人说可能,也有人说不可能。 如果我对 ldd3 的理解是正确的,RAM 到 RAM 的复
我正在寻找一种方法来查找总内存和正在使用的内存的单个熟值输出。 gwmi Win32_OperatingSystem | select TotalVisibleMemorySize, FreePhys
我在 Heroku 上有一个网络应用程序,我试图了解添加具有 1GB RAM 的 Memcached 实例与向我的 Postgres 服务器添加 1GB RAM 之间的区别/权衡。 如果我添加一个 M
在我的例子中,我有一个包含大约 6000 个实例化类的字典,其中每个类都有 1000 个属性变量,所有类型都是字符串或字符串列表。当我建立这本字典时,我的 RAM 变得非常高。有没有一种方法可以在将字
我的问题是:如果您用完了所有可用的视频内存,并尝试创建新纹理 (SDL),是否会自动使用普通内存而不是视频内存?或者,您是否必须尝试使用使用普通 ram 的表面 (SDL)?如果您出于任何原因无法
我在从 ram 地址获取值时遇到问题。 我有一些限制,即它必须是 C 代码,并且存储信息的地址由 u32 值给出,例如 0x001c0080。 环境是ARM的,我用的是eclipse sdk。我怎样才
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我正在尝试使用 GCC 编译器(标准 C)编译裸机应用程序。我使用 Cyclone V SoC 和 Cortex-A9 处理器。 eclipse DS-5。我收到这些错误 - “Region ram
如果 RAM 不是问题(我的服务器上有接近 200GB),是逐行读取更快还是将所有内容读入 RAM 并访问它?每行将是大约 200-500 个 unicode 字符的字符串。每个文件有近 200 万行
我在我的两台机器上运行以下命令: import os, sqlite3 import pandas as pd from feat_transform import filter_anevexp db
我正在尝试在我的一个项目中引入一些 CUDA 优化。但我认为我在这里做错了什么。我想实现一个简单的矩阵 vector 乘法 (result = matrix * vector)。但是当我想将结果复制回
#include #include #include #include #include #include using namespace std; char randomLetter()
我是一名优秀的程序员,十分优秀!