Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

Linq and higj use of memory

Hello,

   I`ve writtern the code above and when I try upload more than 100K records the RAM goes from 2.7G to to 6G. Is the garbage collection not working well? Is my cod ewrong? I can't dispose the objects...

  Thanks,
  Marco Castro
foreach (var item in entity.LinqQuery.ToList()))
{
  OLAP_VENDAS olapv = new OLAP_VENDAS();
  olapv.VENDAS = new Misc().GenID("OLAP_VENDAS");
  olapv.VENDAS_CABECALHO = olap.VENDAS_CABECALHO;
  olapv.QT_CONSIG = item.EnvioConsignacao;
  try
  {
    entity.AddObject(olapv.GetType().Name, olapv);
    if (i++ > 500)
    {
      entity.SaveChanges();
      // It connects to the database again
      entity = ActiveEntity.GetEnt();
      i = 0;
    }
   }
   catch (Exception e)
   {
      throw new FaultException<FaultContract>(new FaultContract(e.ToString()));
    }
}

Open in new window

0
MarcoCastro
Asked:
MarcoCastro
  • 3
1 Solution
 
crysallusCommented:
Your logic with the i variable in the if statement looks bad to me. By resetting i to 0, it's never reaching 500 (assuming it's not initialised to a value above 500, which you've left out of the code sample), which is an infinite loop, which would be why your memory is spiralling out of control.

You might want to explain what your code is supposed to be doing, as that might make it easier to help you. Without that, I can't really offer you alternatives.
0
 
MarcoCastroAuthor Commented:
Hi, the i variable is not a problem (it is i++, so it is incremented). The reason of i is: load 500 AddObject and then save them in a single process. It is to make the process as fast as possible.

The code is intended to transfer a huge amount of data from one table to another. I can`t do it directly in the database because my LinqQuery if populated by a Dot.Net routine. After populated its content is transfered to a table in the SQL FireBird. So, for each record in LinqQuery one new object is the created as an entity and saved in the database.

thanks,
Marco Castro
0
 
MarcoCastroAuthor Commented:
The answer is: detach all created objects.
0
 
MarcoCastroAuthor Commented:
I got the answer by myself.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now