如何解决读取h5文件时释放内存
我有一堆h5文件,每个文件的大小约为200 GB。这些文件的结构如下:
file1.h5
├image [float64: 3341 × 126 × 256 × 256]
├pulse [uint64: 126]
└train [uint64: 3341]
def read_h5(file_name,pulse_avg=True,train_idx=0,pulse_idx=0):
"""
Read image data from a h5 file to a xarray
"""
hf = h5py.File(file_name,'r')
if not pulse_avg:
coords = {'train': np.array(hf.get(f'train')),'pulse': np.array(hf.get(f'pulse')),}
dims = ['train','pulse','slow_scan','fast_scan']
xarr = xr.DataArray(np.array(hf.get(f'image')),dims=dims,coords=coords)
del hf
return xarr.isel(train=train_idx).isel(pulse=pulse_idx)
else:
coords = {'train': np.array(hf.get(f'train'))}
dims = ['train',coords=coords)
del hf
return xarr
读取整个文件时,由于对象很大,因此内存使用情况符合预期:
dummy = images_from_disk('file1.h5',pulse_avg=False,train_idx=slice(None),pulse_idx=slice(None))
dummy.nbytes * (2 ** -30)
205.2626953125
读取前使用的内存:
total used free shared buff/cache available
Mem: 754G 41G 697G 18M 15G 710G
Swap: 4.0G 132M 3.9G
读取后使用的内存:
total used free shared buff/cache available
Mem: 754G 247G 491G 18M 15G 504G
Swap: 4.0G 132M 3.9G
但是,如果我读取相同的文件但保留较小的版本(只有两个脉冲而不是126个脉冲),则对象的大小显然会较小,但不会释放内存:
dummy_reduced = images_from_disk('file1.h5',pulse_idx=slice(None,2))
dummy_reduced.nbytes * (2 ** -30)
3.2626953125
读取前使用的内存:
total used free shared buff/cache available
Mem: 754G 41G 697G 18M 15G 710G
Swap: 4.0G 132M 3.9G
读取后使用的内存:
total used free shared buff/cache available
Mem: 754G 247G 491G 18M 15G 504G
Swap: 4.0G 132M 3.9G
如何释放内存以连接三个以上的h5文件? 完成这项工作的代码将类似于以下内容:
test = xr.concat([images_from_disk(file,train_idx=slice(None,10),2)) for file in my_files],pd.Index([int(file.stem[-2:]) for file in my_files],name='module'))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。