0

I am using the following gcm class to post data to send a gcm notification to about 10,000 android devices:

class GCMandroid
{
    private JArray RegIDs;
    private string tickerText;
    private string level;
    private string id;
    private string title;
    private string message;
    private string date;

    public GCMandroid(List<string> Ids,string tickerText,string level,string id,
        string title,string message,string date)
    {
        this.RegIDs = new JArray(Ids.ToArray());
        this.tickerText = tickerText;
        this.level = level;
        this.id = id;
        this.date = date;
        this.title = title;
        this.message = message;
    }
    public string GetPostData()
    {
        string postData =
            "{ \"registration_ids\": " + this.RegIDs + ", " +
            "\"time_to_live\":" + "43200" + ", " +
            "\"collapse_key\":\"" + "Key" + "\", " +
            "\"data\": {\"tickerText\":\"" + this.tickerText + "\", " +
            "\"level\":\"" + this.level + "\", " +
            "\"id\":\"" + this.id + "\", " +
            "\"date\":\"" + this.date + "\", " +
            "\"title\":\"" + this.title + "\", " +
            "\"message\": \"" + this.message + "\"}}";
        return postData;
    }
    public bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }

    public string SendGCMNotification(string apiKey, string postData, string postDataContentType = "application/json")
    {
        ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);

        //
        // MESSAGE CONTENT
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        //
        // CREATE REQUEST
        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
        Request.Method = "POST";
        Request.KeepAlive = false;
        Request.ContentType = postDataContentType;
        Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
        Request.ContentLength = byteArray.Length;

        Stream dataStream = Request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        //
        // SEND MESSAGE
        try
        {
            WebResponse Response = Request.GetResponse();
            HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
            if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
            {
                var text = "Unauthorized - need new token";
            }
            else if (!ResponseCode.Equals(HttpStatusCode.OK))
            {
                var text = "Response from web service isn't OK";
            }

            StreamReader Reader = new StreamReader(Response.GetResponseStream());
            string responseLine = Reader.ReadToEnd();
            Reader.Close();
            Response.Close();
            return responseLine;
        }
        catch (Exception e)
        {
        }
        return "error";
    }
}

this is used in a windows form that contain a timer and each 10 seconds will tick a background worker if it is not busy to fetch data from database and send it to about 10,000 android devices with calling :

GCMandroid gcm = new GCMandroid(sublist, tickerText, level, id, title, message,date);
gcm.SendGCMNotification(AndroidApiKey, gcm.GetPostData());

where sublist is max 1000 as the gcm cloud quota per each request. the notification is recieved well but the programs uses large amount of memory.

after removing functions while trying to detect the main part of the project that is causing the ram usage (2 GB of memory used by the process in 4 days) I found that sending the notification is causing this ram usage.

I searched for problems of ram usage with httpwebrequset and didn't find anything related. I also tried to call the garbage collector but it doesn't clear all the memory a big part stays used; it clears only about 5% of the total RAM memory used. Can anybody help to stop this large memory usage.

4

1 に答える 1