none
HttpClient causes high cpu usage after a request (Xamarin) RRS feed

  • Question

  • I have an out of the box (default template) xamarin forms application that I added a single http get to "google.com" on a button press. The CPU usage of the app is minimal until I make that web request. At that point it begins to use quite a bit of cpu - probably adding another .1s cpu usage every second or so (even while the app is backgrounded). I'm wondering why this is and if there are any solutions to it?

    Here is the command for the button press:

    public ICommand MakeRequestCommand
    {
      get
      {
        return new Command(async () =>
        {
          HttpClient client = new HttpClient();
          await client.GetAsync("http://google.com");
        });
     }
    }
    Wednesday, April 18, 2018 7:27 PM

All replies

  • Hello David,

    >>I'm wondering why this is and if there are any solutions to it?

    The above code could be parsed as below.

           public ICommand MakeRequestCommand
            {
                get
                {
                    return new Command(Test);
                }
            }
    
            public async Task Test() {
                HttpClient client = new HttpClient();
                MessageBox.Show("sdsd");
                var message= await client.GetAsync("http://google.com");
                Console.WriteLine(message.Content.ReadAsStringAsync().Result);
            }
    
            public class Command:ICommand
            {
                public Func<Task> p;
                public Command(Func<Task> p)
                {
                this.p = p;
                 }
             }

    For the above code, you have a ICommand  property and the value is Command object whose ctor receive a Func<Task> parameter. This process don't trigger the Func<Task> method. 

    I'm confused with you asynchronous anonymous methods and it just send request without obtain response object. which means you do some valueless job and no pay back.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 19, 2018 6:53 AM
    Moderator
  • I'm not concerned about functionality or getting anything back from the request - I'm just throwing it away for that example. My concern is that after this GetAsync() call, the CPU on my android device spikes and remains elevated indefinitely after the request. This is causing battery warnings on users' phones and high battery usage in general. I have tried client.DefaultRequestHeaders.ConnectionClose = true; but that doesn't seem to help.

    Here is a screenshot of the CPU use: i. imgur .com /zKbx5FP.png

    • Edited by David Hauck Thursday, April 19, 2018 12:32 PM
    Thursday, April 19, 2018 12:28 PM
  • Hello David,

    Try to close http connection and dispose unmanaged resource after send request.

         using (HttpClient client = new HttpClient()) {
                  await client.GetAsync("http://google.com");
                }

    Maybe you need to consult some people in Xamarin forum at below link.

    https://forums.xamarin.com/

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 23, 2018 9:19 AM
    Moderator