4. February 2006 17:59
A lot of developers working with these great modern garbage collected single-inheritance object oriented programming languages on big powerful servers may go their whole career without actually seeing an OutOfMemoryException. This will happen from time to time on the compact framework and I'm happy to say I typically pay attention to what I'm doing and my hard to track down bug from yesterday is my first OOME. I have a sync process that merges data into SQL CE 2. To try to squeeze blood (performance) out of a rock (SQLCE 2.0) it prepares and caches update and insert commands. Here's a small piece of that code:
System.Data.DataTable schemaTable = dr.GetSchemaTable();
dbCommand = DataHub.Database.GetCommand(GetUpdateTemplate(ref schemaTable, pk, changeColumn), DataAccessObject.CONNECTION_STRING);
foreach (System.Data.DataRow row in schemaTable.Rows)
columnName = (string)row["ColumnName"];
int columnSize = (int)row["ColumnSize"];
dbParameter = new SqlCeParameter(
"@" + columnName,
dbParameter.Precision = Convert.ToByte((short)row["NumericPrecision"]);
dbParameter.Scale = Convert.ToByte((short)row["NumericScale"]);
int columnOrdinal = (int)row["ColumnOrdinal"];
object pVal = DBNull.Value;
pVal = dr.GetValue(columnOrdinal);
dbParameter.Value = pVal;
This worked for a long time until someone decided that a column of type ntext should be replicated. The column size for a field of type ntext happens to return something like 580million bytes. When the code tries to set the parameter value, it finds it can't allocate that much space. The maximum stack size in the compact framework v1 is 640k (reminds me of MS-DOS low memory, no one will ever need more than 640k); 640kb * 1024b/kb = 655,360bytes, or "not enough" and the OutOfMemoryException is thrown.
I feel a little sorry for anyone building a custom sync process using SQL CE. SQL Mobile is much nicer but does not remove the potential for things like this to happen. I have a project I'm working on which I'll open-source IF I get it done. I say if becuase I have occasionally announced some wacky plan here only to be distracted by a shiny object and said project dies on the vine.
Speaking of stack size optionsScalper was telling me about F# and how .... look, a penny!