OutOfMemory Exception during deserialization

889 views
Skip to first unread message

Sarnam

unread,
May 11, 2012, 4:01:25 AM5/11/12
to mongod...@googlegroups.com
Hi ,
Please see the log:
Exception occured in ExecuteQuery() method of MongoDB Implmentation. Details are Server state:Unknown, Error message:An error occurred while deserializing the FieldList property of class Eclipse.Core.Model.DynamicFormData: Exception of type 'System.OutOfMemoryException' was thrown., stack trace:   at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMember(BsonReader bsonReader, Object obj, BsonMemberMap memberMap) in E:\mongodb-mongo-csharp-driver-142\Bson\Serialization\BsonClassMapSerializer.cs:line 469
   at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) in E:\mongodb-mongo-csharp-driver-142\Bson\Serialization\BsonClassMapSerializer.cs:line 159
   at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) in E:\mongodb-mongo-csharp-driver-142\Bson\Serialization\BsonClassMapSerializer.cs:line 77
   at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) in E:\mongodb-mongo-csharp-driver-142\Bson\Serialization\BsonSerializer.cs:line 223
   at MongoDB.Driver.Internal.MongoReplyMessage`1.ReadFrom(BsonBuffer buffer, IBsonSerializationOptions serializationOptions) in E:\mongodb-mongo-csharp-driver-142\Driver\Internal\MongoReplyMessage.cs:line 105
   at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions serializationOptions) in E:\mongodb-mongo-csharp-driver-142\Driver\Internal\MongoConnection.cs:line 442
   at MongoDB.Driver.MongoCursorEnumerator`1.GetReply(MongoConnection connection, MongoRequestMessage message) in E:\mongodb-mongo-csharp-driver-142\Driver\Core\MongoCursorEnumerator.cs:line 296

Kindly help me.


Thanks
Sarnam Singh

Adrien Mogenet

unread,
May 11, 2012, 1:36:03 PM5/11/12
to mongod...@googlegroups.com
What are you trying to deserialize ?
What are your heapspace parameters ?

craiggwilson

unread,
May 11, 2012, 3:14:20 PM5/11/12
to mongod...@googlegroups.com
Could you post the classes you are attempting to deserialize to as well as the json document in the database that is being deserialized?

Sarnam

unread,
May 14, 2012, 1:40:32 AM5/14/12
to mongod...@googlegroups.com

To clarify the thing , here is the class (object) & mongoDb Implementation :

In the given Code , most cause of this error comes from From (Linq Expression) method and ExceuteQuery() method. So please have a look at the code given below:

Class (to be serialized or deserialized ):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using Eclipse.Core.Enums;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.Serialization.Attributes;
namespace Eclipse.Core.Model
{
    [Serializable]
    [DataContract]
    public class DynamicFormData : ContentBase
    {
       
        public virtual ModerationStatus Status { get; set; }

        [DataMember]
        public int FormId { get; set; }
        [DataMember]
        public int SiteId { get; set; }
        [DataMember]
        public string IPAddress { get; set; }
        [DataMember]
        public DateTime InsertedOn { get; set; }

       
        [DataMember]
        [Column(TypeName = "xml")]
        public string UserContent { get; set; }

       
        [DataMember]
        [Column(TypeName = "xml")]
        public string Tags { get; set; }

       
        [DataMember]
        [ForeignKey("Status")]
        public int StatusId { get; set; }

        [DataMember]
        public DateTime ApprovedOn { get; set; }
        [DataMember]
        public int ApprovedBy { get; set; }
       
        [DataMember]
        public int ModifiedBy { get; set; }
        [DataMember]
        public DateTime ModifiedOn { get; set; }


        private List<DynamicFormField> _fieldsList;
       
       
        public List<DynamicFormField> FieldList
        {
            get
            {
                return _fieldsList;
            }
            set
            {
                _fieldsList = value;
                if (_fieldsList!=null)
                {
                    UserContent = Utility.Serialize<List<DynamicFormField>>(_fieldsList);   
                    //UserContent = string.Empty;
                }
            }
        }

        private List<string> _tagList;
      
       
        public List<string> TagList
        {
            get
            {
                return _tagList;
            }
            set
            {
                _tagList = value;
                if (_tagList != null)
                {
                   // Tags = string.Empty;
                    Tags = Utility.Serialize<List<string>>(_tagList);
                }
            }
        }
       
        // TODO : Remove these two methods.
        public List<DynamicFormField> GetFields()
        {
            return _fieldsList;
        }

        public string GetTags()
        {
            return string.Join(",", Utility.DeSerialize<List<string>>(Tags).ToArray());
        }
    }

   
}

MongoDb Implementation:

