c# - Too Many Objects causing out of memory exception -
quick overview of situation: text files on scheduled basis split character count, developing app splits each row out list of models depending on file parsing. e.g. persons text file 300 lines long create list of custom model "person". each item in list line in file. doing via below:
private static list<person> initializepeople(datamanager dm, string filepath) { list<person> people= new list<person>(); //get lines file loop through foreach (var line in dm.getdatalinesfromfile(filepath).tolist()) { person person = new person(); //this splits out character counted split line //into models format , adds list people.add((person)dm.getformatteddata(person, new formatmanger(), line)); } return people; }
this works fine until file millions of lines long. can see commiting cheeky nono in loop in instantiating new object each time loops. believe issue lies, starts struggling memory when miilions of objects being instantiated. better way of doing this? other way more memory effecient? appreciated.
do not materialize data (i.e. avoid tolist()
, toarray()
etc.) work ienumerable only.
first, check dm.getformatteddata
implementation should like
public ienumerable<string> getdatalinesfromfile(filepath) { // check neither readalllines nor readalltext there // check absence of tolist() , toarray() return file .readlines(filepath) // possible way of reading file .select(...) // possible, not necessary part .where(...); // possible, not necessary part }
then redesign initializepeople
that:
// note ienumerable<person> instead of list<person> public static ienumerable<person> initializepeople(datamanager dm, string filepath) { //todo: what's "nmy"? //todo: want new formatmanger() each people instance? return getdatalinesfromfile(filepath) .select(line => (person)dm.getformatteddata(nmy, new formatmanger(), line)); }
and of linq can have lot of flexibility, e.g.
file.writealllines(@"c:\myfile.txt", initializepeople(dm, @"c:\people.txt") .where(people => people.lastname == "smith") .select(people => string.format("{0} {1} years old", people.firstname, people.age));
Comments
Post a Comment