簡體   English   中英

如何在C#編碼中優化內存資源

[英]How To Optimize Memory Resources in C# Coding

下面的代碼在執行時會消耗15 MB的內存資源。 如何最大程度地減少對該資源的使用?

這是代碼:

string reply = "";
MySqlDataReader reader = null;
try
{
    findSMS.Enabled = false;
    findSMS.Interval = 30000;

    if (mycon.State == ConnectionState.Closed)
        mycon.Open();

    MySqlCommand mycmd = null;
    MySqlCommand updatecmd = null;
    string strnewsms = "select * from isms_data where sms_status='processing' and ((id%" 
                    + total_exe + ")=" 
                    + appflag + ") and try<=15 order by id desc limit 0," 
                    + query_limit + "";

    int countupdate1 = 0;

    while (countupdate1 < 10)
    {
        try
        {
            if (mycon.State == ConnectionState.Closed)
            {
                mycon.Open();
            }

            mycmd = mycon.CreateCommand();
            mycmd.CommandText = strnewsms;
            reader = mycmd.ExecuteReader();
            countupdate1 = 0;
        }
        catch (Exception ex)
        {
            mycmd.Dispose();
            if (mycon.State != ConnectionState.Closed)
            {
                mycon.Close();
            }
            Thread.Sleep(2000);
            saveErr("Error Occurred on select command >> " 
                    + ex.ToString() 
                    + " Frequency=" + countupdate1);
            countupdate1++;

        }
        if (countupdate1 == 0)
            break;
    }

    if (countupdate1 > 0)
    {
        string smsTest = "An error has encountered for selecting data For ISMS New Application No " 
            + appflag;
        try
        {
            sendTestSMS("01726300352", smsTest, "0");
            Thread.Sleep(2000);
            sendTestSMS("01722412196", smsTest, "0");
            Thread.Sleep(2000);
            saveErr("Error Occurred on select command");
            MessageBox.Show("select Error >>");
        }
        catch (Exception exx)
        {
            MessageBox.Show("Err");
        }
    }

    reply = "No Processing";
    string msisdn = "No MSISDN";
    string sms = "No Request Found";
    string strupdate = "";
    string stakeholder = "";
    string tarstr = "No Processing";

    if (appmonitor_status == Convert.ToString(1))
        api_status = InsertIntoBit(APPLICATION_ID);

    if (reader.HasRows)
    {
        while (reader.Read())
        {
            findSMS.Interval = 10;
            int id = int.Parse(reader.GetValue(0).ToString());
            stakeholder = reader.GetValue(2).ToString();
            msisdn = reader.GetValue(5).ToString();
            msisdn = msisdn.Replace(" ", "");
            msisdn = msisdn.Replace("-", "");
            sms = reader.GetValue(8).ToString();
            chkdup = reader.GetInt32(11);
            guid = reader.GetValue(14).ToString();
            int startid = -1, endid = -1;

            reply = sendSMS(sms, msisdn, chkdup, guid);

            startid = reply.IndexOf("<SMS_STATUS>");
            endid = reply.IndexOf("</SMS_STATUS>");
            tarstr = reply.Substring((startid + 12), (endid - (startid + 12)));


            int countUpdate = 0;
            while (countUpdate < 10)
            {

                if (tarstr == "SUCCESS")
                {
                    strupdate = "Update isms_data SET sms_status='" 
                        + tarstr 
                        + "',send_time=now(),try=try+100 WHERE id=" 
                        + id.ToString();
                }
                else
                {
                    strupdate = "Update isms_data SET sms_status='" 
                        + tarstr 
                        + "',send_time=now(),try=try+1 WHERE id=" 
                        + id.ToString();
                }

                try
                {
                    if (mycon1.State == ConnectionState.Closed)
                    {
                        mycon1.Open();
                    }

                    updatecmd = mycon1.CreateCommand();
                    updatecmd.CommandText = strupdate;

                    updatecmd.ExecuteNonQuery();
                    countUpdate = 0;
                }

                catch (Exception ex)
                {
                    updatecmd.Dispose();
                    if (mycon1.State != ConnectionState.Closed)
                    {
                        mycon1.Close();
                    }
                    Thread.Sleep(2000);
                    saveErr("Error Occurred on Update command For MSISDN" 
                        + msisdn + ">>" 
                        + ex.ToString() 
                        + " Frequency=" + countUpdate);
                    countUpdate++;
                }

                if (countUpdate == 0)
                    break;
            }

            if (countUpdate > 0)
            {
                string smsTest = "An error has encountered for updating sms status of " 
                    + msisdn + " For " 
                    + stakeholder + " in ISMS Application No >> " + appflag;
                saveErr(smsTest);
                try
                {
                    sendTestSMS("01726300352", smsTest, "0");
                    Thread.Sleep(2000);
                    sendTestSMS("01722412196", smsTest, "0");
                    Thread.Sleep(2000);
                    sendTestSMS("01730070547", smsTest, "0");
                    Thread.Sleep(2000);
                }
                catch (Exception exx)
                {
                    MessageBox.Show("Error in sending test sms for mobile no " + msisdn + "");
                }

                MessageBox.Show("Update Error >>");
            }

            if (sendSMSList.Items.Count >= 20)
                sendSMSList.Items.Clear();

            string[] listdata1 = { msisdn, sms, tarstr };
            ListViewItem li2 = new ListViewItem(listdata1);
            sendSMSList.Items.Add(li2);
            Application.DoEvents();

            //api_status = InsertIntoBit(APPLICATION_ID);
        }
        if (appmonitor_status == Convert.ToString(1))
            api_status = InsertIntoBit(APPLICATION_ID);
    }

    if (!reader.IsClosed)
        reader.Close();

    if (mycon.State != ConnectionState.Closed)
        mycon.Close();
    if (mycon1.State != ConnectionState.Closed)
        mycon1.Close();

    if (sendSMSList.Items.Count >= 20)
        sendSMSList.Items.Clear();

    string msisdn1 = "No MSISDN";
    string sms1 = "No Request Found";
    string tarstr1 = "No Processing";
    string[] listdata = { msisdn1, sms1, tarstr1 };
    ListViewItem li = new ListViewItem(listdata);
    sendSMSList.Items.Add(li);

}

catch (ArgumentOutOfRangeException err)
{
    saveErr("HTTP Error!!!!Wrong Output from http!!! >> " + reply);
    MessageBox.Show("Argument out of range exception");
}

catch (Exception err)
{
    saveErr("Main Error ==> " + err.ToString());
    MessageBox.Show("Main Error");
}

/*if (!reader.IsClosed)
    reader.Close();*/

findSMS.Enabled = true;

如果您真的很想讓程序集在任務管理器中顯示,而內存比您需要放棄托管代碼少。 作弊的一種方法是使用Salamander Protector之類的東西編譯您的應用程序,這使您的應用程序成為本機可執行文件,而不是.NET程序集。

要么

另一種方法是使用ngen工具 ,該工具可以從Microsoft本地編譯代碼(因此可能可靠/安全)。

資料來源: 使C#/。NET占用的空間很小?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM