Fluent NHibernate memory leak
fluent-nhibernate, memory-leaks, nhibernate
Solution
I'm such a noob, indeed I should not create a new session factory every time but use an existing one. What lead me into the wrong direction is that originally my code is hosted in a WCF service, in which I thought I had to create a new factory every time.
I have now made sure only 1 factory exists throughout the code and the memory consumption is stable.
Problem
I'm pulling my hair over what seems to be a memory leak but I cannot find the cause :( I built an application on my own PC, pretty heavy beast with alot of memory, so I never realised my process consumed so much memory. I only realised after releasing the code to a production environment with limited memory. I use quite a few different technologies and patterns, including WCF, but I have now come to the point where I stripped most of the code to find out what consumes so much memory... Here is the most simplified version of the app, 1 entity, 1 mapping, and a simple procedure that retrieves from the DB, while it's running, I can see the the process in the task manager growing endlessly: ``` using System; using System.Linq; using FluentNHibernate.Cfg; using FluentNHibernate.Cfg.Db; using FluentNHibernate.Mapping; using NHibernate; using NHibernate.Linq; namespace ConsoleApplication18 { public class Program { private static void Main() { var i = 0; while ( i < 1000000 ) { ISession session = BuildSessionFactory().OpenSession(); ITransaction transaction = session.BeginTransaction(); var users = session.Query< User >().ToList(); transaction.Commit(); transaction.Dispose(); transaction = null; session.Dispose(); session = null; Console.WriteLine( users.Count ); users = null; i++; } } private static ISessionFactory BuildSessionFactory() { return Fluently.Configure() .Database( MsSqlConfiguration .MsSql2008 .ConnectionString( "Data Source=SEB-PC\\SEBPC;Initial Catalog=Nice;Integrated Security=True" ) ) .Mappings( m => m.FluentMappings.AddFromAssemblyOf<User>() ) .BuildSessionFactory(); } } public class User { public virtual Guid Id { get; set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } public virtual string Email { get; set; } } public class UserMap : ClassMap< User > { public UserMap() { Id( x => x.Id ).UnsavedValue( Guid.Empty ); Map( x => x.FirstName ) .Length( 50 ) .Not.Nullable(); Map( x => x.LastName ) .Length( 50 ) .Not.Nullable(); Map( x => x.Email ) .Length( 254 ) .Not.Nullable() .Unique(); } } } ``` Am I doing something wrong? I got the 3rd party DLL's using nuget, and the versions are the following: FluentNHibernate: 1.3.0.733 Iesi.Collections: 1.0.1.0 NHibernate: 3.3.1.4000 Sorry if this is a newbie question, but I have been googling all over the place for over a week now :( Thanks! Seb :)