killing processes problem

LinuxRer

New Member
Joined
Jul 6, 2025
Messages
4
Reaction score
0
Credits
55
Hii,

I hope whoever reading are doing well ,
I have got a query , like , mine is windows machine , and installed kali to experiment and whenever I open few tabs , the memory will shoot up to 98 percent(other applications added) and system stops responding and then as a solution I got an idea that , I can simply kill processes , whenever system resources reaches 90 percent and , I have written script using some sources , and excluded the root users processes and I am stilll unsure , if this will trouble my system , so anyone ,can please read the code and let me know , if I can execute this as job , when ever the memory consumption goes above 90 percent this gets executed and kill the least recent cache.
Code:
import psutil
import os
import time
import signal

IGNORE_USERS = ["root"]
MEMORY_THRESHOLD = 90.0


def get_external_process():
    external_procs = []
    for proc in psutil.process_iter(['pid','name','username','memory_percent']):
        try:
            if proc.info['username'] not in IGNORE_USERS:
                external_procs.append(proc)

        except (psutil.NoSuchProcess,psutil.AccessDenied):
            continue
        return external_procs
while True:
    mem = psutil.virtual_memory()
    if mem.percent >= MEMORY_THRESHOLD:
        print(f" High memory usage detected:{mem.percent}%")

        processes = get_external_processes()
        processes.sort(key = lambda p:p.info['memory_percent'] reverse = True)

        for proc in processes:
            try:
                print(f"killing PID {proc.pid}({proc.name()}) using proc.info['memory_percent']:.2f}% RAM")
                os.kill(proc.pid,signal.SIGKILL)
                break
            except Exception as e:
                print("Error:" ,e)
time.sleep(5)

Thanks
 


I think the real solution might be to figure out why your RAM usage is spiking that high when you have a browser with some open tabs.

First, how much RAM do yo have?
Two, which browser are you using?
Three, how many open tabs are we talking about?
 
Hii,

I hope whoever reading are doing well ,
I have got a query , like , mine is windows machine , and installed kali to experiment and whenever I open few tabs , the memory will shoot up to 98 percent(other applications added) and system stops responding and then as a solution I got an idea that , I can simply kill processes , whenever system resources reaches 90 percent and , I have written script using some sources , and excluded the root users processes and I am stilll unsure , if this will trouble my system , so anyone ,can please read the code and let me know , if I can execute this as job , when ever the memory consumption goes above 90 percent this gets executed and kill the least recent cache.


Thanks
I was thinking of the same questions as @KGIII in post #2.

If answers to those questions seem not to illuminate the issue, it sounds like the situation could benefit from a memory leak investigation. It's not normal at all for a standard installation to run into such memory problems. In fact linux memory management is particularly robust.

What do the logs say? Perhaps run one or both commands like the following:
Code:
journalctl -b -x -p 3

joournalctl -b | grep -i error

I think killing root processes without discriminating as to their functions is a dangerous practice and inappropriate for the functioning of the operating system. Hopefully there are much better solutions for the problem.
 
Actually , I am doing the dual boot in my system , so , thing is that , when I use the windows , it can simultaneously open soo many applications , intellij and 40 tabs.. something like that, but , when it comes to the linux part , it can only open 20 tabs and memory already goes to 85 or like that , then I try to open terminal or something it will cross 90 percent and it only matter of time , it will slowly reaches , as I keep my laptop in sleep mode. when I ran those command , I got error saying SGX support disabled ,

