我試圖在numpy中創建高效的廣播數組,例如一組只有1000個元素的shape=[1000,1000,1000]
數組,但重複1e6次。這可以通過np.lib.stride_tricks.as_strided
和np.broadcast_arrays
來實現。內存中numpy跨步數組/廣播數組的大小?
但是,我無法驗證內存中是否有重複,這很關鍵,因爲實際上在內存中複製陣列的測試往往會導致我的機器無法回溯。
我已經試過檢查使用.nbytes
數組的大小,但是這似乎並不符合實際使用的內存:
>>> import numpy as np
>>> import resource
>>> initial_memuse = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
>>> pagesize = resource.getpagesize()
>>>
>>> x = np.arange(1000)
>>> memuse_x = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
>>> print("Size of x = {0} MB".format(x.nbytes/1e6))
Size of x = 0.008 MB
>>> print("Memory used = {0} MB".format((memuse_x-initial_memuse)*resource.getpagesize()/1e6))
Memory used = 150.994944 MB
>>>
>>> y = np.lib.stride_tricks.as_strided(x, [1000,10,10], strides=x.strides + (0, 0))
>>> memuse_y = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
>>> print("Size of y = {0} MB".format(y.nbytes/1e6))
Size of y = 0.8 MB
>>> print("Memory used = {0} MB".format((memuse_y-memuse_x)*resource.getpagesize()/1e6))
Memory used = 201.326592 MB
>>>
>>> z = np.lib.stride_tricks.as_strided(x, [1000,100,100], strides=x.strides + (0, 0))
>>> memuse_z = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
>>> print("Size of z = {0} MB".format(z.nbytes/1e6))
Size of z = 80.0 MB
>>> print("Memory used = {0} MB".format((memuse_z-memuse_y)*resource.getpagesize()/1e6))
Memory used = 0.0 MB
所以陣列的.nbytes
報告的「理論」的大小,但顯然不是實際的大小。 resource
檢查有點尷尬,因爲看起來好像有些東西被緩存(可能?),導致第一次跨越佔用一些內存,但未來的跨步不需要。
tl; dr:你如何確定內存中numpy數組或視圖的實際大小?
只要找到了Python的駐留集大小進程不是確定特定numpy陣列使用多少內存的可靠方法。它不會將分頁考慮進去,並且沒有簡單的方法可以預測分配給已刪除或超出範圍的陣列的內存何時會被釋放回操作系統。 –