简体   繁体   English

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

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

The following code consume 15 MB of memory resources when it executes. 下面的代码在执行时会消耗15 MB的内存资源。 How can I minimize the use of that resource? 如何最大程度地减少对该资源的使用?

Here is the code: 这是代码:

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;

If you really really really want to make your assembly show in task manager with less memory than you need to ditch managed code. 如果您真的很想让程序集在任务管理器中显示,而内存比您需要放弃托管代码少。 A way to cheat is to compile your application using something like Salamander Protector which makes your application a native executable rather than a .NET assembly.? 作弊的一种方法是使用Salamander Protector之类的东西编译您的应用程序,这使您的应用程序成为本机可执行文件,而不是.NET程序集。

OR 要么

Another way is to use the ngen tool which compiles the code natively and is from Microsoft (so presumably reliable/safe). 另一种方法是使用ngen工具 ,该工具可以从Microsoft本地编译代码(因此可能可靠/安全)。

Source: Making C#/.NET have a small footprint? 资料来源: 使C#/。NET占用的空间很小?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM