When my computer freezes up, usually its really easy to resolve the problem by going into the terminal and running top to find out whats eating up the memory and killing that process. It can be difficult sometimes when its badly frozen up, I have to hit
F1 to get into the terminal. Also its handy to have a keyboard shortcut setup for running xkill so I can kill GUI apps that I know are freezing it up.
How do you deal with this problem? Also how do you deal with memory overload problems in general? I used to just hit
T, run top, and see what program was using the most memory then kill it, but thats a lot of steps and when your computer is frozen, a single step can take a long time. So I'm looking into better ways, like setting up a killswitch to do it in one step, or setting up a daemon that will kill any programs that are about to freeze up the machine.
Also whats the best way to find out which program is causing the problem? top isn't always easy to see because sometimes top shows no programs eating up excessive CPU, but if I scroll down I see there are about 50 instances of this program running, so it would be a whole lot better if I could see combined CPU usage of the program. Firefox and chrome are the two main culprits, occasionally sublime-text-3 (maybe this is due to having too many tabs open?).
So I found this command:
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
Which gives this list right now:
PID PPID CMD %MEM %CPU 6896 968 /opt/google/chrome/chrome - 13.1 2.7 27334 4769 /usr/lib/firefox/firefox 6.6 13.0 5607 4769 /usr/bin/compiz 4.7 44.7 26381 26363 /usr/lib/libreoffice/progra 2.7 0.0 953 4769 /opt/google/chrome/chrome h 2.0 3.0 2777 2457 /usr/lib/xorg/Xorg -core :0 1.8 29.5 6980 4769 /usr/lib/x86_64-linux-gnu/z 1.6 0.0 8180 8167 mono /usr/lib/AirVPN/AirVPN 1.6 2.1 1096 968 /opt/google/chrome/chrome - 1.4 0.1
My computer isn't frozen right now so its alright, but firefox itself is freezing up when I try to use it. I tend to keep loads of tabs open, is that why I see so many instances of chrome and firefox when I run top?
I see htop gives a lot more options than top, so its probably a better tool for diagnosing the issue. Theres a few things I don't understand about all this, firstly which is more important %CPU or %MEM when it comes to your computer freezing regularly. htop allows you to sort by either one, heres what it shows me when I sort by memory usage:
So like I saw from running ps, firefox and chrome are taking up most of the memory, and compiz is taking up most of the CPU. Are there any other important factors to look at when it comes to unfreezing your system? For example, does the length of time a process has been running for come into play, I see compiz has been running longer than anything else:
As for a killswitch, I found one that someone else wrote:
while [ 1 ]; do echo echo checking for run-away process ... CPU_USAGE=$(uptime | cut -d"," -f4 | cut -d":" -f2 | cut -d" " -f2 | sed -e "s/\.//g") CPU_USAGE_THRESHOLD=300 PROCESS=$(ps aux r) TOPPROCESS=$(ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | grep -v PID | head -n 1) if [ $CPU_USAGE -gt $CPU_USAGE_THRESHOLD ]; then kill -9 $(ps -eo pid --sort=-%mem | grep -v PID | head -n 1) echo "High memory usage process killed $TOPPROCESS\n" echo "Load average was at $CPU_USAGE\n" else echo 'CPU Load Back to Normal' exit 0 fi done
But I'm not 100% sure how it works or what it does. When I run:
uptime | cut -d"," -f4 | cut -d":" -f2 | cut -d" " -f2 | sed -e "s/\.//g"
Its returning numbers like 149, 179, 178. I read the uptime manual and can't understand how its useful for preventing your system from freezing up.
They're not process IDs I see in any of the top memory using apps shown by htop.
Running uptime by itself, I get this:
17:49:53 up 1 day, 11:09, 2 users, load average: 3.16, 2.45, 2.03
So my system has been up for 17.49 hours. I don't know what the load average means in terms of my computer. I was given this computer by my brother recently since he had an unused one at his office, when I check the specs:
$ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 8 On-line CPU(s) list: 0-7 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 26 Model name: Intel(R) Core(TM) i7 CPU 940 @ 2.93GHz Stepping: 4 CPU MHz: 1867.000 CPU max MHz: 2934.0000 CPU min MHz: 1600.0000 BogoMIPS: 5852.21 Virtualization: VT-x L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 8192K NUMA node0 CPU(s): 0-7
I'm pretty shocked, its a beast, 8 CPUs. And the RAM:
free -lm total used free shared buff/cache available Mem: 12005 7154 311 531 4539 3996 Low: 12005 11694 311 High: 0 0 0 Swap: 30516 1002 29513
12 Gb, my God I had no idea. My old laptop had 2 Gb. So its a mystery why its freezing like this now.
So what I'm wondering is:
1.) Whats the best way to find out what is causing your system to slow down and freeze?
2.) Whats the best way to unfreeze your system, especially in those worst case scenarios when you can't move the mouse pointer. Would a shortkey combo connected to a killswitch be the way to go?
3.) For making a kill switch, how would you go about doing it? What should the script do? Should it find out which program is using the most memory and/or CPU and kill it. I'd prefer to take a more sophisticated approach because in certain cases the app thats using most memory or CPU might be something I need to keep open, such as if I'm working on firefox and killing it could cause loss of data I'm writing. I can supply the script with a list of apps which are less important, so it can run through that list and evaluate if they are eating enough memory that makes it worth killing them.
4.) Would increasing the size of the SWAP partition help?