Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7031279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:46:57+00:00 2026-05-28T00:46:57+00:00

How do you force objects to dispose after use in order to free up

  • 0

How do you force objects to dispose after use in order to free up memory? And, how do you force GC to collect?

Here’s my Save Code. I’ve noticed that, every time I execute this function, my memory consumption goes up that would eventually caused an out of memory error after a couple of hits.

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

Here’s My business layer class

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:46:58+00:00Added an answer on May 28, 2026 at 12:46 am

    You should dispose your DataContext. I can’t see it being removed anywhere, so the connection will remain open and references may be held on to (preventing the GC from picking them up). This may be what’s causing the problem. If you don’t want to dispose manually, you can perform the transaction within a using block.

    Edit in response to Business Layer update –

    You can wrap the methods in using blocks like this:

    public void insert(CashExpense item)
    {    
       using(CEADataStoreDataContext CashExpensesDB = new CEADataStoreDataContext())
       {
           CashExpensesDB.CashExpenses.InsertOnSubmit(item); 
           CashExpensesDB.SubmitChanges();
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After answering a question about how to force-free objects in Java (the guy was
Here's my use case. We are trying to narrow down a potential memory leak
Is there any way to force the persistence order of objects in JPA 2
If I have an object that I would like to force to be accessed
I'm looking to force my application to shut down, and return an Exit code.
Why does SWING always force me to mark some particular objects as final ?
If I have two objects in chipmunk (I'm using cocos2d-iphone), once I've detected that
I have four objects - for the sake of arguments, let say that they
I need to be able to gain access to objects after my activity has
There is a problem out of memory exception when exucuting this code. The code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.