On an 8GB laptop, VSCode + some build + 20 tabs open in Chrome ** will run out of memory in a blink of an eye **. It is a very strict user experience such as stall for each OS.
When I think about it, since I was a child, I was often reprimanded by my mother for leaving things out. Rather than just a lack of memory capacity, it may be due to the difficult-to-heal childhood.
I feel that it would be better to get angry with my mother each time than to correct my behavior. So, create a ** mother-like app ** that warns you to close unnecessary software on your desktop when memory is tight.
--Ubuntu18.04 + gnome3 environment
To get the amount of memory available on Linux, you generally use the ** free command **.
$ free
total used free shared buff/cache available
Mem: 8124668 4831144 267628 201468 3025896 2774924
Swap: 2097148 512 2096636
Of these, the available real memory capacity is ** the available column in the Mem row **. Roughly speaking, this value is calculated by ** [free space + capacity that can be released immediately such as used cache] **.
The content of this free command is a formatted version of the content of the / proc / meminfo pseudo file. Files are easier to handle to use from python, so pick up information from ** / proc / meminfo **.
~$ cat /proc/meminfo
MemTotal: 8124668 kB
MemFree: 328384 kB
MemAvailable: 2640012 kB
Buffers: 740992 kB
・ ・ ・(Abbreviation)
Try opening the file on the interactive shell. Use the split () function to separate line breaks and spaces, and check the order in which Available comes.
>>> f = open('/proc/meminfo')
>>> f.read().split()
['MemTotal:', '8124668', 'kB', 'MemFree:', '318860', 'kB', 'MemAvailable:', '2631456', 'kB', ...]
>>> f.seek(0)
0
>>> f.read().split()[7]
'2625528'
** A notification should be given when an element with an index of 7 falls below 0.5GB (500,000kB). ** ** (The basis for 0.5GB is somehow)
By the way, as you can see from the above example, the contents are updated ** every time you read ** even after opening.
Use the notify-send command. The format is as follows.
$ notify-send -u critical --icon=Icon name"title" "message"
--Specify the notification type in -u. I'm angry at the content, so I think it's okay to be critical. --You can specify the icon with --icon. You can also specify a custom icon. See below. --You can use simple markup in your messages.
I borrowed a mother icon from Mr. Irasutoya and used it (I also considered a live-action mother, but it was powerful and I felt the emotional resentment).
Place the completed icon as ** shikaruhaha.png ** under ** / usr / share / pixmap . (If you place it in a place other than / usr / share / pixmap, you must specify the path). - Omit the extension when specifying with icon. ** **
$ notify-send -u critical --icon=shikaruhaha "Mother's voice" "<b>J( 'д')Takashi! You're not leaving it messy!</b>"
When you execute the above, it becomes like this.
memwarn.py
#!/usr/bin/python3
import time
import subprocess
with open("/proc/meminfo") as f:
while(True):
f.seek(0)
arr = f.read().split()
available = int(arr[7])
if available < 500000:
subprocess.call(['notify-send', "-u", "critical", "--icon=shikaruhaha", "Mother's voice", "<b>J( 'д')Takashi! You're not leaving it messy!</b>"])
time.sleep(2)
--Loop every 2 seconds --Forcibly return the read position with f.seek (0) (because once read, the read position will be at the end) --Use ** subprocess.call ** to execute the shell. Pass shell commands in a space-separated list of strings. -Give execute permission with the file name ** memwarn.py ** and place it under ** / usr / libexec / **.
Start after logging in in the X environment. Place the following files under / etc / xdg / autostart /. After logging in, it will start automatically.
/etc/xdg/autostart/memwarn.desktop
[Desktop Entry]
Name=MemoryMother
Type=Application
Exec=/usr/libexec/memwarn.py
OnlyShowIn=GNOME;
NoDisplay=true
That's it
--Don't tell me to add more memory ――No matter how much you are too dependent -** Actually, it's not the name "Takashi" **
Recommended Posts