How to free memory when large number of DataTables are used?
when try use large number of datatables, app faced memory exception. in fact, app keeps datatable , merge them together. after merge try dispose objects it's not sufficient.
public class phase { . . . datatable dt = new datatable("result"); command = new sqlcommand(olapcommand, commandconnection); using (command) { command.commandtimeout = 0; sqldataadapter sqldataa = new sqldataadapter(command); sqldataa.fill(dt); sqldataa.dispose(); sqldataa = null; } while (!olap.results.tryadd(this.id, dt)) ; bool locktacken = false; spinlock.enter(ref locktacken); // of data gathered if (olap.passes.count == olap.numquery) merger.resultqueue.enqueue(olap); if (locktacken) spinlock.exit(); public class merger { olap olap = new olap(); using (olap) { while (!resultqueue.trydequeue(out olap)) ; //lock (sync) //{ //datatable olapresults = (datatable)olap.results. toarray(); int[] key = olap.results.select((x) => x.key).toarray(); datatable dt = new datatable(); using (dt) { olap.results.trygetvalue(key[0], out dt); (int = 1; < key.count(); i++) { dt.merge(olap.results[key[i]]); } dt.clear(); dt.dispose(); dt = null; } olap.dispose(); olap = null; } public class olap : idisposable { public olap() { commands = new concurrentdictionary<int, string>(); results = new concurrentdictionary<int, datatable>(); passes = new concurrentdictionary<int, bool>(); unioncommand = ""; numquery = 0; } // flag: has dispose been called? bool disposed = false; ~olap() { foreach (datatable dt in results.values) { dt.clear(); //debug.writeline("olap.dt cleared."); dt.dispose(); } } public void dispose() { dispose(true); gc.suppressfinalize(this); } protected virtual void dispose(bool disposing) { if (disposed) return; if (disposing) { foreach (datatable dt in results.values) { dt.clear(); //debug.writeline("olap.dt cleared."); dt.dispose(); } } //gc.collect(gc.getgeneration(this), gccollectionmode.optimized); disposed = true; }
how fix it?
when try use large number of datatables, app faced memory exception. in fact, app keeps datatable , merge them together. after merge try dispose objects it's not sufficient.
a list<t> uses 6 times less memory datatable. should able merge list<t>(s) using bing or google.
http://lauteikkehn.blogspot.com/2012/03/datatable-vs-list.html
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment