I created a little program to measure my 4g data consumption and it's working pretty well... Except for one part.
After running for a whole day, I heard my computer making sounds I've never heard so when I went to my Task Manager and was surprisingly welcomed with a Python program using 3GB of RAM. I stopped the program and re-ran it. It started at 20 Mb of RAM consumed and kept getting higher and higher.
I tried using the garbage collector to (free) the value returned each time but that didn't help since the return value is a float
. I did try to cast it to int
without success.
import time
import threading
from tkinter import *
import psutil
import socket
import gc
def running_processes():
for proc in psutil.process_iter():
try:
# Get process name & pid from process object.
processName = proc.name()
processID = proc.pid
print(processName , ' ::: ', processID)
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
def data(start_val):
# Returns the data consumed while connected
start_val = 0
result = 0
global timer
while True:
current_val = psutil.net_io_counters().bytes_sent + psutil.net_io_counters().bytes_recv
result = current_val - start_val + result
start_val = current_val
timer = threading.Timer(1.0, data)
timer.start()
timer.cancel()
return convert_to_mo(result)
def main():
# Runs and displays data
kiss = data(0)
display_label['text'] = str(kiss) + ' || Mo Consumed '
display_label.pack()
app.after(5, main)
#gc.collect(kiss)
def convert_to_mo(value):
# 1 octet = 1 byte = 8 bits // 1024 bytes = 8192 bits = 1 kb;
return value/1000000
app = Tk()
app.title("Data Consumption Monitor")
app.geometry('420x40')
app.configure(background = 'black')
frame = Frame(app)
display_label = Label(frame, font = 'montserrat 20', bg = 'black', fg = '#20C20E')
frame.pack(anchor=CENTER)
running_processes()
main()
app.mainloop()