V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
just1
V2EX  ›  Python

web.py 内存占用过高

  •  
  •   just1 · May 6, 2016 via Android · 3544 views
    This topic created in 3645 days ago, the information mentioned may be changed or developed.
    一个很简单的交互。
    本地有一个 json 文件。
    客户端发送一个字符串,服务器接受,读入 json ,遍历对象查找哪一项包含这个字符串,并把这一项输出。
    今天发现内存占用很高, top 查一下发现这个脚本花了 250mb 内存。。而且查询越多次内存越大。
    这是怎么回事呢?该如何解决
    web.py python2
    6 replies    2016-05-06 13:20:58 +08:00
    YUX
        1
    YUX  
    PRO
       May 6, 2016
    有没有用 flask 也试一下?
    just1
        2
    just1  
    OP
       May 6, 2016 via Android
    @YUX 另一个用 tornado 的很正常。
    主要是不想重写代码。。
    justou
        3
    justou  
       May 6, 2016   ❤️ 1
    尝试用 memory_profiler 做一下内存分析呢?
    py 采用的对象缓冲池, 垃圾回收不是立即的, 有些时候会被缓存一段时间.
    曾经在多线程中下载文件, 发现如果用 with 语句打开文件写入后, 即使出了 with 的作用域, 被写入的 buffer 也不会被立即释放, 结果内存会飙升, 显式的 open, close 就没问题了
    mulog
        4
    mulog  
       May 6, 2016   ❤️ 1
    可以用 objgraph 做做 profile 看一下是什么东西占着内存
    SlipStupig
        5
    SlipStupig  
       May 6, 2016   ❤️ 1
    方法有几种:
    1.分析占用原因, python 采用分代回收,活的越久的对象越不会被回收,所以看一下 profile 状态
    2.不用分析,定时 del 对象和强制 GC ,但是这么做会降低性能
    just1
        6
    just1  
    OP
       May 6, 2016 via Android
    @justou
    @mulog
    @SlipStupig 感谢,等会坐下测试
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1000 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 21:56 · PVG 05:56 · LAX 14:56 · JFK 17:56
    ♥ Do have faith in what you're doing.