Learn System Programming: Build a RAM Manager in Python

Build a Python RAM manager using psutil that monitors memory usage, lists top processes, and kills memory hogs. The script uses argparse for command-line flags, alerts when usage exceeds 85%, and handles permission errors gracefully. Useful for debugging leaks and managing heavy workloads like Docker or compilation.
Chapters
So for creating this, what you need is you need one dependency which is Psutil library. Psutil library is basically a cross-platform library that basically helps you read and process some of the information. So very first library that we need which is very important for this project is Psutil library. Now this is a cross-platform, it means memory, CPU, disk, and even the network, it can directly from the OS, it can automatically take it. So if you wanted to simply install it, just write down the pip command for this, like pip install psutil. This is how you will write it, and you can easily install it. You can open the terminal in your Visual Studio Code, uh, to install this, or either like you can use your PowerShell or the command prompt as well. So that's it. Everything else is the standard Python which we are going to use in this project. I will be showing you the complete code and I will be explaining each and every line of the code in this particular file as well. And also I will give you the Python file which you can use for the reference to create the perfect project for you. Now when I talk about the code, the very first thing that you have to do is you have to import the libraries which are required. So I'm using the 3 libraries apart from psutil. The very first library is argparse. Basically this is to build the command line interface for us so that everything comes up in the terminal and we can easily view, watch, kill anytime, and we can even make it sleep. So this is for monitoring loop. And the second thing that I'm using is obviously Psutil. So this is basically the engine for us. It does all the actual system reading for us. And this also gives us the details about our virtual memory, returns what is the total number of RAM that our system has right now., and it also shows how much RAM are we using currently and how much is available and percentage used and much more. Other is the time and the datetime library, just to present that, uh, what is, uh, the, like, what is the current time right now, and I can just display it, right? So now what I have done is after importing these important libraries, I have started with creating a function which is get_memory_info. Now what is this get_memory_info? This is a very short function which I have mentioned. So basically This function is calling psutil virtual memory function. So basically that one line, this particular thing is basically returning the total RAM, used RAM, available RAM, and even the percentage used, and all the pulls straight from operating system for us. Now we are going to convert the raw byte value into the gigabyte, so that is why I am doing what I am using 1024 as a value. And 3 times I'm doing it. So basically I can convert the byte value into the gigabyte so that I can get the total GB, used GB, available GB, and the percentage value. So for total GB, used GB, and available GB, I'm dividing basically with the 1024 times 3. That is basically bytes to kilobytes, and then kilobytes to megabytes, and then megabytes to gigabytes. That is why I am multiplying it with 3. I hope you understood. I'm repeating. What we are doing is— why 3? Because I'm converting bytes into kilobytes, then kilobytes to megabytes, and then megabytes to gigabytes. And this is gonna return all as a dictionary. That is important so the rest of the script can use it cleanly. That is why we are generating a dictionary out of it. You can see we are using the curly bracket in the return statement. Now next, what I have done is I have created another function which is the get top processes. Now see, very first thing that I'm doing is I'm understanding what is the usage, what is the total memory that I have, and how much is available, what is the percentage, and so on. Then I'm checking what are the top processes happening. So by default, if you will run this code file, you will get top 10 processes. Now why top 10 processes? This is basically a core diagnostic function, I can say, which is psutil process -a. This, this is basically going to loop through every single running process on our machine, every app, every background machine, every background services that is going on. So let's say, uh, you are opening Visual Studio Code, you are opening Chrome, you are opening editor like CapCut and all. So whatever is opened on your system, it is gonna track it and it is gonna loop on it and give you 4 values. What are the 4 values? First is PID, which is the process ID, its name, its usage in bytes, and also the percentage of the RAM it is consuming or it is using. So basically it is going to return 4 things for us. Now this is for getting the values from the processes. I want to work on top 10. You can either, you know, when you will run this file, you can mention do you want to work for top 5, top 10, whichever number basically. So you can just pass on the value in the function as well, but for now by default I have made it top 10. Now what I have done next is I have used try and except block. Yes, this program will also help you build logic, understand Python in the better way, understand your operating system in the better way, and also have a good understanding of the core concepts of computer science. Now what I have done is I have written try and except block. Now what try and except block will do? Basically, processes can disappear in the mid loop. There is a chance that I might terminate any of my application, any of my background application, so it might disappear in the mid loop. So something closes when we are reading it, or we might not have any permission from our, uh, C drive or D drive, which is the local my PC, uh, you know, drives that we have in which we have our window, basically the operating system. So it might crash. So rather than making it crash, or rather than crashing the whole script, what we are doing is we can just skip that one and move on. That's it. Now once we have got the full list, we are going to sort the memory usage. Whichever usage is the highest, you can see we are sorting it In line number 46, we are using the sort function. We are sorting it and we are making sure the highest comes first. And then according to the highest, it means according to the descending order, I am going to make it return top 10. By default it will be 10, but anyhow, if you are going to pass any n value in this, then it will basically go to that particular value. Like, basically it will return that number of top results. Now next is my function which is the print_report. However you wanted to display the report, you can. The very first function is I'm calling the function that I have created, which is get_memory_info. So this is just a format, everything we collected to clean readable format. Basically, I'm just making it in the readable table format so that I can see total RAM, used RAM, available RAM, and then PID, process name, memory in gigabytes. And then we are also lined up in the columns, and we are also using f-string. This is also important to note because if you are learning Python, you should have the good knowledge of, you know, presenting the output in the better way. Because output is what makes your thing more clear, that how good you are in Python programming language, how good you are with the operating systems, how good you are with the core understandings. So that is why you should use f-string basically, so that we can fix the width of formatting as well. Because I do not want a long number like 1.169544, whatever number comes up in the, in the picture. But I just want few digits after percentage, so that too matters a lot. So that is why we are using f-string. Now this will print the report, correct? Now at some point we might want to stop it. So that is— there is a function called kill_process. kill_process, and you can terminate any particular process. So this is where it gets useful. So basically, this is the point where it gets useful, because not just— we are not just making it informative that, okay, we will get the information and that's it. No, at any point you know that, okay, this is something which is consuming more RAM and probably I don't need it right now, you can terminate it, right? So you are going to get the process ID, PID, to the— to this function which is kill_process., and then you can use the function which is called psutil_process_pid. This is basically going to basically handle to that running process and terminate it immediately whenever the stop signal is sent, or same as closing it from the task manager, you can say it, right? So what we are doing is we are wrapping this in the try and except block for error handling if the process is manually closed by the user or if we do not have the permission. So we are trying to close something and we do not have admin rights, so we cannot do that. That is why we have to enclose it in the try and except block, which is very, very important. Otherwise your entire program will look ugly and will crash as well. Now next thing is I have created a monitor. Now at some point of time, I am, let's say, working on training a machine learning model or artificial intelligence project I am working on, or maybe I am working on a large data set. At that point of time, I might have to monitor my entire device so that I can stop working, I can stop— I can, you know, terminate anything. So basically, I want to do the continuous monitoring, right? So now what I have done in the monitor script is this particular script is basically one-time check for— it is like a watchdog I'm creating. It loops forever, it keeps checking the memory every few seconds. It is controlled by the interval parameter. If you see, I have the interval parameter over here. This is the interval parameter which I am mentioning, and according to the interval parameter and the threshold that I am mentioning, this function is gonna work, right? So this will print the full report immediately, so you can catch the exact movement thing spikes. And otherwise, what will happen is it will, it will print the quite single line updates.. And whenever you are done, you want to break the loop, you can just press Ctrl+C from your keyboard and it will basically— we are basically doing the keyboard interruption. And you can terminate this particular function as well with the Ctrl+C. You can see I have written keyboard interrupt also. Now before keyboard interrupt, it will check your system continuously with a threshold of 85%. By default, it will keep on printing, printing, printing. If it goes above the threshold, which is 85%, it will give you the alert as well and will return the top 5 processes which is going on. Understood? Now next is our main function, which is if name name name main, and this is basically our command center. Our rpass is defining every flag that script will accept, like top, kill, watch, interval, threshold. All these can be the input from the user. That is why I'm adding I'm using basically the argpass so that I can utilize my terminal to kill and watch and pass on the interval and the threshold manually via terminal. So that this is a very useful library if you wanted to utilize. You can actually call based on what you typed in the terminal via this. And then we are using the arg to kill the process, monitor, and basically we are using this to, you know, call the functions that we have created above. Now If you wanted to see, we just are going to run this. Now there are so many things which is just continuously going on in my system. So it will show each and everything to us that what happened, why, which is, which application is basically consuming more RAM. So now you can see this is a very basic laptop which I am using for the recording. So you can see this is the memory report of 147 and 13 seconds. What is the total RAM? 5 GB RAM. Used RAM is 5.51 GB, which is the 94%. Available RAM is very less because I'm utilizing— I have opened so many different applications. Docker is also there. I have Notepad open, and even Visual Studio Code is open, so it also consumes a lot of RAM. So you can see it shows these are the 10 top 10 memory hungry processes. Memory hungry processes like Chrome I have opened, 2 different IDs I have opened, then Zoom is there, PowerShell is there. I have different things which is, which is running basically. Now if you want to know which commands, you can see over here we have written top, kill, watch, interval, and threshold. And even in the same file I have added above which all the file, which all the commands which you can utilize right now. This is important because when you work with the operating system, when you work with the libraries that helps you automate your some tasks, or you will, which helps you basically in the day-to-day life, that makes programming more powerful. So I hope you have understood each and every line of code, and I hope you have understood the, the idea behind using this, right? So there are so many different things. So very first thing is why coder specifically should care about this is now even if you are not buying a budget laptop, let's say, this script earns its place in any developer kits as well. Now let me tell you how. Very first thing is debugging. When we do debugging, memory leakage happen. If you are writing a long running scripts, let's say, okay, let's say I have written this script only, and in this script only, or the service, I am basically gonna like, if I'm saying watch alongside it, you can see in the real time instead of guessing the which application is taking more load. Second is heavy local workload. So when we compile large project, when we are trying to work on Docker, when we are trying to work on Airflow, when you are trying to work on— when you are trying to basically directly publish your code from your system to GitHub, then also it is very important. These all spikes which RAM gets, you can visually see it, and whenever you feel something wants to be terminated, what something needs to be terminated, you can. Third is the automation. This really helps in the automation. This is a single file with zero external dependencies. Beside this, uh, psutil library, you can drop it into the cron job and even a startup script. And even you can wrap it in a bigger DevOps monitoring tool as well and can use it. And it is basically a building block for your next project. It is not just one script, it is gonna be very useful if you understand it from the core perspective, fundamental perspective, and you can easily utilize. So I hope you have understood each and every line. So stay tuned, happy learning, and










