• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

使用python实现内存监控


This Wind

Recommended Posts

1、memory_profiler可以监控代码的内存消耗及增长量,以下面的代码为例。

1577498920-40955b91b039c31.png

发现在for循环里增加了0.3MB,这个工具可以帮助我们定位内存泄露的问题。

1577498923-40955b91b039c31.png

2、profile与cProfile可以监控代码的CPU占用,看下下面的代码。

1577498927-40955b91b039c31.png

结果如下,ncalls是调用次数,cumtime可以看到累计时间,包含子函数,tottime不包含子函数,可以看到总共占用cpu0.049秒,test用了0.038,test_1用例0.01秒,在上面的代码里,我们使用了sleep,但是看结果,可以确定sleep并没有占用cpu。

1577498930-40955b91b039c31.png

下面是去掉sleep的情况,test_1执行稍快了一点点,说明虽然不使用CPU,但是cpu的占用与释放还是有损耗的,只是不明显。

1577498934-40955b91b039c31.png

3、cProfile和profile类似,下面是一种常用的用法。

1577498938-40955b91b039c31.png

可以根据时间排序。

1577498941-40955b91b039c31.png

原文地址:https://www.cnblogs.com/small-office/p/10553460.html

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now