Coffeehouse Thread

8 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

how memory management works for class that has only static methods with no member variables

Back to Forum: Coffeehouse
  • User profile image
    Surendiran Bala

    I have a busincess layer that has only static methods (i made its static since there is no member variables)

    For example

    public class SampleBusinessLayer

    {

    public static string F1()

    {

    }

    public static string F2()

    {

    }

    }

     

    I'm calling the the methods in my presentation layer or in any other layer..but my question is how memory management works the above class.

  • User profile image
    evildictait​or

    It works the same as it always did. Those are static functions, not static variables. When you forget about the result it'll eventually be cleaned up by the garbage collector.

  • User profile image
    magicalclick

    I am guessing here, but, it is like evildictaitor said, it is method, so, it is only count as code. There is no dynamic allocated values because there is no values, just codes. Any field declared inside the code is obviously on the call stack and released at return of the method.

    If you are thinking about static field memory usage, my guess is it will not be released until you null the field or clear the collection. I mean, they are is always available, so, I don't think GC should do anything to static fields.

    Leaving WM on 5/2018 if no apps, no dedicated billboards where I drive, no Store name.
    Last modified
  • User profile image
    figuerres

    but with the "static" i think only one copy of each method will be created and shared by all instances of the class ....  and if i recall this can create some side effects that lead to nasty bugs under some conditions.....  unless you have tought this out very fully then do not do that.

    static is a tricky option sometimes to use.

  • User profile image
    Sven Groot

    @figuerres: That's not strictly accurate. Static methods are fine, they have no problems. It's static state that you need to be careful about, because that can lead to threading issues. If these static methods operate only on local variables and their parameters, they should be safe (assuming the parameters passed in aren't references to state shared by threads, but that goes for any function).

  • User profile image
    evildictait​or

    , figuerres wrote

    but with the "static" i think only one copy of each method will be created and shared by all instances of the class ....  and if i recall this can create some side effects that lead to nasty bugs under some conditions.....  unless you have tought this out very fully then do not do that.

    static is a tricky option sometimes to use.

    Only one method would be created anyway. The only time multiple copies of a method can end up in memory is if either:

    a) The method appears in two different assemblies

    b) The method is generic and is used more than once with different generic parameters.

    For example, the code for object.ToString() only appears only once in memory, despite that it is an instance method on every object that you create.

    Sven is right - static variables are the thing to watch out for. Static methods which don't have static variables associated with them are perfectly fine and have no problems.

  • User profile image
    figuerres

    , evildictaitor wrote

    *snip*

    Only one method would be created anyway. The only time multiple copies of a method can end up in memory is if either:

    a) The method appears in two different assemblies

    b) The method is generic and is used more than once with different generic parameters.

    For example, the code for object.ToString() only appears only once in memory, despite that it is an instance method on every object that you create.

    Sven is right - static variables are the thing to watch out for. Static methods which don't have static variables associated with them are perfectly fine and have no problems.

     

    yeah that's right ----  i was recalling some code that had static vars and was called in a multi threaded app .... not fun finding randomly corrpupted data and then finding where and how that happened.

  • User profile image
    Ion Todirel

    @magicalclick:Actually everything is allocated on the heap, except for value types, so the resources are not released until the GC feels like it, even in static methods

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.