how can I avoid OOM and CPU running at maximum utilization

nesteiner

New Member
Joined
Dec 13, 2020
Messages
11
Reaction score
1
Credits
95
hey, guys, please forgive my poor english at first.
I am a noob programmer, so I usually write some dead loop code by accident, and then the cpu will run at maximum utilization, so that I can't control my desktop, including switch to tty, what can I do is to reboot it forcely.
and I found that when programming, the memory will decrease, so when I open some application which take large memory, the desktop stuck, even though I can switch to tty, it is very slow, I have to reboot it forcely.

though I have installed systemd-oomd, but I found that when the desktop stuck, the memory is close to full, so the systemd-oomd cannot detect it.

so many situations above, can you help me out ?
 


so many situations above, can you help me out ?

I'm not sure. We have a resident programmer or two, who might be able to help.

In the meantime, you could tell us your computer's specifications.

Though, I'm not sure how much that matters. After all, people have been successfully programming on even the oldest and slowest hardware.
 
OOM is typically for "Out Of Memory" and doesn't help much with CPU.

In the past I have opened two or three console windows at the same time.
I have top running in one before I start my problem code.
That way I can see the process ID (it's usually the top one)

Then in the third console, I can either "nice" or "kill" my process if necessary.
 
hey, guys, please forgive my poor english at first.
I am a noob programmer, so I usually write some dead loop code by accident, and then the cpu will run at maximum utilization, so that I can't control my desktop, including switch to tty, what can I do is to reboot it forcely.
and I found that when programming, the memory will decrease, so when I open some application which take large memory, the desktop stuck, even though I can switch to tty, it is very slow, I have to reboot it forcely.

though I have installed systemd-oomd, but I found that when the desktop stuck, the memory is close to full, so the systemd-oomd cannot detect it.

so many situations above, can you help me out ?
The most effective resolution is to write code with loops that work as intended within the limits that are configured to achieve the outcome desired. Another solution is to write the code in a virtual machine or some other sandboxed environment so that uncontrolled looping doesn't bring down the host.
 
I have top running in one before I start my problem code.

I was thinking strace but figure this is outside my wheelhouse enough to be less than comfortable making suggestions.
 
The problem with some pernicious uncontrolled looping code that starts to overwhelm the CPU and RAM, is that it can act like a forkbomb and simply not allow any other keyboard instruction to get through to stop it. No number of Cntl+c, kill signals or SysRq key presses has any effect. The only way out is a power cycle. BTDTBTTS. My answer was to try and be more careful with the coding.
 
oh, I forgot to ask, when the oomd works, and I wonder if there is a systemd to watch cpu like oomd, I want them to kill the problem process automatically
 
the problem is that anything that monitors processes is itself a process and if the CPU is thrashing or bogged down so is the monitor process. I wrote something called LCARS and it is now on all my systems. I can use it to kill a bad process from another computer. Without having to ssh or tty or anything else. I program also. But even that if the system is too held up will not work either.
The take away is be more careful in programming or write something that can remotely kill processes or anything else and have it running.
 
@APTI can you give me more detail about your LCARS ?
the problem is that anything that monitors processes is itself a process and if the CPU is thrashing or bogged down so is the monitor process. I wrote something called LCARS and it is now on all my systems. I can use it to kill a bad process from another computer. Without having to ssh or tty or anything else. I program also. But even that if the system is too held up will not work either.
The take away is be more careful in programming or write something that can remotely kill processes or anything else and have it running.
 
Sounds like one or more serious bugs in your code. The OOM kernel module probably isn't going to help you here.

An infinite loop can be enough to cause CPU usage to go through the roof. Especially if you're using recursion in your program (where a function calls itself recursively).
But if your memory is rapidly decreasing - that also indicates that you almost certainly have one or more memory leaks in your code.

Fix the issues in your code and the problems you're having will go away.

What programming language are you using?
From the description of the problems, I'm assuming that either C or C++?

In which case, if you're dynamically allocating memory in a loop and never freeing/deleting that memory, you're creating memory leaks. You need to free/delete ALL dynamically allocated memory/objects as soon as they are no longer required.

If you're using C++ and your compiler supports the C++11 standard (or greater), then assigning new/dynamically created objects to smart pointers like std::unique_ptr can help to automate memory management and prevent memory leaks.

std::unique_ptr's will delete objects they point to when/if the smart pointer drops out of scope.

But if you're using C, you need to do ALL memory management manually in your code. There is no other way around it. Any dynamically allocated memory MUST ALWAYS be free'd by you as soon as it is no longer required.

And if you're using recursion, you should always put in some code to limit the amount of recursion. Because infinite recursion is NOT usually a good idea.

C and C++ are both extremely powerful programming languages. They will do whatever you tell them to, even if what you're telling them to do is extremely stupid and/or ill advised.
They can also do things that many other programming languages either cannot do, or simply WILL not do because it's not necessarily safe to do. And because of this - it's very easy to get things wrong.

If you're not careful, you can cause all kinds of serious issues. Your application crashing is the least of your worries. If you do things REALLY badly - you can crash your entire system, which it looks like you may have just discovered. And it's possible to do even worse than that - at least you didn't accidentally overwrite your PC's boot sector, like I did once! Ha ha. Fortunately it wasn't one of my PC's. It was at college, many years ago. And they had backups of everything! Ha ha!

If you're having problems with your code, feel free to post any problematic bits in a reply here, along with some details about what your program's supposed to be doing. And myself, or one of the other programmers here will take a look at it and will try to help you to fix it.

Please note - If you do post code here - make sure you enclose the code in [code][/code] tags.
If you include the language in the opening tag - e.g. [code=c] for C, or [code=cpp] for C++, the code-block in your will have better syntax highlighting and the indentation/formatting of the code will be retained.

The best bet is to add the opening and closing tags to your post and then copy/paste your code inside it.
If you post code as plain text, without using code tags, the indentation/formatting will be lost and there will be no syntax highlighting. So please use code tags if you do decide to post your code.

e.g.
You would write a C++ snippet like this:
[code=cpp]
include <iostream>

int main()
{
std::cout << "Hello world\n";
return 0;
}
[/code]

Then in your post it will appear like this:
C++:
include <iostream>

int main()
{
    std::cout << "Hello world\n";
    return 0;
}

NOTE: Before anybody asks how I got the first code block to render as plain text - I used plain tags around the entire code block, to show how you'd include code whilst composing a post. But as mentioned above, the original indentation/formatting of the code in the first code block was lost because that block was rendered on the page as plain text, not code! The plain text rendering routines for posts here strip out leading whitespace. So if posting code here - code tags are essential!
 
@JasKinasis actually I use springboot + kotlin, what do I mean memory decrease is the use of memory is increasing,
I think it's the matter of springboot multithreading make the dead loop take all the cpu resources
 
Ah, right. I haven't done a lot with Kotlin. But I know that it is still possible to end up with memory leaks in programs written in Kotlin and other Java/JVM based languages.
Hopefully these links might help:
 
oh, thank you. but I think we are getting off the topic
oh, I forgot to ask, when the oomd works, and I wonder if there is a systemd to watch cpu like oomd, I want them to kill the problem process automatically
 
oh, thank you. but I think we are getting off the topic
Not really.
I get that you want to find a way to forcibly shut down your program when it's memory usage gets out of control. But surely the better solution is to actually address the bugs/defects in your code, to prevent this problem from occurring in the first place. From your limited description, it sounds like a classic memory leak. Which is why I originally assumed you were working with C or C++.
You're using Kotlin - and it's still possible to have memory leaks in Kotlin and other Java/JVM based languages.
So profiling your code and checking for memory leaks would be a good start. Otherwise, if it's NOT a memory leak - then you have some other fundamental defects in your logic/code that are causing this problem.

Either way, the REAL problem exists in your program itself. Fix that and you won't need to worry about finding a way to shut your process down if it goes out of control.

As a band-aid, whilst you're debugging/testing your program - you could perhaps write a shell script to run constantly in the background, in a loop. At the end of each iteration, it should sleep for a set period (10 seconds, 30 seconds, 60 seconds... Whatever you think is appropriate). That will minimise the scripts resource usage.

On each wake-up it should use ps to check for a running instance of your program.
If it finds one running, it should get the PID. Once you have a PID monitor the systems RAM, or the processes CPU usage - and if the system RAM drops below a certain level, or the processor remains at high usage for a certain number of wake-ups - it shuts your program down using the kill command.
That might be one way of automatically shutting your program down if it goes haywire!

Another way might be to run your program through a debugger in an IDE and perhaps have a system monitor running, so you can see your systems performance. As soon as you see the memory usage /CPU usage starting to excessively increase - stop the program from the IDE. Most debuggers have a "Stop debugger", or "Stop debugging" type option that will kill the program.
But the trick there is killing your program in time, before it goes too far and freezes your system!

You did say before that you were dropping to a tty and forcing a reboot to stop your program.
If you can get to a tty - another option would be to use something like: killall nameofprogram or kill -9 $(pgrep nameofprogram) - that should kill your errant program, then your system should start responding normally again and you'll be able to switch back to your desktop.
 
There are always some surprises, for example, I have 3.0 GB memory left, but I launch the Siderman's Civilization 6 which need more than 4 GB, the system will be stuck and the system-oomd doesn't work at all, I don't know why
 
@APTI can you give me more detail about your LCARS ?
Simply put it allows you to execute commands on any computer running lcars on it from another running it. It uses a mysql database to distribute commands. However if your system totally locks up then nothing at all will help. If you have more questions just ask.
 

Members online


Latest posts

Top