2

I've got a problem where I'm running a background process using mono 3.0.3 on a SLES box, and while using top I discovered that it's using 100% CPU. I searched and have tried the following possible solutions:

1) Setting the MONO_THREADS_PER_CPU environment variable to 1000, i.e.

export MONO_THREADS_PER_CPU=1000

This did not work. I tried with 300, per this thread, but that did not work, either.

2) According to this stackoverflow post, using Timers in the program to be executed using mono might cause high CPU load, however I made a very simple test program with just an infinite loop and ran it... and mono still had 100% CPU usage.

This is the program I used to test whether it was simply due to using Timers:

    static void Main(string[] args)
    {
        while(true)
        {
            //Console.WriteLine("Stayin alive.");
        }
    }

3) I thought perhaps this was an issue in mono 3.0.3, so I upgraded to 3.6.0, which is apparently the latest version recommended for SLES 11.3. Mono still takes up 99-100% CPU.

So I'm left wondering what is going on and how can I fix this issue. Any help is very much appreciated.

Community
  • 1
  • 1
Tino
  • 304
  • 4
  • 14
  • 2
    Your test code would run 100% CPU usage on any system (mono/.NET/c++/etc), its an infinite loop with no yield... I'm guessing the issue is in your code, not in Mono's setup. Without seeing what you are doing, its hard to tell you how to fix it but I'm guessing this isn't the right path. Just because the thread is background doesn't mean it can't be a processor resource hog. – Ron Beyer Jul 28 '15 at 19:41
  • Did you try running the code in the question you linked? That would be a better way to see if timers are an issue. Like already said infinite loops will always be 100% if you don't sleep somewhere. – AliciaBytes Jul 28 '15 at 20:00
  • "how can I fix this issue" Don't use silly infinite while loops that don't actually do anything. You can manually "throttle" the loop back by calling `Thread.Sleep()` with a small interval in there within your loop. On a side note, a single core system would be at 100%. A dual core system would see 50%, while a 3 core system would see 33% usage, etc...get the idea? – Idle_Mind Jul 28 '15 at 20:01
  • Thread.Sleep() is a blocking call, it would still be using 100% of the resources available. – Null511 Jan 20 '18 at 16:45

1 Answers1

1

Limiting CPU would need to be set at the linux level. For example:

https://unix.stackexchange.com/questions/151883/limiting-processes-to-not-exceed-more-than-10-of-cpu-usage

example

cpulimit -l 50 COMMAND

Otherwise, there is likely a bug in the code causing this that needs to be fixed (e.g. infinite loops).

Community
  • 1
  • 1
Kevin Seifert
  • 3,494
  • 1
  • 18
  • 14