Reputation: 1
The following code consume 15 MB of memory resources when it executes. 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;
Upvotes: 0
Views: 167
Reputation: 5836
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.?
OR
Another way is to use the ngen tool which compiles the code natively and is from Microsoft (so presumably reliable/safe).
Source: Making C#/.NET have a small footprint?
Upvotes: 1