1

From the static, I understand that whenever a static variable is declared - It's memory get allocated in RAM. Suppose, we have integer static int i = 5; then a memory of 4 byte will be occupied somewhere in computer. And the same will happen if I have a static class or any reference type.

But my question is - if I declare a generic list like List<string> in c# and that is static. So what or how much memory will be allocated for this list in computer. And I assume that If I add items in this list - then it will require some more memory.

  • So, it breaks my concept about static - that a static field has a fixed memory allocation at the time of declaration and that can not be changed through the application lifetime.

Can someone genius in c# help me out here?

Ankush Jain
  • 5,654
  • 4
  • 32
  • 57
  • static is actually same memory area (address) not necessary it's amount I guess. – madoxdev Oct 11 '17 at 12:55
  • who said static field has fixed memory allocation? – M.kazem Akhgary Oct 11 '17 at 12:55
  • 2
    `static` or non `static` has little to do with how the memory management will behave. A static is just a very longed lived object and will be treated as such. `static` is a misnomer and therefore misleading, there is not really anything very static about it; `static` really means ["associated with a type instead of any one instance of the type"](https://stackoverflow.com/a/9410742/767890). – InBetween Oct 11 '17 at 12:55
  • About memory, maybe this link can help you: https://stackoverflow.com/a/207605/316799 – Felipe Oriani Oct 11 '17 at 12:56
  • 1
    List is a reference type, so you only allocate 4 or 8 bytes to store the reference to the object. Formally in the loader heap, where all statics are allocated, it is associated with the AppDomain. The content of the list is in the GC heap as normal. It will never be garbage collected if you don't use Clear() and/or TrimAccess(). – Hans Passant Oct 11 '17 at 12:59

3 Answers3

1

There's no difference in the allocation of static member compared to non-static ones. "Static" just means that the member is visible and accessible to all instances of the class declaring it.

For the List<>: all objects you instantiate with a "new" keywork are created in a part of the memory called Heap. So are the static list you are asking about. Lists in .NET are created as arrays of a certain length plus a pointer to an eventual new array. Then, whenever that first array gets filled by adding items to the list, a new array is created and linked to the first using the pointer. In this way the list can grow.

Stefano Liboni
  • 149
  • 2
  • 11
0

You're making a few assumptions about how .NET does memory management.

Under the hood (and I'd recommend looking) List uses Array to allocate blocks of data and is instantiated to a size of 4 unless specified, so you'll have a pointer for the array and it's size multiplied by the size of int. The amount of memory used initially depends on what size the array is when you instantiate the List.

E.g. if you have List<int> then you have a memory pointer for the List instance, a memory pointer for Array and at whatever size you set in the constructor multiplied by the amount of memory required for the data type of T. All of this gets put in the Gen0 cache initially and more or less memory is allocated, deallocated, moved to the Gen1 and Gen2 blocks as you populate, depopulate, use the List.

Given all of the above, there is no definitive answer unless the question is refined, e.g. "How much memory is allocated when I instantiate List<int>(5)?"

As for static, that's pretty much moot as the same amount of memory has to be allocated for the instance.

DiskJunky
  • 4,750
  • 3
  • 37
  • 66
0

From a different angle, maybe the way to help would be to explain just what 'static' is in .net.

Here's a simple class:

public class MyClass
{
    public string Zeus;
    public static string Hades;
}

Okay, so what does that 'static' mean for our Hades string? Static basically means: it only exists in one place - it doesn't matter how many instances of the class you make, there's only going to be one Hades string.

MyClass first = new MyClass();
MyClass second = new MyClass();
MyClass third = new MyClass();

... there are now three Zeus strings. One for each of those MyClasses:

first.Zeus = "first";
second.Zeus = "second";
third.Zeus = "third";

... but there's only one Hades:

MyClass.Hades = "only version";

Notice how I didn't put 'first.Hades', or 'second.Hades'? That's because, since there's only one version, I don't have to put an instance to get to it. In fact, VisualStudio will flat out tell you, "I can't do this - you're trying to get to a static variable, but you're trying to get to it through an actual instance of your class."

Instead, you just use: MyClass.Hades.

So, getting back to your memory question?

public class MyClass
{
    public List<string> Zeus;
    public static List<string> Hades;
}

The way that those List are stored really isn't any different. The only difference is, you'll always have one List for your static Hades variable... and you'll have as a Zeus List for every MyClass you create (that hasn't been GarbageCollected)

Make sense? It's kinda important to get this concept down, because it'll come into play a lot for stuff like caching or having a Singleton global object.

Kevin
  • 2,133
  • 1
  • 9
  • 21