Comment forcer les objets à être éliminés après utilisation afin de libérer de la mémoire ? Et, comment forcer le GC à collecter ?
Voici mon code de réduction. J'ai remarqué qu'à chaque fois que j'exécute cette fonction, ma consommation de mémoire augmente, ce qui peut provoquer une erreur de mémoire après quelques essais.
protected void btnSaveEmptyOC_Click(object sender, EventArgs e)
{
try
{
if (ViewState["ServiceDetailID"].ToString() != null)
{
CashExpense tblCashExpenses = new CashExpense();
Guid CashExpensesID = Guid.NewGuid();
tblCashExpenses.CashExpensesID = CashExpensesID;
tblCashExpenses.ServiceDetailsID = new Guid(ViewState["ServiceDetailID"].ToString());
tblCashExpenses.Description = txtDescriptionEmptyOC.Text;
tblCashExpenses.Quantity = Decimal.Parse(txtQTYEmptyOC.Text);
tblCashExpenses.UnitCost = Decimal.Parse(txtUnitCostEmptyOC.Text);
tblCashExpenses.CreatedBy = User.Identity.Name;
tblCashExpenses.DateCreated = DateTime.Now;
tblCashExpenses.CashExpensesTypeID = "OTHER";
CashExpenses_worker.insert(tblCashExpenses);
CashExpenses_worker.submit();
//Clear items after saving
txtDescriptionEmptyOC.Text = "";
txtQTYEmptyOC.Text = "";
txtUnitCostEmptyOC.Text = "";
ValidationMessage.ShowValidationMessage(MessageCenter.CashExpenseMaintenace.InsertOC2, "SaveEmptyOC", this.Page);
MyAuditProvider.Insert(this.GetType().ToString(), ViewState["MarginAnalysisID"].ToString(), MessageCenter.Mode.ADD, MessageCenter.CashExpenseMaintenace.InsertOC2, Page.Request, User);
divOtherCost.Visible = false;
grd_othercost.Visible = true;
btnaddothercost.Visible = true;
tblCashExpenses = null;
}
else
{
ValidationMessage.ShowValidationMessage(MessageCenter.CashExpenseMaintenace.SaveServiceDetailOC, "SaveEmptyOC", this.Page);
}
}
catch
{
ValidationMessage.ShowValidationMessage(MessageCenter.CashExpenseMaintenace.InsertOCError, "SaveEmptyOC", this.Page);
}
finally
{
//Rebinds the Grid
populategrd_othercost();
Dispose();
GC.SuppressFinalize(this);
}
}
Voici ma classe de couche métier
public class CashExpensesBL
{
CEADataStoreDataContext CashExpensesDB = new CEADataStoreDataContext();
public IEnumerable<CashExpense> get()
{
return CashExpensesDB.CashExpenses;
}
public IEnumerable<CashExpense> get(Expression<Func<CashExpense, Boolean>> express)
{
return CashExpensesDB.CashExpenses.Where(express);
}
public void insert(CashExpense item)
{
CashExpensesDB.CashExpenses.InsertOnSubmit(item);
}
public void delete(CashExpense item)
{
CashExpensesDB.CashExpenses.DeleteOnSubmit(item);
}
public void deleteDC(Guid servicedetailid)
{
CashExpensesDB.sp_deleteDefaultCost(servicedetailid);
}
public void submit()
{
CashExpensesDB.SubmitChanges();
}
}