Python 内存问题求教

2020-10-17 21:26:43 +08:00
 JCZ2MkKb5S8ZX9pq
# 伪代码
from psd_tools import PSDImage

def main():
    for file in file_list:
        save_jpg(file)
        
def save_jpg(file):
    img = PSDImage.open(file).composite()
    img.save(output_name)

1796 次点击
所在节点    Python
5 条回复
sss495088732
2020-10-17 22:54:22 +08:00
看源码有没有释放句柄
xchaoinfo
2020-10-17 23:44:43 +08:00
with PSDImage.open
ipwx
2020-10-17 23:45:57 +08:00
1. 请及时关闭不需要的东西。
2. 32 位程序只能用 2GB 用户态内存。请确信你用的不是 32 位 python 。
laike9m
2020-10-18 00:07:47 +08:00
你需要的不是猜测,而是 profiling,推荐几个库吧:
https://github.com/pythonprofilers/memory_profiler
https://mg.pov.lt/objgraph/
nonduality
2020-10-18 09:56:39 +08:00
假如你尝试这么写呢?

```python
def save_jpg(file):
fd = PSDImage.open(file)
fd.composite().save(output_name)
fd.close()
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/715997

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX