【发布时间】:2017-05-03 05:18:45
【问题描述】:
# encoding: utf-8
import sys
import commands
import time
import gc
import numpy
process=sys.argv[0]
def get_use_memory():
global process
return commands.getstatusoutput('ps aux | grep "{0}" | grep -v "grep"'.format(process))
def normalize_feature(node, delete_list):
print 'normalize_feature_step1', get_use_memory()
normal_features = []
for i in range(0, node.shape[0]):
feature_numpy = node[i, :]
feature_numpy_d = numpy.delete(feature_numpy, delete_list, axis=0)
normal_features.append(feature_numpy_d)
del feature_numpy
del feature_numpy_d
print 'normalize_feature_step2', get_use_memory()
np_normal_features = numpy.array(normal_features)
print sys.getsizeof(np_normal_features) / float(1024) / float(1024)
print 'normalize_feature_step3', get_use_memory()
del normal_features
gc.collect()
print 'normalize_feature_step4', get_use_memory()
return np_normal_features
#gc.set_debug(gc.DEBUG_STATS|gc.DEBUG_LEAK)
rows=1024
columns=10240
a = []
for i in range(0, rows):
b = []
for j in range(0, columns):
b.append(float(i) * j)
a.append(b)
del b
print get_use_memory()
node_1 = numpy.array(a)
print sys.getsizeof(node_1) / float(1024) / float(1024)
print get_use_memory()
del a
gc.collect()
print get_use_memory()
node_2 = normalize_feature(node_1, [0, 100, 1000])
print sys.getsizeof(node_2) / float(1024) / float(1024)
print get_use_memory()
del node_1
del node_2
gc.collect()
print get_use_memory()
输出:
(0, 'wangye 5319 96.5 1.0 581036 360528 pts/28 S+ 11:23 0:03 python test.py')
80.0001068115
(0, 'wangye 5319 106 1.3 662964 442456 pts/28 S+ 11:23 0:04 python test.py')
(0, 'wangye 5319 112 0.2 316812 98072 pts/28 S+ 11:23 0:04 python test.py')
normalize_feature_step1 (0, 'wangye 5319 112 0.2 316812 98072 pts/28 S+ 11:23 0:04 python test.py')
normalize_feature_step2 (0, 'wangye 5319 115 0.5 398372 179704 pts/28 S+ 11:23 0:04 python test.py')
79.9766693115
normalize_feature_step3 (0, 'wangye 5319 116 0.7 480272 261596 pts/28 S+ 11:23 0:04 python test.py')
normalize_feature_step4 (0, 'wangye 5319 116 0.5 398688 180148 pts/28 S+ 11:23 0:04 python test.py')
79.9766693115
(0, 'wangye 5319 116 0.5 398688 180148 pts/28 S+ 11:23 0:04 python test.py')
(0, 'wangye 5319 117 0.0 234864 16324 pts/28 S+ 11:23 0:04 python test.py')
在 normalize_feature_step3 和 normalize_feature_step4 之间释放 80M 内存。因为 del normal_features 发布了它的项目,即 numpy.ndarray。而最终内存只有16M。
但是当我将代码的第 38 行和第 39 行更改为: 行=10240 列=1024
输出:
(0, 'wangye 5400 99.5 1.1 604944 385888 pts/28 S+ 11:25 0:03 python test.py')
80.0001068115
(0, 'wangye 5400 109 1.4 686872 467892 pts/28 S+ 11:25 0:04 python test.py')
(0, 'wangye 5400 116 0.2 317024 98176 pts/28 S+ 11:25 0:04 python test.py')
normalize_feature_step1 (0, 'wangye 5400 116 0.2 317024 98176 pts/28 S+ 11:25 0:04 python test.py')
normalize_feature_step2 (0, 'wangye 5400 100 0.5 399592 180852 pts/28 S+ 11:25 0:05 python test.py')
79.7657318115
normalize_feature_step3 (0, 'wangye 5400 101 0.8 481276 262576 pts/28 S+ 11:25 0:05 python test.py')
normalize_feature_step4 (0, 'wangye 5400 101 0.7 480444 261904 pts/28 S+ 11:25 0:05 python test.py')
79.7657318115
(0, 'wangye 5400 101 0.7 480444 261904 pts/28 S+ 11:25 0:05 python test.py')
(0, 'wangye 5400 101 0.2 316836 98296 pts/28 S+ 11:25 0:05 python test.py')
内存在 normalize_feature_step3 和 normalize_feature_step4 之间没有任何变化。最终内存为98M。
所以我认为 numpy 可能会处理一些内存。我想知道如何释放内存。 谢谢!
【问题讨论】: