Does Dependency Injection cause extra memory consumption?

Current application was developed by other developer by Dependency Injection using Unity. The application was developed in MVC4 and Entity Framework. This application is not big, not even medium sized application.

Now, team wants to remove dependency injection as it is consuming lot of memory.

My questions are

1) Is dependency injection really consume lot of memory ?

2) Advantages of having Dependency Injection ?

3) what is the purpose of having Unity, Dependency Injection ?

4) How to remove this Dependency Injection from application ?

Thanks


1) Is dependency injection really consume lot of memory ?

Dependency Injection is a pattern. The pattern by itself does not use any memory. However, you can't create loosely coupled code when implementing static classes, so during runtime class instances should be created. The amount of memory this consumes however is negligible compared to all other parts of the system, especially in a web application where ASP.NET itself creates a lot of garbage per request. And in the very rare case that you are under very tight memory constraints (which will hardly never be an ASP.NET web application), you can build up object graphs that consist of only singletons. This means objects are created once and cached, and this can reduce the amount of garbage your application creates to zero. So the use of Dependency Injection itself doesn't have to consume more memory and the difference is negligible.

That doesn't mean that a tool such as Unity consumes some memory. I've hardly ever seen this been a problem, although its always possible that misuse of the tool causes memory leaks. I think you should profile the application before deciding to remove some tool that might not be the problem at all.

However, one common pitfall is that Entity Framework's DbContext is kept alive too long (longer than a single request). If you do that, you will typically hit lots of problems, such as concurrency bugs and memory leaks. This however is not strictly related to Dependency Injection, it's rather easy to accidentally cache a DbContext for too long.

2) Advantages of having Dependency Injection ?

This is a whole topic of its own. Dependency Injection is about applying the SOLID principles, especially the Dependency Inversion Principle. The Dependency Inversion Principle is about creating loosely coupled code. When done right, this increases the maintainability of a system. Complete books are writen about this subject.

3) what is the purpose of having Unity, Dependency Injection ?

Unity is just one of the many tools to use when using Dependency Injection, but for a really small application, you could even consider applying DI without any tool. For small applications there is a clear advantage in not using such tool.

But in general, where DI helps in making your application maintainable, a DI library (such as Unity) will help in making your Composition Root (the place where you wire everything together) more maintainable.

4) How to remove this Dependency Injection from application ?

Instead of reverting back to your old practices, I think you and your team should take a step back and see this as an opportunity to start learning something new and improve your skills. There is a reason that people apply the Dependency Inversion Principle, the SOLID principles and apply Dependency Injection. It's because these practices improve the overall structure of an application and makes a system as a whole more maintainable.

It does take some time however to understand and master the concept of Dependency Injection, but IMO its time worth spent. You will improve your skills as a developer, which makes you more valuable for your company and any other company if you decide to switch jobs.

The most influential work on the subject of Dependency Injection, is Mark Seemann's book Dependency Injection in .NET. That book has had a major impact on how I and many others write code. A second edition of that book is coming out in 2018. I'm co-authoring this new edition. My advice would be to read either one of these editions, and preferably the second, since it is a major upgrade.


I think you need to understand what dependency injection is first, which may well give you the answers to all 4 questions.

A good answer can be found here from the question Why does one use dependency injection?


In short, removing Unity is unlikely to resolve your memory issue. Also, Unity (and its ilk) are Inversion Of Control (IoC) containers, not dependency injection. Do a quick Google just so you can get your terminology straight.

To answer each question in turn:

1) Is dependency injection really consume lot of memory ?

IoC containers do not consume lots of memory (if used properly) and are used by many VERY large systems without issue. The memory consumption is probably an issue elsewhere in your system. Why do you suspect your IoC container is the culprit? Have you profiled?

2) Advantages of having Dependency Injection ?

Mutliple - One is that it makes you think about the dependencies in your system rather than just newing everything up... It also allows you to make it more testable and dynamic, ie you can swap "components" more easily.

3) what is the purpose of having Unity, Dependency Injection ?

The idea is that you program against interfaces rather than concrete implementations. This enables TDD (using mocks etc) and other best practices. It also means that you can swap around implementations more easily without breaking everything.

4) How to remove this Dependency Injection from application ?

Don't... You won't solve your memory issues but will introduce lots of other issues... If anything you are more likely to compound your current issue.

THAT said... If you have misconfigured Unity and set some strage lifetimes etc then it is possible that you are leaking memory that way. However, throwing the baby out with the bathwater is not the way forward.

链接地址: http://www.djcxy.com/p/82250.html

上一篇: ASP.NET MVC全局数据库

下一篇: 依赖注入是否会导致额外的内存消耗?