Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
GitHub or Developer Community with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
Guys, I found out that one of my Mono projects on Mac is having 100% CPU usage without any real load. Funniest thing that under Wine on the same machine it didn't have that problem.
I dig a lot and found that Mono's System.Threading.Thread.Sleep() resides on clock_nanosleep on non-windows platforms. That is generally good on Linux, but not on FreeBSD, Solaris and Mac OS X that don't have native clock_nanosleep at all and some substitution is performed instead. Thread.Sleep() is not a high precision timer, it does not need nanosecond resolution and can be easily fixed by using usleep, clock_sleep or even with mach_wait_until. Any of those lowers CPU usage in my case to 0-3% without any precision loss.
Even more, lot's of games are using Mono, MonoGame or Unity these days and games usually have loops with Thread.Sleep(1) inside. Those games on Mac OS X are heating the CPUs without any purpose meaning that millions of computers around the world right now uses excess power just because of clock_nanosleep!
So let's lower worldwide CO emission and excessive power usage by just fixing that one function!
I've not been able to reproduce this using the trivial test attached below. Could you provide a example that shows the 100% CPU behavior you have seen?
Created attachment 17595 [details]
Created attachment 17596 [details]
Thread.Sleep loop sample
Single Thread.Sleep call won't give you any significant CPU usage, you have to call it in a loop. It's common practice in games, multithreaded apps, network servers, etc.
Reopening this one. It's critical for infinite loops with Tread.Sleep(1)
I cannot reproduce with Mono 126.96.36.199 (2017-12/dcaf47f5187), please reopen if you still can. Thank you.