3

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. Does dependency injection really consume lot of memory?

  2. What are the advantages of applying Dependency Injection?

  3. What is the purpose of using Unity?

  4. How do I remove Dependency Injection from application?

Steven
  • 166,672
  • 24
  • 332
  • 435
user3757426
  • 127
  • 1
  • 5
  • 17
  • 2
    1.No.It's Just another way of initializing Objects 2.It creates a loosed coupled System(higher Abstraction).3.Its just a framework,You can code yourself if you need it 4.Why do you want to remove it ?when you have a loosely coupled system?Are you are sure memory consumption is because of DI ? – Rangesh Jul 16 '14 at 12:58

4 Answers4

8
  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 temporary objects 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 DI Container, such as Unity, consumes no memory though. But I can't recall this ever being a problem, although its always possible that misuse of the tool (or a bug) causes memory leaks. I think you should profile the application before deciding to remove some tool that might not be the problem at all.

One common pitfall, though, is that Entity Framework's DbContext is kept alive too long (longer than a single request). If you do that, you will typically hit a myriad 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—even without using DI.

  1. 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 written about this subject.

  1. what is the purpose of having Unity, Dependency Injection ?

Unity is just one of the many DI Containers you can use when practicing Dependency Injection. Note that DI Containers are not mandatory tools. You could even consider applying DI without any tool. For small applications there is even a clear advantage in not using such tool.

But in general, where DI—as a pattern—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.

  1. 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 came out in 2019. I co-authored this new edition. My advice would be to read either one of these editions, and preferably the second, since it is a major upgrade.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • Re: the third paragraph of your answer, how does a long-lived `DbContext` cause concurrency bugs? A `DbContext` represents a unit-of-work and it does not mutate any static state beyond any lazy initialization. – Dai Jul 27 '19 at 10:40
  • @Dai: Note the text "(longer than a single request)" in the third paragraph. Memory leaks can appear when you start reusing DbContext for a long period of time, as a DbContext doesn't automatically flush its cache. Concurrency issues start to appear when you access the same DbContext instance from different threads/requests in *parallel*, which is something that often done implicitly and by accident. – Steven Jul 27 '19 at 11:12
0

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?

Community
  • 1
  • 1
Joe
  • 1,214
  • 3
  • 15
  • 33
0

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, i.e. 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.

Belogix
  • 8,129
  • 1
  • 27
  • 32
0

1) Is dependency injection really consume lot of memory ?

No, Using DI with Unity you can control your object lifetime you can decide when your objects is disposed and you can adopt singleton design pattern also.

2) Advantages of having Dependency Injection ?

Using DI help your application more : increase Maintainability, testability, flexibility, extensibility, late binding, parallel programming. It also relates to the SOLID principle.

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

To remove the tight coupled dependencies in your application by using loosely couple design.

4) How to remove this Dependency Injection from application ?

Remove Unity reference. Delete all your register methods.

For more information you should check this link Patterns and practices