0

will situation 1 and/or 2 cause it to sit around waiting for GC. I find that the context being used multiple times is causing our code to have methods that have nothing but a using block and I'd like to avoid the using block in those cases if the context will get disposed quickly enough. An example would be a method which does some saving and opens up the context -> saves data to multiple tables -> then returns 200 OK.

Situation 1

public function test()
{
    return new myContext().Events.FirstOrDefault();
}

Situation 2

public function test2()
{
    var ctx = new myContext();

    return ctx.Events.FirstOrDefault();
}

Situation 3

public function test3()
{
    Event e;
    using(var ctx = new myContext()) {
        e = ctx.Events.FirstOrDefault();
    }

    return e;
}
matthewdaniel
  • 1,842
  • 4
  • 21
  • 35

2 Answers2

0

any instance is sitting around and waiting for GC. in case of using - we mark it is ready to be collected (solution 3). Solution 1.2 will do the same after exiting the scope.

skalinkin
  • 1,024
  • 9
  • 19
  • I don't think this is correct. Using will actually dispose of the object I believe. – matthewdaniel Apr 04 '16 at 18:39
  • this is correct. using will call Dispose method in IDisposable instance. But it does not guarantee that GC will clean it. It will simple mark it ready to be garbage collected. but if you still have reference somewhere to it it will not be garbage collected – skalinkin Apr 04 '16 at 18:47
  • how about 1,2 will do the same after exiting the scope. Do you have any helpful links? So then is there very little difference in the 3 approaches practically? – matthewdaniel Apr 04 '16 at 18:57
  • correct there are little. In solution 1 Dispose will be called by CLR after exiting from test function on all local variables with does not have references anymore. in solution 3 we do it just a little earlier by "using". – skalinkin Apr 04 '16 at 19:06
  • 2
    "Dispose is NOT automatically called and must be explicity called if resources are to be released" http://stackoverflow.com/a/45049/3507404 – Ingweland Apr 04 '16 at 21:02
  • according to the SDE on EF it sounds like it is safe to not call dispose since the connections are only used as necessary. thumbs up for the comment. http://blog.jongallant.com/2012/10/do-i-have-to-call-dispose-on-dbcontext.html – matthewdaniel Apr 05 '16 at 13:56
-1

Solution 1 and 2 will not dispose the context. Disposing the context means that unmanaged resources will be cleaned up. Setting your context to null or leaving the scope will not trigger such a clean up. You should either use using construct or add some code where you explicitly call context.Dispose (either in try...finally or any other preferred way (for example, you may decide to make your custom class to implement IDisposable, and dispose the context there)

Entity Framework and context dispose

Uses of "using" in C#

Will the Garbage Collector call IDisposable.Dispose for me?

Community
  • 1
  • 1
Ingweland
  • 1,073
  • 1
  • 13
  • 25
  • 1
    Interesting article here http://blog.jongallant.com/2012/10/do-i-have-to-call-dispose-on-dbcontext.html#.U6WdzrGEeTw seems to show that disposing is not really even necessary. Thoughts? – matthewdaniel Apr 04 '16 at 18:19