c# - DataTable does not release memory -
i have data loading process load big amount of data datatable data process, every time when job finished dataloader.exe(32bit, has 1.5g memory limit) not release memory being used.
i tried 3 ways release memory:
- datatable.clear() call datatable.dispose() (release 800 mb memory still increase 200 mb memory every time data loading job finish, after 3 or 4 times of data loading, out of memory exception thrown because exceeds 1.5 g memory in total)
- set datatable null (no memory released, , if choose load more data, out of memory exception thrown)
- call datatable.dispose() directly (no memory released, , if choose load more data, out of memory exception thrown)
following code tried testing(in real program not called recursively, triggered directory watching logic. code testing. sorry confusion.):
using system; using system.collections.generic; using system.linq; using system.text; using system.data; namespace datatable_memory_test { class program { static void main(string[] args) { try { loaddata(); console.readkey(); } catch (exception ex) { console.writeline(ex.tostring()); console.readkey(); } } private static void loaddata() { datatable table = new datatable(); table.columns.add("dosage", typeof(int)); table.columns.add("drug", typeof(string)); table.columns.add("patient", typeof(string)); table.columns.add("date", typeof(datetime)); // fill data table make take 1 g memory. (int = 0; < 1677700; i++) { table.rows.add(25, "indocin", "david", datetime.now); table.rows.add(50, "enebrel", "sam", datetime.now); table.rows.add(10, "hydralazine", "christoff", datetime.now); table.rows.add(21, "combivent", "janet", datetime.now); table.rows.add(100, "dilantin", "melanie", datetime.now); } console.writeline("data table load finish: please check memory."); console.writeline("press 0 clear , dispose datatable, press 1 set datatable null, press 2 dispose datatable directly"); string key = console.readline(); if (key == "0") { table.clear(); table.dispose(); console.writeline("datatable disposed, data table row count {0}", table.rows.count); gc.collect(); long lmemorymb = gc.gettotalmemory(true/* true = collect garbage before measuring */) / 1024 / 1024; // memory in megabytes console.writeline(lmemorymb); } else if (key == "1") { table = null; gc.collect(); long lmemorymb = gc.gettotalmemory(true/* true = collect garbage before measuring */) / 1024 / 1024; // memory in megabytes console.writeline(lmemorymb); } else if (key == "2") { table.dispose(); gc.collect(); long lmemorymb = gc.gettotalmemory(true/* true = collect garbage before measuring */) / 1024 / 1024; // memory in megabytes console.writeline(lmemorymb); } console.writeline("job finish, please check memory"); console.writeline("press 0 exit, press 1 load more data , check if throw out of memory exception"); key = console.readline(); if (key == "0") { environment.exit(0); } else if (key == "1") { loaddata(); } } } }
if move part when ask reiterate outside of function, memory freed correctly (only tested method 1(clear , dispose)):
static void main(string[] args) { try { string key; { loaddata(); console.writeline("job finish, please check memory"); console.writeline("press 0 exit, press 1 load more data , check if throw out of memory exception"); key = console.readline(); } while (key == "1"); } catch (exception ex) { console.writeline(ex.tostring()); console.readkey(); } }
probably, objects' memory freed when out of scope
Comments
Post a Comment