估计最大 numpy 数组大小以适应内存

问题描述 投票:0回答:1

我有以下代码来估计新的 numpy 数组是否适合内存:

import numpy as np
from subprocess import Popen, PIPE

def getTotalMemory():
    output, _= Popen(r'wmic ComputerSystem get TotalPhysicalMemory', stdout=PIPE).communicate()
    val = ''.join([c for c in str(output) if c.isdigit()])
    return int(val)

def getFreeMemory():
    output, _= Popen(r'wmic OS get FreePhysicalMemory', stdout=PIPE).communicate()
    val = ''.join([c for c in str(output) if c.isdigit()])
    # FreePhysicalMemory returns kilobytes, hence the multiplication with 1024
    return 1024 * int(val)

def estimateNumpySize(dim, square = False, nptype = np.float64):
    # Make sure that the actual nptype is an np.dtype
    nptype = np.dtype(nptype) if not isinstance(nptype, np.dtype) else nptype
    
    # Determine the number of elements
    numElem = dim if not square else dim * dim

    # Return the value
    return numElem * nptype.itemsize 
    
# Get the memory sizes
freeMemory = getFreeMemory()
print('Total memory: {0}'.format(getTotalMemory()))
print('Free memory: {0}'.format(freeMemory))

for s in [3, 4, 5, 6, 7]:
    estSize = estimateNumpySize(10**s, True)
    print('\nEstimated array size: {0}'.format(estSize))
    print('Will it fit?: {0}'.format(freeMemory > estSize))
    try:
        dummy = np.zeros((10**s, 10**s))
        print('It fits! \nEstimated size equals actual size: {0}'.format(estSize == dummy.nbytes))
    except MemoryError:
        print('Array to large to fit in memory')

我使用 python 2.7 和 python 3.7 得到的输出如下:

Total memory: 34276392960
Free memory: 23102312448

Estimated array size: 8000000
Will it fit?: True
It fits!
Estimated size equals actual size: True

Estimated array size: 800000000
Will it fit?: True
It fits!
Estimated size equals actual size: True

Estimated array size: 80000000000
Will it fit?: False
It fits!
Estimated size equals actual size: True

Estimated array size: 8000000000000
Will it fit?: False
Array to large to fit in memory

Estimated array size: 800000000000000
Will it fit?: False
Array to large to fit in memory

这或多或少符合我的预期,除了第三种情况显然不适合内存,但创建毕竟没有问题。我想知道为什么会这样?

两个内存函数返回的数字与资源监视器中显示的数字一致。

我知道我并不严格需要事前检查,因为我可以使用 Try-Except 块捕获创建错误,但我认为事前检查更干净

python python-3.x python-2.7 numpy numpy-ndarray
1个回答
0
投票

这可能是因为 numpy 处理内存分配的方式或您的操作系统。让我解释一下,要么 numpy 使用不同的内存分配策略,例如以块的形式分配内存,要么您的操作系统正在分配虚拟空间而不是物理位置,或者您的操作系统允许称为“过度使用”的东西,这意味着它说您可以存储,但是当您尝试访问它但无法访问它。

© www.soinside.com 2019 - 2024. All rights reserved.