Forum Xamarin Xamarin.Forms

Threadpool Hill Climbing on Device Timer

Hi There,

I've been having some odd performance issues in my Xamarin Forms Android app.
I have been using BLE to collect data from a device. Since the data is pretty high frequency, Xamarin's ble implementation fires hundreds of events per second. I have an implementation written using stock Android, that works with no problems.

When I use Xamarin (Forms) however, it seems like the event handling cannot keep up. As I suspected this could be a problem with my implementation, I took a step back, and decided to implement a simple timer on the UI thread in a new project, to see if rapid callbacks would cause the same issue I was seeing. Sure enough, I begin seeing these messages

06-15 11:49:16.385 D/Mono (32279): [0xdb215930] hill climbing, change max number of threads 3
06-15 11:49:19.188 D/Mono (32279): [0xdb215930] hill climbing, change max number of threads 2
06-15 11:49:20.589 D/Mono (32279): [0xdb215930] hill climbing, change max number of threads 3
06-15 11:49:23.393 D/Mono (32279): [0xdb104930] hill climbing, change max number of threads 2
06-15 11:49:24.794 D/Mono (32279): [0xdb215930] hill climbing, change max number of threads 3
06-15 11:49:27.597 D/Mono (32279): [0xdb215930] hill climbing, change max number of threads 2
06-15 11:49:28.998 D/Mono (32279): [0xdb215930] hill climbing, change max number of threads 3
06-15 11:49:31.801 D/Mono (32279): [0xdb215930] hill climbing, change max number of threads 2
06-15 11:49:33.602 D/Mono (32279): [0xdb215930] hill climbing, change max number of threads 3
06-15 11:49:36.409 D/Mono (32279): [0xdb104930] hill climbing, change max number of threads 2
06-15 11:49:40.214 D/Mono (32279): [0xdb104930] hill climbing, change max number of threads 3

This would seem to be OK, but If I wait long enough, the threadpool keeps allocating more and more threads to this timer.

06-15 12:16:36.784 D/Mono (32279): [0xdb1d0930] hill climbing, change max number of threads 44
06-15 12:16:42.763 D/Mono (32279): [0xdb1d0930] hill climbing, change max number of threads 45
06-15 12:16:43.669 D/Mono (32279): [0xdb1d0930] hill climbing, change max number of threads 46
06-15 12:16:51.756 D/Mono (32279): [0xdb1d0930] hill climbing, change max number of threads 47
06-15 12:16:52.576 D/Mono (32279): [0xdb1d0930] hill climbing, change max number of threads 48
06-15 12:16:55.283 D/Mono (32279): [0xdb1d0930] hill climbing, change max number of threads 49
06-15 12:16:55.558 D/Mono (32279): [0xdb1d0930] hill climbing, change max number of threads 50
06-15 12:17:01.248 D/Mono (32279): [0xdb1d0930] hill climbing, change max number of threads 51
06-15 12:17:09.004 D/Mono (32279): [0xd9ec9930] hill climbing, change max number of threads 48

This will eventually cause problems with CPU performance.

My code for this test was literally to create a new xamarin forms project (Visual Studio), and insert a Device.StartTimer on the UI thread every 0.1 seconds.

            protected override void OnStart()
            {
                // Handle when your app starts
                Device.BeginInvokeOnMainThread(() => Device.StartTimer(TimeSpan.FromSeconds(0.1), Timer_Elapsed));
            }

            private bool Timer_Elapsed()
            {                    
                return true;
            }

Is there something I am doing wrong here? It seems highly suspicious that the threadpool needs to allocate so many resources to run a simple timer every 100ms.

Answers

  • MichaelvanderHorstMichaelvanderHorst USMember ✭✭
    edited August 2016

    It seems that I have the same issue, not even doing one every 100 ms but still got this...
    08-11 16:02:46.572 D/Mono (26206): [0x828899a8] hill climbing, change max number of threads 87
    08-11 16:02:47.577 D/Mono (26206): [0x828899a8] hill climbing, change max number of threads 88
    08-11 16:02:48.577 D/Mono (26206): [0x828899a8] hill climbing, change max number of threads 89
    08-11 16:02:49.582 D/Mono (26206): [0x828899a8] hill climbing, change max number of threads 90
    08-11 16:02:50.582 D/Mono (26206): [0x828899a8] hill climbing, change max number of threads 91
    08-11 16:02:51.587 D/Mono (26206): [0x828899a8] hill climbing, change max number of threads 92
    08-11 16:02:52.587 D/Mono (26206): [0x828899a8] hill climbing, change max number of threads 93

  • JohnChiodiniJohnChiodini USMember
    edited August 2016

    Are you running the latest version of Xamarin? I think they made some massive changes to Threadpool in Mono 4.2.1 and above...

    http://www.mono-project.com/docs/about-mono/releases/4.2.1/

    After I updated, threadpool seems to cap off at a certain point. I haven't checked the timer, but realized that many fast calls seem to take a lot of CPU, probably because of the JNI bridge.

Sign In or Register to comment.