2012-01-09 61 views
1

如何強制對象在使用後進行處理以釋放內存?而且,你如何強制GC收集?如何強制處理對象/ GC

這是我的保存代碼。我注意到,每次執行這個函數時,我的內存消耗都會增加,最終會在幾次命中後導致內存不足錯誤。

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); 
    } 
} 

這裏是我的業務層類

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(); 
    } 
} 

回答

3

你應該處理您的DataContext。我看不到它在任何地方被移除,因此連接將保持打開狀態,並且可能會引用參考(阻止GC拾取它們)。這可能是導致問題的原因。如果您不想手動處理,則可以在using區塊內執行交易。

編輯迴應業務層更新 -

你可以用在using塊的方法是這樣的:

public void insert(CashExpense item) 
{  
    using(CEADataStoreDataContext CashExpensesDB = new CEADataStoreDataContext()) 
    { 
     CashExpensesDB.CashExpenses.InsertOnSubmit(item); 
     CashExpensesDB.SubmitChanges(); 
    } 
} 
+0

嗨,我該如何處理datacontext?我在業務層上有一個單獨的課程。在短時間內添加它.. – anonymous1110 2012-01-09 02:45:12

+0

您可以簡單地調用'datacontext.Dispose'方法 - http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.dispose.aspx – keyboardP 2012-01-09 02:48:09

+0

hi ,我已經添加了我的業務層類。請看看它謝謝。 – anonymous1110 2012-01-09 02:49:28

3

分配空變量引用你的對象,使用GC.Collect();強制垃圾收集。您可能需要連續調用兩次以加速整個過程中的不可訪問對象。

+0

tblCashExpenses = null;然後添加GC.COllect()2x?在我的嘗試塊? – anonymous1110 2012-01-09 02:43:25

+0

@ anonymous1110是的,看看這是否有幫助。 – dasblinkenlight 2012-01-09 02:54:55

-1

設置對象爲空,則調用:

GC.Collect的(); GC.WaitForPendingFinalizers();

+0

這不起作用,因爲一個GC.Collect()調用只是將對象移動到下一代。沒有辦法,保證GC.Collect()甚至可以做一些事情.. – 2017-11-23 09:07:59