Problem
For two days now I have been struggling with this problem from the side, and my ideas are running out. A little ... background: we have a WinForms application that needs to access the database, build a list of related objects in memory from this data, and then display it in a DataGridView. The important point is that we first populate the cache application (List), and then create a mirror of the local cache in the form on which the DGV lives (using the list constructor parameter).
Since it takes several seconds to load the data (the database is located on the local network server) to load, we decided to use BackgroundWorker and update the DGV only after the data was downloaded. However, it seems that when loading via BGW there is a memory leak ... or an error on my part. When downloading using the lock method, the application consumes about 30 MB of RAM; with BGW it's a jump of up to 80 MB! Although this may not seem like that in any case, our customers are not too happy with it.
Corresponding code
The form
private void MyForm_Load(object sender, EventArgs e) { MyRepository.Instance.FinishedEvent += RefreshCache; } private void RefreshCache(object sender, EventArgs e) { dgvProducts.DataSource = new List<MyDataObj>(MyRepository.Products); }
Repository
private static List<MyDataObj> Products { get; set; } public event EventHandler ProductsLoaded; public void GetProductsSync() { List<MyDataObj> p; using (MyL2SDb db = new MyL2SDb(MyConfig.ConnectionString)) { p = db.PRODUCTS .Select(p => new MyDataObj {Id = p.ID, Description = p.DESCR}) .ToList(); } Products = p;
To the end!
GetProductsSync or GetProductsAsync is called in the main thread, not shown above. Maybe GarbageCollector is just lost with two threads? Or does the task manager show incorrect values?
Will be welcome for any answers, suggestions, criticism.
source share