1

I have an ASP.NET application that has multiple helper classes. I'm a little worried about Memory leaks. Each time i want to use a helper class member function i call them like this

new SampleHandler().DoFunction();

Since it dosen't have any strong reference to the object created can I guarantee whether GC will clear the memory for the object created ?
Since there is a high chance for me that I won't be using the object again in the page I started coding like this.

Note: There are numerous calls to various member functions belonging to different helper classes in the code behind file performed in the same way.

Adrian De Barro
  • 495
  • 2
  • 6
  • 21
  • 3
    Make your helper methods static – Janne Matikainen Oct 17 '13 at 07:04
  • what is wrong with static functions? – Sumit Gupta Oct 17 '13 at 07:04
  • 1
    Static functions are not thread safe. Since in ASP.NET static functions span scope over the whole process, all users get to use the same (static) function. – Dinesh Krishnan Oct 17 '13 at 07:05
  • 6
    Static methods are thread safe if they are stateless. There is nothing inherently 'not thread safe'. http://stackoverflow.com/questions/1090650/are-static-methods-thread-safe – Ed Chapel Oct 17 '13 at 07:07
  • They can be made thread safe you are exactly right. But imagine the scenario where a user takes 10 minutes to execute a function. Other users have to wait for him to complete which is not desired. – Dinesh Krishnan Oct 17 '13 at 07:10
  • Not declaring a variable will not make GC clean the object any faster. I don't think you have a problem. – usr Oct 17 '13 at 07:10
  • @DineshKrishnan It's not up to imagination, it's down to judgement. Implementing your helper static or not does not protect you from a bad implementation that leaks memory or creates memory pressure. There is not simple pattern to prevent bad coding. – Ed Chapel Oct 17 '13 at 07:13
  • 2
    @DineshKrishnan Static methods are re-entrant unless you've done something foolish by locking or otherwise preventing re-entrancy. http://stackoverflow.com/questions/3129354/threading-and-static-methods-in-c-sharp – Ed Chapel Oct 17 '13 at 07:14
  • Problem would be if your SampleHandler would be a singleton, then thread safety would be an issue and needs to be handled properly. Static or instantiated makes no difference in the db access execution. – Janne Matikainen Oct 17 '13 at 07:18
  • @EdChapel So variables inside a static method remain local to the invoked thread ? – Dinesh Krishnan Oct 17 '13 at 07:20
  • @Dinesh the first line of the accepted answer if you click that link: _"Yes, each thread gets its own separate local variable"_. – CodeCaster Oct 17 '13 at 07:22
  • @DineshKrishnan - no variables inside a static method stay local to their own scope. i.e the method. The same thread calling the same method will get different scoped variables in the method called. – Phill Oct 17 '13 at 07:22

2 Answers2

8

Yes, since there are no other outstanding references, the instance created by new SampleHandler() will be eligible for collection as soon as DoFunction() returns.

There is, however, no guarantee about the time when the GC will collect that instance, as usual.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Type too fast for me :( – Phill Oct 17 '13 at 07:05
  • you mean I can guarantee GC will collect them as soon as the page life expires ? – Dinesh Krishnan Oct 17 '13 at 07:06
  • 1
    @DineshKrishnan, no, you cannot. The GC may keep that data in memory for the whole lifespan of the application pool if it so chooses. It is designed to be smart enough to make the right decision, though, and it is usually best to let it decide. – Frédéric Hamidi Oct 17 '13 at 07:07
  • Will this be a bad coding practice and does it pose any memory leak issues ? – Dinesh Krishnan Oct 17 '13 at 07:08
  • 1
    @DineshKrishnan It *will* be garbage collected, but the GC decides *when*. You can not make a statement on the *when*. – Thorsten Dittmar Oct 17 '13 at 07:09
  • 3
    It is bad coding practices to not make helper methods static - but that's only to my coding standards :-) – Thorsten Dittmar Oct 17 '13 at 07:10
  • @DineshKrishnan, nothing will be leaked, the instance may only remain in memory longer than if it was explicitly freed. For what it's worth, I use the same pattern in some of my code (COM Interop classes, so I cannot use static methods) and I never experienced any problem doing that. – Frédéric Hamidi Oct 17 '13 at 07:11
  • 1
    If you want you could make your SampleHandler implement IDisposable and then use it with using (var sh = new SampleHandler()){ sh.DoFunction(); } – Janne Matikainen Oct 17 '13 at 07:12
4

The garbage collector will take care of unused references. So you don't need to worry about a memory leak. But if you create "garbage"-objects very fast you could have temporary memory pressure.

But if you don't need the instance anyway or the instances are exchangeable you should consider to make the method static.

public class SampleHandler
{
    public static void DoFunction()
    {
        // ...
    }
}

Then you would call it:

SampleHandler.DoFunction();

There is no problem with static methods in ASP.NET even if it's a multithreaded environment. But you should be careful with static fields.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • No the instance is not shareable since those functions usually perform db related tasks. – Dinesh Krishnan Oct 17 '13 at 07:09
  • 1
    @DineshKrishnan just because it performs database tasks does not mean it can't be static. You can't share references out of the scope of the static method in case another call modifies the reference. – Phill Oct 17 '13 at 07:10
  • Since if I make it static won't the helper method stay in memory ? Will it affect the performance when there are say 100 static methods ? Just curious to know. – Dinesh Krishnan Oct 17 '13 at 07:17
  • Why are you so concerned about memory and memory leaks? Do you know anything about how the .NET runtime handles memory? Do you have any problems in your current application? – CodeCaster Oct 17 '13 at 07:19
  • 1
    @DineshKrishnan: A method doesn't "stay" in memory at all, fields do. Use a static method when the method does not belong to a specific object. – Tim Schmelter Oct 17 '13 at 07:20
  • @CodeCaster So far I haven't met with any issues. Just curious. – Dinesh Krishnan Oct 17 '13 at 07:22
  • @CodeCaster just learned static fields only stay in memory not methods. Thanks . – Dinesh Krishnan Oct 17 '13 at 07:26
  • @DineshKrishnan Memory leaks don't technically exist in managed code. Go read [MSDN for the basics](http://msdn.microsoft.com/en-us/library/f144e03t.aspx), read the "CLR Inside Out" blogs or pick up the book "CLR via C#" to learn about memory management and garbage collection. – CodeCaster Oct 17 '13 at 07:26
  • @CodeCaster May be I should have posted the question as "worried about the object staying for longer in the memory" instead of "memory leak". My Bad. Anyway as Thorsten Dittmar said in above comments " You can not make a statement on the when" the memory will be freed by GC, that answers a part of my question. – Dinesh Krishnan Oct 17 '13 at 07:35