質問

はあるので参考にするメモリサイズPythonのデータstucture32、64-bitプラットフォーム?

ない場合は、この思いでいます。により網羅的に良好!定できるようになりました多くのバイトの使用により、以下のPython構造によって len のコンテンツタイプが関連する)?

  • int
  • float
  • 参考
  • str
  • unicode文字列
  • tuple
  • list
  • dict
  • set
  • array.array
  • numpy.array
  • deque
  • 新形式クラスオブジェクト
  • 古い形式のクラスオブジェクト
  • ...もっ忘れ!

(容器に保つだけを参照の他のオブジェクトを明らかにしたくないカウントのサイズの項目自分でも共有する必要があります。)

さらに、わかりやすく伝えるためのメモリ使用によるオブジェクト実行時に(再帰的になる?

役に立ちましたか?

解決

この上に引用し、 sys.getsizeof()に使用するました:

>>> import sys
>>> x = 2
>>> sys.getsizeof(x)
14
>>> sys.getsizeof(sys.getsizeof)
32
>>> sys.getsizeof('this')
38
>>> sys.getsizeof('this also')
48

あなたは、このアプローチを取ることができます:

>>> import sys
>>> import decimal
>>> 
>>> d = {
...     "int": 0,
...     "float": 0.0,
...     "dict": dict(),
...     "set": set(),
...     "tuple": tuple(),
...     "list": list(),
...     "str": "a",
...     "unicode": u"a",
...     "decimal": decimal.Decimal(0),
...     "object": object(),
... }
>>> for k, v in sorted(d.iteritems()):
...     print k, sys.getsizeof(v)
...
decimal 40
dict 140
float 16
int 12
list 36
object 8
set 116
str 25
tuple 28
unicode 28
<時間>

2012年9月30日

のPython 2.7(Linuxでは、32ビット):

decimal 36
dict 136
float 16
int 12
list 32
object 8
set 112
str 22
tuple 24
unicode 32

のPython 3.3(Linuxでは、32ビット)

decimal 52
dict 144
float 16
int 14
list 32
object 8
set 112
str 26
tuple 24
unicode 26
<時間>

2016年8月1日

OSX、Pythonの2.7.10(デフォルト、2015年10月23日、午後07時19分21秒)[GCC 4.2.1互換性のあるアップルLLVM 7.0.0(打ち鳴らす-700.0.59.5を)]ダーウィンの

decimal 80
dict 280
float 24
int 24
list 72
object 16
set 232
str 38
tuple 56
unicode 52

他のヒント

私は喜んでそのようなタスクのために pympler を使用してきました。これは、Pythonの多くのバージョンとの互換性だ - !特にasizeofモジュールは2.2に戻り

例えば、末端で開始しfrom pympler import asizeofでなくprint asizeof.asizeof(v)とhughdbrownの例を用いて、私は(MacOSXの10.5上のシステムのPython 2.5)参照

$ python pymp.py 
set 120
unicode 32
tuple 32
int 16
decimal 152
float 16
list 40
object 0
dict 144
str 32

明らかにそこにいくつかの近似はここですが、私は足跡の分析とチューニングのために、それは非常に有用であることがわかりました。

これらの答えがすべて収集の浅いサイズます。疑いる訪問者はこの問題が起ここに質問に答え"どれくらいの大きさでこの複雑なオブジェクトのメモリ?"

ありの答えがここに: https://goshippo.com/blog/measure-real-size-any-python-object/

のpunchline:

import sys

def get_size(obj, seen=None):
    """Recursively finds size of objects"""
    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    # Important mark as seen *before* entering recursion to gracefully handle
    # self-referential objects
    seen.add(obj_id)
    if isinstance(obj, dict):
        size += sum([get_size(v, seen) for v in obj.values()])
        size += sum([get_size(k, seen) for k in obj.keys()])
    elif hasattr(obj, '__dict__'):
        size += get_size(obj.__dict__, seen)
    elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
        size += sum([get_size(i, seen) for i in obj])
    return size

のように使用してい:

In [1]: get_size(1)
Out[1]: 24

In [2]: get_size([1])
Out[2]: 104

In [3]: get_size([[1]])
Out[3]: 184

を知りたければPythonメモリーモデルをより深くにあり、無駄のない美しいデザインがここに類似した"の合計サイズ"のスニペットのコードの一部として長く説明: https://code.tutsplus.com/tutorials/understand-how-much-memory-your-python-objects-use--cms-25609

メモリプロファイラをお試しください。 メモリプロファイラ

Line #    Mem usage  Increment   Line Contents
==============================================
     3                           @profile
     4      5.97 MB    0.00 MB   def my_func():
     5     13.61 MB    7.64 MB       a = [1] * (10 ** 6)
     6    166.20 MB  152.59 MB       b = [2] * (2 * 10 ** 7)
     7     13.61 MB -152.59 MB       del b
     8     13.61 MB    0.00 MB       return a

また、あなたはグッピーのモジュールを使用することができます。

>>> from guppy import hpy; hp=hpy()
>>> hp.heap()
Partition of a set of 25853 objects. Total size = 3320992 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  11731  45   929072  28    929072  28 str
     1   5832  23   469760  14   1398832  42 tuple
     2    324   1   277728   8   1676560  50 dict (no owner)
     3     70   0   216976   7   1893536  57 dict of module
     4    199   1   210856   6   2104392  63 dict of type
     5   1627   6   208256   6   2312648  70 types.CodeType
     6   1592   6   191040   6   2503688  75 function
     7    199   1   177008   5   2680696  81 type
     8    124   0   135328   4   2816024  85 dict of class
     9   1045   4    83600   3   2899624  87 __builtin__.wrapper_descriptor
<90 more rows. Type e.g. '_.more' to view.>

>>> hp.iso(1, [1], "1", (1,), {1:1}, None)
Partition of a set of 6 objects. Total size = 560 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0      1  17      280  50       280  50 dict (no owner)
     1      1  17      136  24       416  74 list
     2      1  17       64  11       480  86 tuple
     3      1  17       40   7       520  93 str
     4      1  17       24   4       544  97 int
     5      1  17       16   3       560 100 types.NoneType

ご利用の際は、dir([オブジェクト"組込み機能を使用する sizeof 内蔵機能です。

>>> a = -1
>>> a.__sizeof__()
24
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top