but I am scared to do it, as my linux already got crased once.
└─$ journalctl -b -x -p 3
Jul 07 09:02:48 kali kernel: x86/cpu: SGX disabled or unsupported by BIOS.
Jul 07 09:02:48 kali kernel: ACPI BIOS Error (bug): Could not resolve symbol [\>
Jul 07 09:02:48 kali kernel: ACPI Error: AE_NOT_FOUND, During name lookup/catal>
Jul 07 09:02:48 kali kernel: ACPI BIOS Error (bug): Could not resolve symbol [\>
Jul 07 09:02:48 kali kernel: ACPI Error: AE_NOT_FOUND, During name lookup/catal>
Jul 07 09:02:48 kali kernel: integrity: Problem loading X.509 certificate -65
Jul 07 09:02:48 kali (sd-exec-[425]: /usr/lib/systemd/system-generators/systemd>
Jul 07 09:04:10 kali gdm-password][1488]: gkr-pam: unable to locate daemon cont>
Jul 07 09:04:19 kali systemd[1497]: Failed to start app-gnome-gnome\x2dkeyring\>
░░ Subject: A start job for unit UNIT has failed
░░ Defined-By: systemd
░░ Support: https://www.debian.org/support
░░
░░ A start job for unit UNIT has finished with a failure.
░░
░░ The job identifier is 266 and the job result is failed.
Jul 07 09:04:19 kali systemd[1497]: Failed to start app-gnome-gnome\x2dkeyring\>
░░ Subject: A start job for unit UNIT has failed
░░ Defined-By: systemd
░░ Support: https://www.debian.org/support
░░
░░ A start job for unit UNIT has finished with a failure.
░░

[/CODE]
when I once done full updgrade then reboot , system got crased , so should I do that to check this and also , when I keep laptop in sleep mode , the memory consumption auto maticallly goes up , should I not do that ,

sorry for soo many questions ,
Thanks for help
 
Actually , I am doing the dual boot in my system , so , thing is that , when I use the windows , it can simultaneously open soo many applications , intellij and 40 tabs.. something like that, but , when it comes to the linux part , it can only open 20 tabs and memory already goes to 85 or like that , then I try to open terminal or something it will cross 90 percent and it only matter of time , it will slowly reaches , as I keep my laptop in sleep mode. when I ran those command , I got error saying SGX support disabled ,

but I am scared to do it, as my linux already got crased once.
└─$ journalctl -b -x -p 3
Jul 07 09:02:48 kali kernel: x86/cpu: SGX disabled or unsupported by BIOS.
Jul 07 09:02:48 kali kernel: ACPI BIOS Error (bug): Could not resolve symbol [\>
Jul 07 09:02:48 kali kernel: ACPI Error: AE_NOT_FOUND, During name lookup/catal>
Jul 07 09:02:48 kali kernel: ACPI BIOS Error (bug): Could not resolve symbol [\>
Jul 07 09:02:48 kali kernel: ACPI Error: AE_NOT_FOUND, During name lookup/catal>
Jul 07 09:02:48 kali kernel: integrity: Problem loading X.509 certificate -65
Jul 07 09:02:48 kali (sd-exec-[425]: /usr/lib/systemd/system-generators/systemd>
Jul 07 09:04:10 kali gdm-password][1488]: gkr-pam: unable to locate daemon cont>
Jul 07 09:04:19 kali systemd[1497]: Failed to start app-gnome-gnome\x2dkeyring\>
░░ Subject: A start job for unit UNIT has failed
░░ Defined-By: systemd
░░ Support: https://www.debian.org/support
░░
░░ A start job for unit UNIT has finished with a failure.
░░
░░ The job identifier is 266 and the job result is failed.
Jul 07 09:04:19 kali systemd[1497]: Failed to start app-gnome-gnome\x2dkeyring\>
░░ Subject: A start job for unit UNIT has failed
░░ Defined-By: systemd
░░ Support: https://www.debian.org/support
░░
░░ A start job for unit UNIT has finished with a failure.
░░

[/CODE]
when I once done full updgrade then reboot , system got crased , so should I do that to check this and also , when I keep laptop in sleep mode , the memory consumption auto maticallly goes up , should I not do that ,

sorry for soo many questions ,
Thanks for help
From your description linux is not performing as well as MS in relation to memory usage of open tabs. You didn't mention the percentage of memory that MS uses when the tabs are open, but it might be useful to know that.

It should be noted that not all tabs are necessarily the same, nor are all browsers the same, so a reasonable test of the two operating systems would be to use the exact same websites accessed on the tabs for both systems with the same browsers, though the browsers would likely only approximate each other. Something like the same version of firefox in both linux and MS could be suitable.

The disabled SGX (Software Guard Extensions) should not make any difference. It's an intel technology that intel discontinued in 2021 which intel replaced with other security technologies.

Assuming that the different performances between linux and MS are real then the following considerations come to mind.

Does Kali have swap space in terms of a swap file or a swap partition? How does Kali's swap space compare to the MS virtual memory? If they differ greatly, one can create a swap file in linux to increase the virtual memory.

You could try and run a more memory efficient browser, perhaps Brave, Midori, quitebrowser, Min Browser.

What services is Kali running? To see the running services you can run the following:
Code:
systemctl list-units
Perhaps disable all services that are unnecessary.

In relation to killing processes to free up RAM, the linux kernel actually has a mechanism to do this called the Out-Of-Memory-Killer, or OOM killer. When memory is running out the OOM killer terminates processes progressively to try and save the system. It's generally going to kill off processes according to an algorithm that is designed to prevent the system from crashing if it can. To see if the oom killer has been invoked one can check the logs, and the dmesg command:
Code:
journalctl -b | grep -i -e mem -e kill
 
dmesg

Personally, I haven't had reliable results with the linux hibernate, suspend and sleep processes, so I no longer use them but rather just poweroff. It's better for battery life too I found. Different computer laptop brands seem to respond in different ways and there's a lot of variability in the messages online that I've read over time on the issues of suspensions. Perhaps identifying the computer and its specifications will jog someone's memory.
 
Last edited:
Moving this to Kali.
 
From your description linux is not performing as well as MS in relation to memory usage of open tabs. You didn't mention the percentage of memory that MS uses when the tabs are open, but it might be useful to know that.

It should be noted that not all tabs are necessarily the same, nor are all browsers the same, so a reasonable test of the two operating systems would be to use the exact same websites accessed on the tabs for both systems with the same browsers, though the browsers would likely only approximate each other. Something like the same version of firefox in both linux and MS could be suitable.

The disabled SGX (Software Guard Extensions) should not make any difference. It's an intel technology that intel discontinued in 2021 which intel replaced with other security technologies.

Assuming that the different performances between linux and MS are real then the following considerations come to mind.

Does Kali have swap space in terms of a swap file or a swap partition? How does Kali's swap space compare to the MS virtual memory? If they differ greatly, one can create a swap file in linux to increase the virtual memory.

You could try and run a more memory efficient browser, perhaps Brave, Midori, quitebrowser, Min Browser.

What services is Kali running? To see the running services you can run the following:
Code:
systemctl list-units
Perhaps disable all services that are unnecessary.

In relation to killing processes to free up RAM, the linux kernel actually has a mechanism to do this called the Out-Of-Memory-Killer, or OOM killer. When memory is running out the OOM killer terminates processes progressively to try and save the system. It's generally going to kill off processes according to an algorithm that is designed to prevent the system from crashing if it can. To see if the oom killer has been invoked one can check the logs, and the dmesg command:
Code:
journalctl -b | grep -i -e mem -e kill
 
dmesg

Personally, I haven't had reliable results with the linux hibernate, suspend and sleep processes, so I no longer use them but rather just poweroff. It's better for battery life too I found. Different computer laptop brands seem to respond in different ways and there's a lot of variability in the messages online that I've read over time on the issues of suspensions. Perhaps identifying the computer and its specifications will jog someone's memory.
But , I am not tweaking with the roots processes right , so , as long as , I exclude the root processes , I thought , it would be fine
and also , I queries the systemctl , all the prcoess were like either firewall based , housekeeping , sound , and disk control and stuff like that.. so I dont think , we should terminate them ,and regardign the disk consumption , its like variable , when I keep on sleep it gradually goes up (memory consumption) else if I use 20 tabs , like mostly static websties.. and mail n stuff , non video processing only.. may be its issue cause its hdd , I guess ,

just wanted to know , if I can try my script , else , i was scared if i crash my system :)

Thanks for help
 
But , I am not tweaking with the roots processes right , so , as long as , I exclude the root processes , I thought , it would be fine
and also , I queries the systemctl , all the prcoess were like either firewall based , housekeeping , sound , and disk control and stuff like that.. so I dont think , we should terminate them ,and regardign the disk consumption , its like variable , when I keep on sleep it gradually goes up (memory consumption) else if I use 20 tabs , like mostly static websties.. and mail n stuff , non video processing only.. may be its issue cause its hdd , I guess ,

just wanted to know , if I can try my script , else , i was scared if i crash my system :)

Thanks for help
By all means run your script and see what happens. If it crashes the system, you can reboot. Just don't involve any data that you don't wish to lose in the process of testing it.

The mention of root processes in post #3 was just an observation, not a reflection on the script in post #1, so I'm sorry if that wasn't clear.

The issue of the excessive use of memory is not likely to be understood by the application of the script. Far more useful, in the long run, in my view, is to look into any possible precipitating factors for the situation which would involve an investigation involving some of the options mentioned in post #5, and likely some deeper investigation too. The script, if it works, is like a band-aid whereas I would prefer to know what is wrong with the system because it certainly should not behave in that dysfunctional manner. YMMV.
 
It didn't work and system got crased , I installed it again , I guess , I have to change the memory into SSD

Thankss!!
 


Follow Linux.org

Members online


Top