Remove objects from memory

Asked

Viewed 766 times

1

I have a DataGridView in my project and a timer with refresh 5 seconds. I realized that the system was overloading the memory, as each time I listed the previous data remained in memory. This is the code I use to list:

Using (DBEntities db = new DBEntities())
{
      Dgv_Os.Datasource = null;
      List <OS> ostecnicos = (from o in db.OS select o).toList ();
      Dgv_Os.Datasource = ostecnicos;
}

What methods to optimize code make the system lighter?

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

2

@user18748, there is not much to optimize a simple query like this. But you can try to set the following properties:

db.Configuration.AutoDetectChangesEnabled = false;
db.Configuration.ValidateOnSaveEnabled = false;

If at any time you need to call Savechanges(), be sure to do the following:

((IObjectContextAdapter)db).ObjectContext.DetectChanges();
db.SaveChanges();

As mentioned in the link below, it may be interesting to use a Bindinglist: http://madgeek.com/articles/leaks/leaks.en.html

List <OS> ostecnicos = (from o in db.OS select o).toList();
var bindingList = new BindingList<OS>(ostecnicos);
var source = new BindingSource(bindingList, null);
Dgv_Os.Datasource = source;
  • @user18748, I edited the answer to add more prossibility.

2

There is no way to optimize the presented section. It seems to me very suitable. O . Net will take care of releasing the memory when it is possible and necessary and you do not need to worry about it in this case. At least for this section. It may even be that other parts have problems and are not allowing the release of memory.

I even have my doubts whether memory is really overloaded, whether you used reliable methods to determine this.

Maybe you have a very large data volume being loaded. There is another problem.

Did you need the refresh same? This may not be the best way. But even doing this every time ostecnicos receive a new object, the previous one will be free to be removed from memory when the Garbage Collector wish for.

Using the Entity Framework is not the most appropriate way to make the system lightweight. Not that it’s going to change much either but you certainly don’t even consider not using it.

  • The problem is that the memory is only being released with the closing of the form, and the refresh of 5 seconds is necessary, because the system is displayed on a television for technicians to view the.

  • 2

    Still haven’t convinced me that memory is not being released. Prove that it is not. Prove that it is accumulating in each refresh. I keep saying that the refresh should not be necessary. This website informs me of news happening in it that can interest me without doing refresh of any kind.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.