0

I'm making a Operating System in Python at the moment, just as a little project.

However; I need it to print at the start of the code how much memory is available on the host system.

I have looked this up and despite reading through walls of text, I am still unable to find anything. I have not yet attempted to compose anything because I wouldn't know where to start. I am a beginner and am relatively new to Python, so please excuse me if I sound a bit dumb.

Any suggestion or code is welcome. Thanks.

3
  • RAM or disk memory? Commented Oct 27, 2018 at 19:05
  • 1
    Possible duplicate of Get total physical memory in Python Commented Oct 27, 2018 at 19:05
  • 1
    Interestingly this would be something an Operating System would provide. Commented Oct 27, 2018 at 19:08

1 Answer 1

2

Assuming your question is about RAM, you can simply use the psutil package.

In this example I created a simple function memory_available():

import psutil
from psutil._common import bytes2human

def memory_available():
    # Get total physical memory expressed in bytes
    total_phy_memory = psutil.virtual_memory().total

    # Convert to a more readable format
    total_phy_memory_GB = bytes2human(total_phy_memory)

    # Return total physical memory expressed in gigabytes
    return total_phy_memory_GB

print(memory_available())

The psutil package is a:

Cross-platform lib for process and system monitoring in Python.

While bytes2human converts to a more readable format. In this case, Gigabytes.

Sign up to request clarification or add additional context in comments.

1 Comment

If the answer solves your problem or you believe it is the best solution. Please, up-vote and mark this answer as accepted! Thank you! If it doesn't solve your problem, let me know!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.