如何在C#编码中优化内存资源

时间:2014-01-07 05:55:11

标签: c# performance

以下代码在执行时会消耗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;

1 个答案:

答案 0 :(得分:1)

如果你真的真的想在任务管理器中使用比你需要放弃托管代码更少的内存来展示你的程序集。欺骗的方法是使用Salamander Protector之类的东西来编译应用程序,这使得应用程序成为本机可执行文件而不是.NET程序集。?

OR

另一种方法是使用ngen tool本地编译代码并且来自Microsoft(因此可能是可靠/安全的)。

来源:Making C#/.NET have a small footprint?