OutOfMemory

by Administrator 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);
            string columnName;

            SqlCeParameter dbParameter;

            foreach (System.Data.DataRow row in schemaTable.Rows)
            {
                columnName = (string)row["ColumnName"];
                int columnSize = (int)row["ColumnSize"];
                dbParameter = new SqlCeParameter(
                    "@" + columnName,
                    GetSqlDbType(row["ProviderType"].ToString()),
                    columnSize);

                if (!row.IsNull("NumericPrecision"))
                {
                    dbParameter.Precision = Convert.ToByte((short)row["NumericPrecision"]);
                }
                if (!row.IsNull("NumericScale"))
                {
                    dbParameter.Scale = Convert.ToByte((short)row["NumericScale"]);
                }
                int columnOrdinal = (int)row["ColumnOrdinal"];
                object pVal = DBNull.Value;
                try
                {
                    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!

Tags:

Comments (4) -

phentermine
phentermine
3/17/2006 4:43:40 AM #

I believe that to truly Love, is the ultimate
expression of the will to live. A
heart that truly loves is forever
young

Reply

paxil
2/19/2007 4:22:46 PM #

paxil

Reply

ultram
2/19/2007 4:23:07 PM #

ultram

Reply

cialis
2/19/2007 4:23:26 PM #

cialis

Reply

Pingbacks and trackbacks (159)+

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


About the author

Damon Payne is a Microsoft MVP specializing in Smart Client solution architecture. 

INETA Community Speakers Program

Month List

Page List

flickr photostream