TABISH ALAM

TABISH ALAM

  • 1.6k
  • 124
  • 907

Websites were facing 503 error. Memory Leak Problem.

Apr 3 2017 3:47 AM

My websites were facing 503 error. It seems to be due to the overcrowding of the application pool.

 
My hosting service provider says following reasons:
 
All websites are allocated 150 MB of site memory. Whenever this limit exceeded on several occasions, the application pool for the site is restarted, which brings the site back online.This cycle generally takes about 3-5 minutes, and repeats each time the memory threshold is crossed; which is why the website seems to face brief but intermittent downtime.

The memory excess can be attributed to two possibilities;

(a) There is a memory leak somewhere due to suboptimal code, or
(b) The site really requires that much memory
 
My question is:
 
When I run this code, it is throwing 503 error after inserting 700-800 rows.
 
How can I resolve this problem? Please check my code is optimized or not and how can I optimize my following code.
 
Is any code for web.config file to resolve this problem?
 
/***********************************************************************/
 
public class MyFunction

{

public MyFunction()

{

//

// TODO: Add constructor logic here

//

}

public static string ConnectionString

{

get

{

try

{

return System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;

}

catch (Exception ex)

{

throw new ApplicationException("Unable to get Database Connection string from Web Config File");

}

}

}

public static string Execute(string query)

{

try

{

int ret = 0;

SqlConnection con = new SqlConnection(Database.ConnectionString);

con.Open();

SqlCommand cmd = new SqlCommand(query, con);

ret = cmd.ExecuteNonQuery();

con.Close();

return ret.ToString();

}

catch (Exception ex)

{

return ex.Message.ToString();

}

}

public static string GenerateOTP(int length, string is_alpha_numeric)

{

string otp = "";

try

{

string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

string small_alphabets = "abcdefghijklmnopqrstuvwxyz";

string numbers = "1234567890";

string characters = numbers;

if (is_alpha_numeric == "1")

{

characters += alphabets + small_alphabets + numbers;

}

//int length = int.Parse(ddlLength.SelectedItem.Value);

otp = string.Empty;

for (int i = 0; i < length; i++)

{

string character = string.Empty;

do

{

int index = new Random().Next(0, characters.Length);

character = characters.ToCharArray()[index].ToString();

} while (otp.IndexOf(character) != -1);

otp += character;

}

}

catch (Exception ex)

{

}

return otp;

}

}

 
 /*******************************************************/
 

protected void btn_save_Click(object sender, EventArgs e)

{

try

{

string query = "";

int success = 0;

int error = 0;

string member_key = "";

string res = "";

for (int i = 0; i < 1500; i++)

{

repeat:

member_key = MyFunction.GenerateOTP(8, "1");

query = "Insert into [Person].[MemberCode]([MemberKey],[CreateDate]) values('" + member_key + "','" + DateTime.Now.ToShortDateString() + "')";

res = MyFunction.Execute(query);

if (res == "1")

{

success++;

}

else if (res.Contains("Violation of UNIQUE KEY constraint"))

{

goto repeat;

}

else

{

error++;

}

}

if (success > 0)

{

msg.InnerHtml = "<div class='msg msg-success'><strong>Success! </strong>" + success + " Member key has been created successfully.<br /></div>";

}

else

{

msg.InnerHtml = "<div class='msg msg-danger'><strong>Error! </strong>Technical error occured, please try later.<br /></div>";

}

}

catch (Exception ex)

{

msg.InnerHtml = "<div class='msg msg-danger'><strong>Error! </strong>Oops! Something went wrong, Please try again later.<br /></div>";

}

}

 

Answers (5)