Wednesday, March 7, 2012

rda.SubmitSql cause outofmemoryexception

My application create quite a lot log file to submit to the server under WiFi coverage to monitor the application. (log is just txt file). as time goes on, submitsql method throw OutOfMemoryException. I agree my log file is quite big, 80K each and 6 log files to submit.
My log submitter is an instance object. I also tried static object but it doesn't help either.

I do dispose my rda object when it is out.

Is any way I can fix this?Could you provide som sample code - I do not understand the connection between the log files and submitsql - are you submitting the log file text in an insert statement?|||

Sure.

public LogSubmitter()

{

//some local objects.

public void SubmitLog(string logName, SqlCeRemoteDataAccess rda)

{

try

{

//

//read text from log file and append it to StringBuilder: logBuilder

//

string logCmd = "EXEC InsertLog "+"'"+logBuilder.ToString()+"'";

rda.SubmitSql(logCmd ,remoteConnStr);

}

catch(Exception ex)

{

Log.WriteException(ex);

}

}

}

where, InsertLog is a stored procedure on the server.

Is there is problem with this code?

PS: I only submit log when device is under WiFi coverage. rda object is disposed in the caller.

|||Have a close look at your StringBuilder - have you set a useful initial capacity - otherwise you should try that.|||

Thank you very much.

You are right, I didn't set initial capacity. Another reason could be I submit 6 log files continously, each log about takes 80k (max).

But one thing I cannot understand is that I called logBuilder.Remove(0,logBuilder) before submitting each log, so each stringbuilder should be empty before appending new log.

|||Did we solve your outofmemory exeption problems, then?|||

Just got log from my test scanner.

It still has this exception. I will do more changes with "capacity" property.

By the way, does StringBuilder has maximum capacity?

Cheers

|||It does, but the MacCapacity property is not available in NETCF, (according to docs) so who knows what it may be?|||

Thanks Erik.

Is StringBuilder's memory reused if we remove contents from it and then append string again?

|||

Yes, ErikJ, I resolved this issue by checking the length before appending new chars.

The StringBuilder throws "OutofMemoryException" when it's length is greater than 2359294.

Thanks.

|||

Hi, ErikEJ;

Now I have this exception again. The "Length" property is 57341 when this happens.

And I call "Remove" method after I finish submitting a log. It looks like GC doesn't collect those memory.

Thanks.

No comments:

Post a Comment