python编写代码避免内存增加,如何减少Python脚本内存使用量

I have a very large python script, 200K, that I would like to use as little memory as possible. It looks something like:

# a lot of data structures

r = [34, 78, 43, 12, 99]

# a lot of functions that I use all the time

def func1(word):

return len(word) + 2

# a lot of functions that I rarely use

def func1(word):

return len(word) + 2

# my main loop

while 1:

# lots of code

# calls functions

If I put the functions that I rarely use in a module, and import them dynamically only if necessary, I can't access the data. That's as far as I've gotten.

I'm new at python.

Can anyone put me on the right path? How can I break this large script down so that it uses less memory? Is it worth putting rarely used code in modules and only calling them when needed?

解决方案

Organazing:

Your python script seems indeed to be huge, maybe you should consider reorganizing your code first, to split in into several modules or packages. It will probably make easier the code profiling and the optimization tasks.

You may want to have a look there:

And possibly:

Optimizing:

There is a lot of things that can be done for optimizing your code ...

For instance, regarding your data structures ... If you make a big use of lists or lists comprehensions, you could try to figure out where do you really need lists, and where they might be replaced by non-mutable data structures like tuples or by "volatile" objects, "lazy" containers, like generator expressions.

See:

On these pages, you could find some useful information and tips:

Also, you should study your ways for doing things and wonder whether there is a way to do that less greedily, a way that it's better to do it in Python (you will find some tips in the tag pythonic) ... That is especially true in Python, since in Python, there is often one "obvious" way (and only one) to do things which is better than the others (see The Zen of Python), which is said to be pythonic. It's not especially related to the shape of your code, but also - and above all - to the performances. Unlike a lot of languages, which promote the idea that there should be many ways for doing anything, Python prefers to focus on the best way only. So obviously, there are many ways for doing something, but often, one is really better.

Now, you should also verify whether you are using the best methods for doing things, because pythonicality won't arrange your algorithms for you.

But at last, it's very depending on your code it's difficult to answer without having seen it.

And, make sure to take into account the comments made by eumiro and Amr.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编写服务器硬件状态巡检脚本可以使用Python的第三方库psutil来获取服务器硬件信息和状态。以下是一个简单的示例脚本: ```python import psutil # 获取CPU信息 cpu_percent = psutil.cpu_percent() cpu_count = psutil.cpu_count() cpu_freq = psutil.cpu_freq() # 获取内存信息 mem = psutil.virtual_memory() mem_total = mem.total mem_used = mem.used mem_percent = mem.percent # 获取磁盘信息 disk = psutil.disk_usage('/') disk_total = disk.total disk_used = disk.used disk_percent = disk.percent # 获取网络信息 net = psutil.net_io_counters() net_bytes_sent = net.bytes_sent net_bytes_recv = net.bytes_recv # 打印硬件状态信息 print("CPU使用率:{}%".format(cpu_percent)) print("CPU核心数:{}".format(cpu_count)) print("CPU频率:{}MHz".format(cpu_freq.current)) print("内存总量:{}B".format(mem_total)) print("内存使用量:{}B".format(mem_used)) print("内存使用率:{}%".format(mem_percent)) print("磁盘总量:{}B".format(disk_total)) print("磁盘使用量:{}B".format(disk_used)) print("磁盘使用率:{}%".format(disk_percent)) print("网络发送字节数:{}B".format(net_bytes_sent)) print("网络接收字节数:{}B".format(net_bytes_recv)) ``` 通过使用psutil库可以轻松地获取服务器的CPU使用率、内存状态、磁盘空间以及网络流量等信息,并对其进行监控和巡检。根据具体需求,我们可以进一步处理和记录这些信息,也可以设置定期巡检并发送邮件或生成报告等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值