Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi friends,

There is one static class, it has one method called add() and one normal class. It has also one method called sum(). I will create 100 objects in the static class of the normal class...?
How much memory will occupy 1 or 100?

Thanks You
Posted
Updated 11-Oct-15 7:17am
v2
Comments
Zoltán Zörgő 11-Oct-15 12:49pm    
Not really clear. Simply write your test code and try it.
Anyway: static fields are stored once, instance fields are stored per instance.
Andreas Gieriet 11-Oct-15 15:12pm    
Show us the code.
Regards
Andi
PS: Once you programmed it, try it out as Zoltan suggests...
Agasimani 12-Oct-15 2:04am    
This question. How to find the size of class(static)
static class static_class
{

public static void add()
{
Console.WriteLine("Its static class method");
Console.ReadLine();
test obj = new test();
obj.add();
test obj1 = new test();
obj.add();
}


}

class test
{

public void add()
{
Console.WriteLine("Its normal class method"+x);
Console.ReadLine();
}

}


class Program
{
static void Main(string[] args)
{

static_class.add();

}
}

If the (the method) of the static class creates 100 object of the 'standard' class then, obviously, the memory used will be 100 times the one required for (data members of) one instance.
 
Share this answer
 
Comments
BillWoodruff 12-Oct-15 3:23am    
"the memory used will be 100 times" look again at the code :)
CPallini 12-Oct-15 3:38am    
"look again at the code"
There was no code when I proposed my solution. :-)
BillWoodruff 12-Oct-15 7:23am    
A good reason for you to look at it again. :)
Have you actually tried your code ? Set a breakpoint(s), and single-step through the code (using F11 in Visual Studio): with each step look at the values of the variables.

What your code says to me is that you are either very confused about C#, or, if you are in a class, and this is part of homework, then your teacher (and their curriculum) is very confused.

In your code each instance of the 'test Class is created in the static class' 'Add method: it exists only in the scope of that method, and it will be disposed as soon as .NET's garbage collector comes around.

So, it is never the case, here, that you will allocate memory for more than the two instances of the 'test Class with each call to the static class' 'Add method.

Issues for you to explore and educate yourself about:

1. Scope: the persistence of objects; access modifiers

2. .NET's memory allocation and garbage disposal
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900