Python: Statistical system remaining memory

Source: Internet
Author: User


#!/usr/bin/python


With open ('/proc/meminfo ') as FD:

For line in FD:

If Line.startswith (' Memtotal '):

Total = Line.split () [1]

Continue

If Line.startswith (' Memfree '):

Free = Line.split () [1]

Break


freemem = Int (free)/1024.0

Totalmem = Int (total)/1024.0

Print "Freemem:" + "%.2f"% freemem+ ' M '

Print "Totalmem:" + "%.2f"% totalmem+ ' M '

Print "Freemem/totalmem:" + "%.2f"% ((FREEMEM/TOTALMEM) *100) + '% '


The results of the implementation are as follows:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/80/05/wKioL1c0sISTe-qgAAAeJ0GpVPA169.png "title=" 1.png " alt= "Wkiol1c0siste-qgaaaej0gpvpa169.png"/>

Description

1, withopen ('/proc/meminfo ') as FD: and fd=open ('/proc/meminfo ') execution of the same results , are traversing the file, the former only in Python 2.6 and later, the latter when the open file reaches a few g, it consumes memory, while the former does not have this problem.

2. String method:

Line.startswith : What starts with, returns a Boolean value (True/false)

Line.split : separated by a space or TAB key to form a list of values (strings) indexed by the list index

3,"%.2f" and "%s " are formatted string output, the former output is reserved two decimal places


This article is from the "M April Days" blog, please be sure to keep this source http://msiyuetian.blog.51cto.com/8637744/1772888

Python: Statistical system remaining memory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.