Background: Asp.net project (VB.Net) using Ninject 2.2.0.0 IoC, Repository pattern and Service layer
Ninject bindings use InRequestScope()
Problem: Project is about to go live but stress testing revealed that the site is using memory at an high rate and is not disposing of it after use. To give an idea about this: Memory usage of about 70 - 100 MB per user instance per business process in about 2 minutes, so 16 simulated users managed to cause a memory (& CPU) overload and a site crash in under 10 minutes. (Server has 8GB RAM and no other significant processes running on it).
Have tried the following solutions: Tried implementing Using blocks with Dispose in the BaseController (inherited by all Controllers), in the base Repository and in the main database context.
Site couldn't get loaded due to the dbcontext being disposed off immediately so had to remove that from dispose.
Using ANTS memory profiler didn't really but ANTS performance profiler showed that Ninject was unable to get to the Dispose function. It was trying to look for DisposableObject.cs in C:\Projects\Ninject\Maintenance2.2\ninject\src\Ninject\Infrastructure\Disposal folder. Couldn't find it there so crashed. ANTS Reflector did decompile the same DisposableObject code from Ninject.dll so don't know why it was looking for it in it's non-existent default directory.
So tried upgrading Ninject from 2.2.0.0. to v3 using Nuget PM> Install-Package Ninject.MVC3
After a few alterations, got it working but am now stuck on teh ActionFilterAttribute which was in version 2.2.0.0 getting its kernel bindings using NinjectFactoryController like so:
Private _mailService As IEmailService
_mailService = New StandardKernel(New NinjectControllerFactory.QuickQuoteServices()).Get(Of IEmailService)()
Now for Ninject.MVC3, NinjectFactoryController has been commented out and all bindings are now in the register service part of NinjectWebCommon.vb in App_Start folder (VB code obtained from the second code block on page https://gist.github.com/923618).
So had to change the ActionFilterAttribute code to:
<Inject()>
Private _mailService As IEmailService
(the NinjectFactoryController line has been commented out).
This is returning the _mailService as Nothing so in effect the binding is not working.
Although, no memory improvement has been observed in ANTS memory profiler using this new Ninject.MVC3 version.
The ANTS performance profiler is working without crashing but seems to still be looking for other files in the wrong location (like KernelBase.cs, ExtensionsForIEnumerableOfT.cs, Binding.cs etc all giving errors stating that Source code was not found for this file while looking in c:\Projects\Ninject\ninject\src\Ninject... folder).
We still can't get to the memory leak/overload's point of origin, but this much is evident that the application pool is accumulating 70 to 100 MBs of data every run of a part of the business logic and then this memory is not being released until 20 minutes of total site inactivity (ISS 7 Idle-Timeout based), which is of no help at all since the site crashes well before that stage is ever reached.
Any advice would be appreciated.
Kind regards,
- Abrar
Edit:
Managed to finally get Ninject 3.0 working with some guidance.
Also, the issue about Ninject 3 usage in the ActionFilterAttribute was solved using a property declaration like so:
<Inject()>
Public Property _mailService() As IEmailService
Private Get
Return m_mailService
End Get
Set(value As IEmailService)
m_mailService = value
End Set
End Property
Private m_mailService As IEmailService
Now that Ninject 3 is in there doing its thing, the memory issue has slightly improved (i.e, memory is being released a bit more than before), but now, a new issue has arisen. The CPU usage is very high. This problem was there even before Ninject was upgraded from 2.2 to 3.0, but the upgrade doesn't seem to have had any effect on it. The CPU is still maxing out with only 2-3 users and this makes the site non-responsive and it finally crashes within about 10 minutes of usage.
Any thoughts would be appreciated.
Kind regards,
- AbrarHF