分析 python 个生成器中的内存使用情况
Profiling memory usage in python generators
我想弄清楚我的内存在 python 中的生成器函数中的什么位置被使用了。我试过 memprof 和 memory_profiler,但都没有给我关于以下内容的信息发电机。
如何确定我在生成器函数中分配的内存位置和大小?
以下代码片段应该可以在 python3 中使用。如果将生成器函数替换为列表,您会发现内存分配有很大差异。
from memory_profiler import memory_usage
print(f'Memory usage after: {memory_usage()}MB')
def obj_generator(num_objects):
for i in range(num_objects):
new_obj = {
'id' : i,
}
yield new_obj
objects = obj_generator(1_000_000)
print(f'Memory usage before: {memory_usage()}MB')
我想弄清楚我的内存在 python 中的生成器函数中的什么位置被使用了。我试过 memprof 和 memory_profiler,但都没有给我关于以下内容的信息发电机。
如何确定我在生成器函数中分配的内存位置和大小?
以下代码片段应该可以在 python3 中使用。如果将生成器函数替换为列表,您会发现内存分配有很大差异。
from memory_profiler import memory_usage
print(f'Memory usage after: {memory_usage()}MB')
def obj_generator(num_objects):
for i in range(num_objects):
new_obj = {
'id' : i,
}
yield new_obj
objects = obj_generator(1_000_000)
print(f'Memory usage before: {memory_usage()}MB')