Wednesday 23 May 2012

Memory leak upgrading Enterprise Library from 3.1 to 5.0

La versión en castellano de este artículo puede encontrarse AQUÍ

I have been working recently in upgrade a project from .Net 3.5 to .Net 4 and in the way I have upgrade the Enterprise Library from version 3.1 to 5.0 and started using Unity, as it is no longer part of the entlib.

During the upgrade I did some of our tailored changes to the library, mainly the Data and Caching blocks, without much problems, as the code is still quite similar in the areas that required changes.

But once I started to test the application I noticed a big memory leak on it. Initially it wasn't obvious by I managed to work out a way to reproduce it consistently.

Using the profiler of my choice (YourKitProfiler, very nice tool indeed!) I found out that there were a nice leak of Unity objects. Monitoring were those objects were being created I ended up in the method we used to get a new database connection.

DatabaseProviderFactory factory = new DatabaseProviderFactory();  
SqlDatabase db = factory.CreateDefault() as SqlDatabase;  

Our usage of the Provider Factories wasn't the proper one as we weren't disposing them and we were creating a new instance each time we needed a database connection.

Looks like the usage of Unity as part of the enterprise library started to create a complex graph of objects each time you instantiate one of the factories, so if you don't dispose them properly the GC is not able to remove them.

I fixed the problem by using an static instance of the provider, so I use always the same object each time I need a database connection. Another option would have been wrapping that piece of code inside an using block, but it is up to you.

I assume a similar problem is present if you use of any of the Provider Factories in the Enteprise Library, not only on the Database one, so if you find that you have a memory leak in your application after upgrading from version 3.1, have a look at the way you use them.

Update: looks like disposing your providers doesn't work. Each time you create an instance of one of them the configuration file is parsed and an object graph representing it  is created by Unity, which seems its not disposed even if you dispose the provider. So try to reduce the number of providers you create to a minimun, using a singleton for example could be a good idea.

Otro blog de programación



El desarrollo software es una de mis pasiones, compartida con las que trata mi otro blog, y muchas, muchísimas más.

Pero hacer software es una de las cosas de las que va mi vida. Llevo trabajando en desarrollo unos cuantos años, comencé en el mundo de Java y fui saltando de una cosa a otra, hasta que finalmente he terminado como un programador .Net, y no tiene pinta de que sea algo que vaya a cambiar en el futuro cercano.

Disfruto compartiendo los problemas que dan dolores de cabeza durante horas, dias o incluso semanas y finalmente consigo solucionar. Los comparto con mis compañeros y otros amigos programadores, pero creo que sería capaz de compartirlo con unos cuantos más a través de este blog.

Aunque el lenguaje principal (en el que primero aparecerás las entradas) es el Inglés, trataré de poner traducciones de cada entrada en castellano, ya que me da la impresión de que no son muchos los blogs en nuestra lengua que hay por ahí sobre .Net, y supongo que habrá quien los preferirá aunque tampoco que nadie de nuestro gremio tenga demasiados problemas para seguirlos en ingles.

Y eso es prácticamente todo. Tan solo otro programador "friki" que quiere compartir las que cosas en las que pasa las horas con otros programadores "frikis", y quizás ayudarles en el proceso.

Perdonadme si no actualizo el blog muy a menudo, pero como os he dicho tengo demasiadas "pasiones" ;)

Another coding blog

Computer science and software development are two of my passions, shared with the ones my other blog is about and many, many others.

But developing software is one of the things my life is about. I have been working in development for a few years now, started in the Java world and move through different things, and finally I have end up as a .Net developer. And it doesn't seem its going to change in the near future.

I love to share those problems I have been struggling to fix for hours, days, or even weeks, and finally I manage to deal with. I do share them with my team mates, with my other coder friends, but I think I am going to be able to manage sharing it with some more people via this blog.

Although the primary language of the blog is going to be English, I will try to post translations of every entry in Spanish, my mother tongue, as I think there are not many blogs out there in Cervante's language about .Net... if there are I would be glad to see them!

So basically that is it. Just another geeky coder that wants to share the thing he spent the hours on with other geeky coders, and maybe help some of then in the way.

Forgive me if I don't update the blog much often, but I have to many passions ;)