使用python列表中的对象减少内存消耗
我目前正在使用Docker compose和numpy在Django中填充PostGIS数据库 目前的问题是,我不确定是什么消耗了这么多内存:使用python列表中的对象减少内存消耗,python,django,numpy,memory,Python,Django,Numpy,Memory,我目前正在使用Docker compose和numpy在Django中填充PostGIS数据库 目前的问题是,我不确定是什么消耗了这么多内存: tile_density = int(map_height * map_width / settings.NUM_OF_USERS) root2 = sqrt(2) map_radius = map_width / (2 * root2) lat_range = 90 / map_width # 180 / map_width
tile_density = int(map_height * map_width / settings.NUM_OF_USERS)
root2 = sqrt(2)
map_radius = map_width / (2 * root2)
lat_range = 90 / map_width # 180 / map_width / 2
lon_range = 90 / map_height # 180 / map_height / 2
print("Beginning migration creation")
# Begin migration based on shade
for i, y in enumerate(data):
# y starts from top then goes to bottom
# We have to imagine the current numpy array is like the coordinate system
# Then translate it when creating user locations
temp_users = []
for x in y:
# x is now a tile. x starts from left and moves to right
# Points go up to 14 decimal places
if x in (-200.0, 0.0): # No data or no people
continue
# Calculate the 4 Points as boundaries for these users' coordinates.
theta = np.arcsin(i / map_radius / root2)
temp = np.arcsin((2 * theta + np.sin(2 * theta)) / pi)
lat_range = (
temp - lat_range,
temp + lat_range
)
temp = pi * x / 2 / map_radius / root2 * np.cos(theta)
lon_range = (
temp - lon_range,
temp + lon_range
)
# Add user
for user in range(int(tile_density * x)): # Number of users in tile
temp_users.append(
User(
username=uuid4(),
password=make_password("test", None, "md5"),
first_name=get_first_name(),
last_name=get_last_name(),
location=Point(
uniform(lon_range[0], lon_range[1]),
uniform(lat_range[0], lat_range[1])
)
)
)
# Bulk add users to User model
User.objects.bulk_create(temp_users)
temp_users.clear()
变量data
是一个带有dtype Float32的numpy数组。随着迭代的继续(从10GB内存使用开始),内存从10GB快速增加到12GB,迫使docker以代码137退出
我不知道到底是什么在增加内存使用量。在底部,您将看到用户(用户名等);User是我添加到列表temp\u users
中的一个对象,Django将把它批量创建到PostGIS数据库中。据推测,内存泄漏正在那里发生,但我打印了sys.getsizeof(temp_users)
,而且我从未获得额外的内存,因为它在for循环中循环使用
那么到底是什么导致了这里的内存过载呢?Float32 numpy数组的形状约为3600072143(不记得了)。不看代码,您描述的数组需要(36000*72143*4*1e-9)GB,所以大约10GB@juanpa.arrivillaga是的,但是为什么内存的使用总是在增加呢?循环的一次迭代完成后,它不应该波动吗?