Linq and higj use of memory

Posted on 2011-02-23
Last Modified: 2012-05-11

   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...

  Marco Castro
foreach (var item in entity.LinqQuery.ToList()))
  OLAP_VENDAS olapv = new OLAP_VENDAS();
  olapv.VENDAS = new Misc().GenID("OLAP_VENDAS");
  olapv.QT_CONSIG = item.EnvioConsignacao;
    entity.AddObject(olapv.GetType().Name, olapv);
    if (i++ > 500)
      // 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

Question by:MarcoCastro
  • 3

Expert Comment

ID: 34966620
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.

Author Comment

ID: 34969139
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.

Marco Castro

Accepted Solution

MarcoCastro earned 0 total points
ID: 34991704
The answer is: detach all created objects.

Author Closing Comment

ID: 35034603
I got the answer by myself.

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question