1

I often come against the dilemma of making either a singleton or a static class for storing my app's runtime data.

Let's say

public static class DataManager
{
    public static int a = 0;
    public static int b = 0;
    public static int c = 0;
}

Which allows me to call just DataManager.a

and

    public class DataManager
    {

      private static DataManager _sharedInstance = new DataManager();

        static internal DataManager SharedInstance()
        {
            return _sharedInstance;
        }

        public int a = 0;
        public int b = 0;
        public int c = 0;
}

In this call I'll have to call DataManager.SharedInstance().a

Both ways will work. But I'd like to know which is better for my app's memory and performance. Assuming that my data is big and includes many other collections.

Thanks in advance.

Ahmed Elashker
  • 1,001
  • 1
  • 8
  • 17
  • 3
    Try both and compare performance. I expect static to be faster, but you never can be sure. –  Oct 12 '17 at 06:42
  • Do you ever change these values stored inside static or singleton? – Amit Kumar Singh Oct 12 '17 at 06:43
  • 1
    One thing you are missing about singleton class is, you must have a private constructor otherwise another instance can be created anywhere in the code! About the performance I am not sure if there will be any difference but usability wise, you can inherit a singleton class but not a static class. – praty Oct 12 '17 at 06:45
  • 1
    I'd always go for the second option (using `Lazy`) to be able to switch out the data during testing, _unless_ it's measured (under production conditions) to be forbiddingly slow – Haukinger Oct 12 '17 at 06:46
  • 2
    [Which is faster?](https://ericlippert.com/2012/12/17/performance-rant/). Applies to memory footprint too. Your question is too broad, and doesn't even contain enough context for anyone to answer it properly. Either could be faster/use less memory, depending on the situation. The only right answer is for you to test both and see which meets your _specific_ performance goals. Don't have any specific performance goals? Then your concern is entirely premature and not even worth thinking about. – Peter Duniho Oct 12 '17 at 06:47
  • https://stackoverflow.com/questions/5839974/are-static-classes-thread-safe – Amit Kumar Singh Oct 12 '17 at 06:47
  • @Amit Yes of course values will be changing depending on a web service response – Ahmed Elashker Oct 12 '17 at 06:51
  • @PeterDuniho My concern for my app is to both consume less memory and operate faster – Ahmed Elashker Oct 12 '17 at 06:51
  • If you look at some frameworks like `MVVMCross` they use the static class `Mvx` to store singletons for IoC. – jegtugado Oct 12 '17 at 06:53
  • _"My concern for my app is to both consume less memory and operate faster"_ -- most everyone has that concern for their programs. So what? We can't tell you unequivocally which would be faster or consume less memory. Only you can, and you can only do that by _testing it yourself_. That said, frankly I doubt you would find any significant difference on either metric. – Peter Duniho Oct 12 '17 at 06:53

1 Answers1

2

Clearly the first approach is simpler and shorter - both for reading and definitely in performance and memory aspects.

In the second code, the two extra constructs will cause a little overhead.

shahsani
  • 687
  • 7
  • 16