Home>

Developed in Unity.

I started game development from WolfRPGEditor, so I'd like to make a program that flows from top to bottom as much as possible.
I'm thinking of preparing a coroutine for every class and calling it in the scene manager that exists in the scene, but

Is it possible to create a singleton instance and call another class method to be called?

public class Test
{
    public static IEnumerator Main ()
    {
        // Process here
        yield break;
    }
}


I am worried about what I can call and use like yield return Test.Main ();

The reason I'm worried is that I read that static variables eat up memory when running a program.
However, there was nothing written about the detailed behavior of static methods, so
I also want to know how a static method allocates a memory area during program execution.

Does every local variable in the method eat memory?
Or does it seem like a method reference is passed to memory and only one memory area is needed?

Does it become heavy if there are too many static items (variables, methods, classes, etc.)?
I eat but eat about 1000 static methods.

The first question may be incomplete, but thank you.

  • Answer # 1

    Hello.

      

    Static variables read that the memory area is eaten during program execution.

    A normal variable and a static variable are allocated as a variable area and a variable management area.
    Rather, ordinary variables should consume slightly more memory.

      

    Since no detailed behavior was written for static methods,
      I also want to know how a static method allocates a memory area during program execution.

    There is almost no difference in the memory area allocation method for non-static methods and static methods.
    These differences are in principle only whether or not the pointer to this object = this is received as a (hidden) argument.

      

    Does every local variable in the method eat memory?
      Or does it seem like a method reference is passed to memory and only one memory area is needed?

    Method = program. Whether a method is static or non-static, only one set of areas is recorded. You may feel that ordinary (= non-static) methods are copied to the code (program) of the method for the number of objects, but that is not the case. There is only one set of method code.

    A method's local variables are reserved only during execution of the method.
    When the method is not executed, the local variable does not consume memory. (Not secured in memory.)
    If the same method is executed multiple times at the same time, such as by recursive calls or calls from multiple threads, local variables are allocated as many times as the number of multiple executions.
    This is the same for both normal and static methods.

      

    Does it become heavy if there are too many static items (variables, methods, classes, etc.)?
      I eat but eat about 1000 static methods.

    The reverse. In terms of "weight", non-static variables, methods and classes are "heavy" than those that are static. Those that are static are "static". That means it will not change during program execution. This means that resources (memory and CPU) are not consumed for the change.
    However, the difference is within the error range so you don't have to worry about it. Projects that care about this difference should use C ++ instead of C #, and maybe a project that should use C or assembler.

  • Answer # 2

      

    Which should I use a static method or a singleton?

    If you're only worried about the amount of memory used, it's the same or error range.
    I think that the number of singleton objects + instance methods is slightly more than the static methods.

  • Answer # 3

      

    Static variables eat memory when running programs

    That's wrong in the first place