Hello,
I’ve a “standard” problem that I cannot solve myself
I’ve a cerbuino bee, where I log some data on SD, using plain text files.
I cannot read and parse these files because I always “burn” RAM, and get out of memory exception.
Basically my code:
1 - access folder where I have file using System.IO.Directory.EnumerateFiles. Gets first file I may upload and go on. Then this will be repeated until directory is empty.
2 - open a file. Currently I’m using filestream with a little buffer as
Using fs As New System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None, 60)
3 - read lines one by one. Im not using the readLine but I tried to read using a memory leak woraround I was using on Panda and .NET 4.1. It is not rquired, but I’ve a lot of trouble so I tried this an other time.
4 - parse a single line and try to upload data via HTTP.
The memory leak is in step 1-2-3, because disabling step 4 I can see that available memory (Debug.GC…) decreases and I go out of memory.
Every heavy object (FileStream) is instanced with Using, so it’s disposed.
I cannot solve it
Where is the right approach to read some txt files, parse them and upload? I think that this is a task so common that there will be an affordable pattern.
My files are small: about 1KB, and every row is really little, just 4 semicolon separated numbers.
thanks
p.s. line reading is implemented as this:
public static string ReadLineUsingBuffer(System.IO.FileStream fs)
{
int newChar = 0;
int bufLen = 256;// 512; // NOTE: the smaller buffer size.
char[] readLineBuff = new char[bufLen];
int growSize = 256; // 512;
int curPos = 0;
while ((newChar = fs.ReadByte()) != -1)
{
if (curPos == bufLen)
{
if ((bufLen + growSize) > 0xffff)
{
throw new Exception();
}
char[] tempBuf = new char[bufLen + growSize];
Array.Copy(readLineBuff, 0, tempBuf, 0, bufLen);
readLineBuff = tempBuf;
bufLen += growSize;
}
readLineBuff[curPos] = (char)newChar;
if (readLineBuff[curPos] == '\n')
{
return new string(readLineBuff, 0, curPos);
}
if (readLineBuff[curPos] == '\r')
{
// what's this? :)
//if (fs.Peek() == 10)
//{
// fs.ReadByte();
//}
return new string(readLineBuff, 0, curPos);
}
curPos++;
}
if (curPos == 0) return null; // Null fix.
return new string(readLineBuff, 0, curPos);
}