While making some final tests of a class-library that I'm writing for Windows Mobile (using Compact Net Framework 2.0), I ran into an OOM-exception.
Basically, my library loads first a dictionary-file (an ordinary text file with a word list) and thereafter another file based upon the dictionary (I call it KeyMap) which size is more or less the same of the previously loaded dictionary.
Everything worked fine (using the emulator and my real device) with above files until I tried to load a Spanish-dictionary which has a size of approximately 2.7MB. The other language dictionaries I have used so far without any OOM-exceptions amounts to approximately 1.8MB each. With the Spanish dictionary, I can load the first file without any problems but when I try to read the second file, I get the OOM-error.
Below I have written the code that I am using. Basically I read the files and assign its contents to a string-variable (DictData and TextKeyMap). Then I make a Split on the string-variable to pass on the contents to a string-array (Dict and KeyMap).
'Loading Dictionary works
Dim ReadDictionary As StreamReader = New StreamReader(DictPath, Encoding.UTF8)
DictData = ReadDictionary.ReadToEnd()
ReadDictionary.Close()
Dict = DictData.ToString.ToUpper.Split(mySplitSep.ToCharArray) 'mySplitSep=chr(10)
DictData = "" 'perhaps "nothing" is better
'Loading KeyMap gives me error
Dim ReadHashKeyMap As StreamReader = New StreamReader(HashKeyMapPath, Encoding.UTF8)
TextKeyMap = ReadHashKeyMap.ReadToEnd() '<-- OOM-error
ReadHashKeyMap.Close()
KeyMap = TextKeyMap.ToString.Split(mySplitSep.ToCharArray) 'mySplitSep=chr(10)
TextKeyMap = "" 'perhaps "nothing" is better
I am a hobby-programmer with no expert-knowledge so my code shown above can probably be improved. Instead of using ReadToEnd, I tried to read each line in a For-loop but I got the same error (it was also slower).
I presume the error is due to the limitation of 32MB of contiguous memory in Windows Mobile.
Anyone of you who can help me out, perhaps by suggesting some alternative solutions? Maybe the problem is due to my crappy code shown above? What about, loading the second file in another thread? Could this work?
All help I can get will be highly appreciated.
Edit: I asked a similar question some time ago (here) but that one was more related to dealing with the reception of bytes and was resolved using chunks. In this case, I am dealing with strings.
Edit2: This library is a spellchecking-library. It works quite well and implements some quite advance techniques such as Soundex- and DoubleMetaPhone-algorithms. The only major problem so far is the problem mentioned above with a huge text-file for Spanish. Other dictionaries are OK. For more info, please see this link