Remove objects from memory

1

I have DataGridView in my project and a timer refresh of 5 seconds. I realized that the system was overloading the memory, because every time I list the previous data, they remain 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 to make your system lighter?

    
asked by anonymous 03.02.2015 / 12:19

2 answers

2

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

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

You may have a very large volume of data being loaded. That's another problem.

Do you need the same refresh ? This may not be the best way. But even if you do this every time you receive a new object, the former will be free to be removed from memory when the garbage collector desires.

Using the Entity Framework is not the most appropriate way to make your system light. Not that this will change a lot too but you certainly do not even consider the possibility of not using it.

    
03.02.2015 / 12:34
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 point 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: link

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;
    
03.02.2015 / 12:34