V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
qazwsxkevin
V2EX  ›  Python

如何可以得知,运行的某个时候,产生的目标字典占用了多少内存?

  •  
  •   qazwsxkevin · 2020-01-15 20:44:07 +08:00 · 2408 次点击
    这是一个创建于 1554 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如想得知,在函数运行后,return 回来的 dict 占了多少 MB 内存这样?
    6 条回复    2020-01-18 12:25:39 +08:00
    casparchen
        1
    casparchen  
       2020-01-15 20:46:56 +08:00   ❤️ 1
    你这个标题对仗不整齐啊,逗号少一个。

    如何才可以得知,
    运行的某个时候,
    产生的目标字典,
    占用了多少内存。

    看看这样是不是好多了 ?
    si
        2
    si  
       2020-01-15 20:51:18 +08:00
    我搜索了一下:
    Python 获取对象所占内存大小方法:
    sys.getsizeof()
    内置函数__sizeof__()
    iamwho
        3
    iamwho  
       2020-01-15 20:51:53 +08:00 via iPhone
    sys.getsizeof()

    ?
    qazwsxkevin
        4
    qazwsxkevin  
    OP
       2020-01-15 22:59:53 +08:00
    @casparchen 最后一个逗号可以不必吧。。。

    @si 我第一认知也是这个,只是这个似乎是不准确。。。

    @iamwho 这个似乎是不准确的。。。

    看了一晚网上关于这方面的各种说法,问的这个问题,似乎在 Python 上无解。。。
    不过不重要了,打算把磁盘上 53MB 的内容放入字典传来传去了
    iamwho
        5
    iamwho  
       2020-01-16 00:32:13 +08:00
    @qazwsxkevin #4

    Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.

    Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

    https://docs.python.org/3/library/sys.html#sys.getsizeof
    yucongo
        6
    yucongo  
       2020-01-18 12:25:39 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5383 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 08:27 · PVG 16:27 · LAX 01:27 · JFK 04:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.