Reputation: 91
I'm reading data from an Oracle database using the DbDataReader (OracleDataReader) class. I'm running the program in Visual Studio 2010, with a 64-bit Windows 10 Enterprise laptop with 32GB RAM (20GB free), using .NET 3.5 (upgrading to a newer .NET version isn't an option. The application itself is a 32-bit application (not by choice).
The code looks something like this:
//...some code to setup database connection, command, etc.
DbDataReader reader = null;
int count = 0;
try {
reader = command.ExecuteReader();
if(reader.HasRows) {
while(reader.Read()) {
count++;
}
}
}
finally {
if(reader != null) { reader.Close(); }
}
Stack trace:
System.OutOfMemoryException was unhandled
Message=Exception of type 'System.OutOfMemoryException' was thrown.
Source=Oracle.DataAccess
StackTrace:
at Oracle.DataAccess.Client.OracleDataReader.Dispose(Boolean disposing)
at Oracle.DataAccess.Client.OracleDataReader.Close()
at Test.Program.Main(String[] args)
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
I've also had this stack trace a number of times:
System.OutOfMemoryException was caught
Message=Exception of type 'System.OutOfMemoryException' was thrown.
Source=Oracle.DataAccess
StackTrace:
at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure, Boolean bCheck)
at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, Object src, Boolean bCheck)
at Oracle.DataAccess.Client.OracleDataReader.Read()
at Test.Program.Main(String[] args)
I am not allocating any memory myself in this test case, I'm just incrementing an integer, yet I still run out of memory, which tells me that the DbDataReader is allocating memory and probably not deallocating it properly (or in a timely manner). I've tried manually doing garbage collection to see if that helps, but it doesn't. I've also looked into whether the rows it crashes on contain too much data and thus fill up remaining memory, but that doesn't add up either since much larger amounts of data are read from other rows (and discarded) prior to it crashing.
Any ideas/help are greatly appreciated, thank you!
Upvotes: 1
Views: 1146
Reputation: 91
After tinkering around a bit with various settings on the OracleCommand and the OracleDataReader, the problem turned out to be that the FetchSize was initially too high on the OracleDataReader.
Setting the FetchSize on the OracleDataReader after the command is executed resolves the problem and allows the application to run without any memory errors.
Upvotes: 2