Memory increase gradually after every HTTPClient Call in Xamarin iOS. GC collection is not effective

I have monitor the Application Activity using Instrument. I have noticed that Real mem is gradually increase. I just need release the memory which is used by HTTPClient.

I have tried using Dispose(),used using statement,used GC.collect; but those are not reducing the memory. It looks like not effective.

Below is code used in the program.

namespace MemoryManagementStudy
    {
        public partial class ViewController : UIViewController
        {


            protected ViewController(IntPtr handle) : base(handle)
            {
            }

            NetworkStaticCall networkStaticCall;
            public override void ViewDidLoad()
            {

                //lblStatusUpdate.Text = "Success Count: 0, Fail Count: 0 -- Last Update: " + DateTime.Now;
                base.ViewDidLoad();
                //string status = "";
                networkStaticCall = new NetworkStaticCall();
                networkStaticCall.init();
                Timer timer = new Timer(1000 * 30);
                timer.AutoReset = true;
                timer.Elapsed += async (sender, e) => {

                    Console.WriteLine("Memory " + GC.GetTotalMemory(true));
                    await networkStaticCall.MakeSMWebService();
                    Console.WriteLine("Memory " + GC.GetTotalMemory(true));

                };
                timer.Start();
            }

            public override void DidReceiveMemoryWarning()
            {
                base.DidReceiveMemoryWarning();
            }
        }


        class NetworkStaticCall
        {
            //int successCount = 0;
            //int failCount = 0;

            private static HttpClient httpClient;
            private static HttpResponseMessage httpResponseMessage;
            private StringContent dataContent;
            private string xmlString;
            private static HttpClientHandler httpClientHandler;
            private NSUrlSessionHandler nsUrlSessionHandler;
            public void init()
            {

                httpClientHandler = new HttpClientHandler();
                httpClientHandler.Credentials = new NetworkCredential("userName", "Password"); ;
                httpClient = new HttpClient(httpClientHandler);
                httpClient.DefaultRequestHeaders.Accept.Clear();
                dataContent = new StringContent("Xml String Content",
                                                Encoding.UTF8, "text/xml");
            }

            public async Task<string> MakeSMWebService()
            {
                Console.WriteLine("Call Processing");
                try
                {
                    httpClient.DefaultRequestHeaders.Accept.Clear();
                    httpResponseMessage = await httpClient.PostAsync("url", dataContent);
                    Console.WriteLine("Call success");
                    xmlString = await httpResponseMessage.Content.ReadAsStringAsync();
                    //successCount++;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                return "";
            }


        }
    }

We have tried https://stackoverflow.com/a/27830926 but it is not working.

I have tried WebRequest but it still increasing

class CustomWebRequest{

    string finalUrl = "URL";
    WebRequest request;
    WebResponse response;
    StreamReader reader;
    int successCount = 0;
    int failCount = 0;

    public void init(){

    }
    async public Task<string> Post()
    {
        request = WebRequest.Create(finalUrl);
        request.Method = "GET";
        request.Timeout = 10000;
        try
        {
            using (await request.GetResponseAsync())
            {
                using (reader = new StreamReader(response.GetResponseStream()))
                {
                    Console.WriteLine(reader.ReadToEnd());
                }
            }
            successCount++;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            failCount++;
        }
        finally
        {
            if (response != null)
            {
                response.Close();
                response.Dispose();
                response = null;
            }
            if (reader != null)
            {
                reader.Close();
                reader.Dispose();
                reader = null;
            }
            if(request != null){
                request.Abort();
                request = null;
            }
        }
        return string.Format("Success Count: {0}, Fail Count: {1}", successCount, failCount);
    }
}

Answers

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    Are you sure you are doing it correctly? It's normal that memory increases until GC collects unused memory.
    So, try using memory profiler or call GC.Collect to release the memory (you said you tried though).
    So, try memory profiler if it shows where the memory goes.

Sign In or Register to comment.