Python内存分配-可以释放它吗?
我提出了一个问题,在这个问题上,我在释放python中使用的某个变量的内存时遇到了一些问题。尝试了很多东西,最后我尝试在代码中使用内存分析器来查看内存分配到哪里 这是内存探查器的输出:Python内存分配-可以释放它吗?,python,memory,memory-management,Python,Memory,Memory Management,我提出了一个问题,在这个问题上,我在释放python中使用的某个变量的内存时遇到了一些问题。尝试了很多东西,最后我尝试在代码中使用内存分析器来查看内存分配到哪里 这是内存探查器的输出: Line # Mem usage Increment Line Contents ================================================ 30 28.523 MiB 0.000 MiB @profile 31
Line # Mem usage Increment Line Contents
================================================
30 28.523 MiB 0.000 MiB @profile
31 def store_file(file_name,data):
32 29.027 MiB 0.504 MiB with h5py.File(file_name,'w',driver='core',backing_store=False) as f:
33 28.520 MiB -0.508 MiB dset = f.create_dataset("mydataset", (100,) ,dtype='i')
Filename: array.py
Line # Mem usage Increment Line Contents
================================================
35 24.656 MiB 0.000 MiB @profile
36 def load_data_overlap(saved):
37 #os.chdir(numpy_train)
38 24.656 MiB 0.000 MiB print "Inside function!..."
39 24.656 MiB 0.000 MiB if saved == False:
40 25.531 MiB 0.875 MiB train_files = np.random.randint(255,size=(1,40,690,4))
41 25.531 MiB 0.000 MiB train_input_data_interweawed_normalized = []
42 #print "Storing train pic to numpy"
43 25.531 MiB 0.000 MiB part = 0
44 28.523 MiB 2.992 MiB for i in xrange(10):
45 28.523 MiB 0.000 MiB print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
46 28.523 MiB 0.000 MiB if resource.getrusage(resource.RUSAGE_SELF).ru_maxrss > 27033600:
47 28.523 MiB 0.000 MiB print "Max ram storing part: " + str(part) + " At entry: " + str(i)
48 #print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
49 #print "Storing Train input"
50 28.523 MiB 0.000 MiB file_name = 'train_input_'+'part_'+str(part)+'_'+str(dim)+'_'+str(total_frames_with_deltas)+'_window_height_'+str(window_height)+'.h5'
51 27.492 MiB -1.031 MiB store_file(file_name,train_input_data_interweawed_normalized)
52 #print getsizeof(train_input_data_interweawed_normalized)
53 #print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
54 27.492 MiB 0.000 MiB part = part + 1
55 27.492 MiB 0.000 MiB del train_input_data_interweawed_normalized
56 27.492 MiB 0.000 MiB gc.collect()
57 #print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
58 27.492 MiB 0.000 MiB train_input_data_interweawed_normalized = []
59 #print getsizeof(train_input_data_interweawed_normalized)
60 #print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
61 #raw_input("something")
62 28.523 MiB 1.031 MiB for plot in train_files:
63 28.523 MiB 0.000 MiB overlaps_reshaped = np.random.randint(10,size=(45,200,5,3))
64 28.523 MiB 0.000 MiB for ind_plot in overlaps_reshaped.reshape(overlaps_reshaped.shape[1],overlaps_reshaped.shape[0],overlaps_reshaped.shape[2],overlaps_reshaped.shape[3]):
65 28.523 MiB 0.000 MiB ind_plot_reshaped = ind_plot.reshape(ind_plot.shape[0],1,ind_plot.shape[1],ind_plot.shape[2])
66 28.523 MiB 0.000 MiB train_input_data_interweawed_normalized.append(ind_plot_reshaped)
67 28.523 MiB 0.000 MiB print len(train_input_data_interweawed_normalized)
内存被分配到变量中,变量总是被清除的,但是ram的使用从未被释放
然后,我试着想象随着时间的推移内存的使用情况,并看到了这一点:
内存使用情况似乎从未恢复。。看起来python代码已经动态分配了必要的内存,并且从未返回过。。
好像“代码认为”——它需要那么多内存,因为变量以前使用过这么多内存。所以做一些类似向量的事情会增加大小,但不会减少
理解正确吗?python就是这样管理内存的吗?
我试着用谷歌搜索这个主题,但大多数帖子都建议使用
gc.collect()
——因此建议这应该是可能的 请指出哪个var
行“始终被清除”。请指出哪个var
行“始终被清除”。