public class MongoDBPersistenceServiceV2 : Eclipse.Core.Services.Persistence.IPersistanceService
    {
        StringBuilder groupfilterExpressionBuilder = new StringBuilder();
        private string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
        static string databaseName = "EclipseV2";
        static MongoServer server = null;
        MongoDatabase db = null;
        ILoggingService logginService = DependencyResolver.Current.GetService<ILoggingService>();
        static MongoDBPersistenceServiceV2()
        {
            server = GetConnection();
            BsonClassMap.RegisterClassMap<DynamicFormData>(cm =>
            {
                cm.AutoMap();
                cm.UnmapProperty(c => c.UserContent);
                cm.UnmapProperty(x => x.Tags);

            });
            BsonClassMap.RegisterClassMap<Site>();
            BsonClassMap.RegisterClassMap<DynamicForm>();
            BsonClassMap.RegisterClassMap<DynamicFormQuery>();
            BsonClassMap.RegisterClassMap<DynamicFormField>();

        }
        public MongoDBPersistenceServiceV2()
        {
            db = server.GetDatabase(databaseName);
        }
        public List<T> List<T>() where T : Core.ContentBase
        {
            logginService.Info("List() method called at :" + DateTime.Now);
            List<T> retObject = new List<T>();
            try
            {
                MongoCollection collection = db.GetCollection(typeof(T).ToString());
                MongoCursor<T> mongoCursor = collection.FindAllAs<T>();
                using (var enumerator = mongoCursor.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        retObject.Add(enumerator.Current);
                    }
                }
            }
            catch (Exception ex)
            {
                logginService.Info("Exception occured in List() method of MongoDB Implmentation. Details are Server state:{0}, Error message:{1}, stack trace:{2} :", db.Server.State, ex.Message, ex.StackTrace);
            }

            logginService.Info("List() method ended at :" + DateTime.Now);
            return retObject;
        }

        public void Create<T>(T obj_) where T : Core.ContentBase
        {

            try
            {

                MongoCollection seq = db.GetCollection(typeof(T).ToString() + "_Seq");
                SequenceInfo siteSeq = seq.FindOneAs<SequenceInfo>(Query.EQ("CollectionName", typeof(T).ToString() + "_Seq"));
                if (siteSeq != null)
                {
                    siteSeq.SequenceNumber = siteSeq.SequenceNumber + 1;
                    seq.Save<SequenceInfo>(siteSeq);
                }
                else
                {
                    siteSeq = new SequenceInfo();
                    siteSeq.SequenceNumber = 1;
                    siteSeq.CollectionName = typeof(T).ToString() + "_Seq";
                    seq.Save<SequenceInfo>(siteSeq);
                }
                obj_.Id = siteSeq.SequenceNumber;
                MongoCollection collection = db.GetCollection(typeof(T).ToString());
                SafeModeResult result = collection.Save<T>(obj_);


            }
            catch (Exception ex)
            {
                logginService.Info("Exception occured in Create() method of MongoDB Implmentation. Details are Server state:{0}, Error message:{1}, stack trace:{2} :", db.Server.State, ex.Message, ex.StackTrace);
            }




        }
        public void Update<T>(T obj_) where T : Core.ContentBase
        {

            try
            {
                MongoCollection collection = db.GetCollection(typeof(T).ToString());
                collection.Save(obj_);

            }
            catch (Exception ex)
            {
                logginService.Info("Exception occured in Update() method of MongoDB Implmentation. Details are Server state:{0}, Error message:{1}, stack trace:{2} :", db.Server.State, ex.Message, ex.StackTrace);
            }



        }

        public T Find<T>(params object[] keyValues) where T : Core.ContentBase
        {
            logginService.Info("Find() method called at :" + DateTime.Now);
            T retObject = null;
            try
            {

                MongoCollection collection = db.GetCollection(typeof(T).ToString());
                var query = Query.EQ("_id", Convert.ToInt32(keyValues[0]));
                retObject = collection.FindOneAs(typeof(T), query) as T;


            }
            catch (Exception ex)
            {
                logginService.Info("Exception occured in Find() method of MongoDB Implmentation. Details are Server state:{0}, Error message:{1}, stack trace:{2} :", db.Server.State, ex.Message, ex.StackTrace);
            }
            logginService.Info("Find() method ended at :" + DateTime.Now);
            return retObject;
        }

        public List<T> Where<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : Core.ContentBase
        {
            logginService.Info("where() method called at :" + DateTime.Now);
            List<T> retObject = new List<T>();

            try
            {

                MongoCollection collection = db.GetCollection(typeof(T).ToString());

                retObject = collection.AsQueryable<T>().Where(expression).ToList<T>();

                using (var iterator = collection.AsQueryable<T>().Where(expression).GetEnumerator())
                {
                    while (iterator.MoveNext())
                    {
                        retObject.Add(iterator.Current);
                    }
                }
            }
            catch (Exception ex)
            {
                logginService.Info("Exception occured in Where(Linq Expression) method of MongoDB Implmentation. Details are Server state:{0}, Error message:{1}, stack trace:{2} :", db.Server.State, ex.Message, ex.StackTrace);
            }

            logginService.Info("where() method ended at :" + DateTime.Now);
            return retObject;
        }



        public void Remove<T>(T obj_) where T : Core.ContentBase
        {

            try
            {

                MongoCollection collection = db.GetCollection(typeof(T).ToString());
                var query = Query.EQ("_id", obj_.Id);
                var sortBy = SortBy.Ascending(new string[] { "_id" });
                collection.FindAndRemove(query, sortBy);


            }
            catch (Exception ex)
            {
                logginService.Info("Exception occured in Remove() method of MongoDB Implmentation. Details are Server state:{0}, Error message:{1}, stack trace:{2} :", db.Server.State, ex.Message, ex.StackTrace);
            }


        }

        public List<T> Where<T>(string expression, string sortExpression_, string sortDirection, int pageIndex, int pageSize, int pagesCount) where T : Core.ContentBase
        {

            IEnumerable<T> temp = null;
            try
            {

                MongoCollection collection = db.GetCollection(typeof(T).ToString());
                temp = collection.AsQueryable<T>().Where(expression).OrderBy(sortExpression_ + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageSize);


            }
            catch (Exception ex)
            {
                logginService.Info("Exception occured in where() method of MongoDB Implmentation. Details are Server state:{0}, Error message:{1}, stack trace:{2} :", db.Server.State, ex.Message, ex.StackTrace);
            }



            return temp.ToList<T>();
        }

      

        public List<DynamicFormData> ExecuteQuery(object queryExpression_)
        {
            logginService.Info("Execute Method() method called at :" + DateTime.Now);
            List<DynamicFormData> retObject = new List<DynamicFormData>();
            IMongoQuery qry = (IMongoQuery)queryExpression_;
            try
            {
                MongoCollection collection = db.GetCollection(typeof(DynamicFormData).ToString());
                MongoCursor<DynamicFormData> mongoCursor = collection.FindAs<DynamicFormData>(qry);

                using (var iterator = mongoCursor.GetEnumerator())
                {
                    while (iterator.MoveNext())
                    {
                        retObject.Add(iterator.Current);
                    }
                }
            }
            catch (Exception ex)
            {
                logginService.Info("Exception occured in ExecuteQuery() method of MongoDB Implmentation. Details are Server state:{0}, Error message:{1}, stack trace:{2} :", db.Server.State, ex.Message, ex.StackTrace);
            }
            logginService.Info("ExecuteQuery() method ended at :" + DateTime.Now);
            return retObject;

        }

       
      
       

        private static MongoServer GetConnection()
        {
            MongoServerSettings settings = new MongoServerSettings();
            settings.MaxConnectionLifeTime = new TimeSpan(0, 0, 600);
            settings.ConnectTimeout = new TimeSpan(0, 0, 600);
            settings.MaxConnectionIdleTime = new TimeSpan(0, 0, 600);
            settings.WaitQueueTimeout = new TimeSpan(0, 0, 600);
            settings.SocketTimeout = new TimeSpan(0, 0, 600);
            settings.SafeMode = SafeMode.True;
            settings.SlaveOk = true;
            settings.ConnectionMode = ConnectionMode.Direct;
            settings.Server = new MongoServerAddress("172.16.10.8", 27017);
            //settings.Server = new MongoServerAddress("127.0.0.1", 27017);
            settings.MinConnectionPoolSize = 1;
            settings.MaxConnectionPoolSize = 500;

            MongoServer server = new MongoServer(settings);

            return server;
        }


        public string searchStr { get; set; }

    }

    internal class SequenceInfo
    {
        public int Id { get; set; }
        public string CollectionName { get; set; }
        public int SequenceNumber { get; set; }
    }


So Please suggest me, as what code snippet is wrong.

Thanks
Sarnam Singh

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/Vsbmb7hO1RUJ.

To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

craiggwilson

unread,
May 14, 2012, 9:23:28 AM5/14/12
to mongod...@googlegroups.com
I can't even get this to compile.  For instance, your "Where" method takes a bunch of strings and then passes them to methods that take lambda expressions.  Regardless, you still haven't provided us with the query that is actually failing or the structure of your document in the database.  Without knowing what you are doing, it'll be pretty hard to help you out.

Most likely, you have a circular reference in your code.  Perhaps DynamicFormField has a reference to it's parent DynamicFormData?
To unsubscribe from this group, send email to mongodb-user+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages