python - 我的列表使用了多少内存?

标签 python numpy memory h5py

我正在通过以下方式将 hdf5 数据集读入内存:

import h5py
with h5py.File('file.hdf5') as f:
    a = f['data'][:]

其中“a”最多可以包含 1 亿个整数。如何查询该列表到底占用了多少内存(以 MB/GB 为单位)?

最佳答案

h5py 将(大多数)值加载为 numpy 数组。数组具有 shape 属性和 dtype

对于我的 Ipython session 中碰巧有的数组,我可以获得这些属性:

In [211]: X.shape,X.dtype
Out[211]: ((51, 13), dtype('float64'))

In [212]: X.size
Out[212]: 663

In [213]: X.size, X.itemsize
Out[213]: (663, 8)

In [214]: X.nbytes
Out[214]: 5304

Ipython whos 命令还为我提供了以下信息:

X   ndarray       51x13: 663 elems, type `float64`, 5304 bytes

X 也使用一些内存来存储像这样的属性,但大部分内存使用在数据缓冲区中,在本例中为 5304 字节长。

h5py 可能有一些附加信息;我必须检查它的文档。但这些是 numpy 的基础知识。

在 h5py 文档中,我看到数据集具有形状、大小和数据类型。我没有看到 nbytes 或 itemsize。您可能必须推断这些。

<小时/>

对于一个小示例文件,我得到(在 Ipython session 中)

In [262]: y
Out[262]: <HDF5 dataset "y": shape (10,), type "<i4">
In [265]: y1=f['y'][:]

以及 whos 条目:

y             Dataset     <HDF5 dataset "y": shape (10,), type "<i4">
y1            ndarray     10: 10 elems, type `int32`, 40 bytes

y1 是一个 ndarray ,具有我描述的所有属性。卸载后的 y 没有 nbytes,但可以根据 shape 和 dtype 计算得出。

关于python - 我的列表使用了多少内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35996315/

相关文章:

python - 从 flask-admin 的模型 View 中删除/隐藏/删除 "with selected" View

numpy - numpy 中的 flatten 和 ravel 有什么区别?

c - 将变量放在特定地址会生成大型二进制文件

javascript - 对象引用会占用额外的内存吗?

Python:在 Pandas 数据框中获取位置

python - 从 Pandas /字典创建交互式层次结构图

python - 无法安装 OpenCV python3.8

python - 尝试在 Windows 7 中的 Anaconda(python) 上安装 pymc 并出现奇怪的错误?

python - 通过无循环的 bool 索引数组的 bool 索引数组

performance - 排序算法的内存速度权衡