0

我正在使用以下 gcm 类发布数据以向大约 10,000 个 android 设备发送 gcm 通知:

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";
    }
}

这在包含计时器的 Windows 窗体中使用,如果后台工作人员不忙从数据库中获取数据并将其发送到大约 10,000 个调用的 Android 设备,则每 10 秒将打勾:

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

每个sublist请求的 gcm 云配额最大为 1000。通知收到很好,但程序使用大量内存。

在尝试检测导致 ram 使用的项目主要部分(进程在 4 天内使用 2 GB 内存)时删除功能后,我发现发送通知导致此 ram 使用。

我使用 httpwebrequset 搜索了 ram 使用问题,但没有找到任何相关内容。我也尝试调用垃圾收集器,但它并没有清除大部分保留使用的所有内存;它仅清除所用总 RAM 内存的约 5%。任何人都可以帮助阻止这种大量内存使用。

4

1 回答 1

0

您的 GetPostData 函数正在附加大量字符串。请改用 StringBuilder。

于 2013-11-01T15:30:23.893 回答