0

I have monitor the Application Allocation using Instrument. I have noticed that Memory 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

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using UIKit;

namespace MemoryManagementStudy
{
public partial class ViewController : UIViewController
{


    protected ViewController(IntPtr handle) : base(handle)
    {
    }
    CustomWebRequest customWebRequest;
    public override void ViewDidLoad()
    {
        customWebRequest = new CustomWebRequest();  
        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 * 60);
        timer.AutoReset = true;
        timer.Elapsed += async (sender, e) => {
            status = await customWebRequest.Post();
            InvokeOnMainThread(()=>{
             lblStatusUpdate.Text =status + " -- Last Update: " + DateTime.Now;
            });
            GC.Collect();
        };
        timer.Start();

    }




}


class CustomWebRequest{

    string finalUrl = "URL";

    //StreamReader reader;
    int successCount = 0;
    int failCount = 0;


    async public Task<string> Post()
    {
        var request = WebRequest.Create(finalUrl);
        request.Method = "GET";
        request.Timeout = 10000;
        try
        {
            using (var response = await request.GetResponseAsync())
            {                  
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    Console.WriteLine(reader.ReadToEnd());
                }
            }
            successCount++;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            failCount++;
        }
        finally
        {

            if(request != null){
                request.Abort();
                request = null;
            }
        }
        return string.Format("Success Count: {0}, Fail Count: {1}", successCount, failCount);
    }
}
}
Community
  • 1
  • 1
Gowtham
  • 149
  • 8

0 Answers0