【发布时间】:2018-08-21 19:43:05
【问题描述】:
我的 python 应用程序在启动期间从文件中读取数据并将这些数据存储在字典中(字典是数据读取器类的属性)。一旦应用程序启动并使用了读取的数据,就不再需要字典中的这些数据。但是,它们会消耗大量内存。如何删除这些字典以释放内存?
例如:
class DataReader():
def __init__(self, data_file):
self.data_file = data_file
def read_data_file_and_store_data_in_dictionary():
self.data_dictionary = {}
for [data_name, data] in self.data_file:
self.data_dictionary[data_name] = data
class Application():
def __init__(self, data_file):
self.data_reader = DataReader()
self.data_reader.read()
def start_app(self):
self.use_read_data()
应用程序启动后,不再需要self.data_dictionary。如何永久删除self.data_dictionary?
【问题讨论】:
-
当不再需要时,只需发送
self.data_reader = None。 -
@martineau 真的有用吗?
del不需要吗? -
是的,它确实有效,所以
del不是必需的。 Python中的对象是引用计数的,当它达到零值时会自动删除。