Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Case 1:
C#
class B
{
int m;
int n;
}

class A
{
int x;
int y=0;

public void Add()
{
B objb=new B();
}

}

main()
{
A obj=new A();
obj.add();
}



Case 2:
C#
class B
{
int m;
int n;
}

class A
{
int x;
int y=0;
B objb;         //This will allocate memory ??
public void Add()
{
objb=new B();     //may it will allocate new memory??
}

}

main()
{
A obj=new A();
obj.add();
}


What is the difference between two cases ?? which is efficient in the case of memory allocation and utilization(first one create the object inside the method so that,after the method calling it will set as free... but in second one i think Only after the object de-allocation of class A, it will free the memory, so I think first method is good....)??Any suggestions??
Posted
Updated 2-May-12 3:47am
v6
Comments
Sergey Alexandrovich Kryukov 1-May-12 12:03pm    
You can compare efficiency only for the codes which do something. Think on what your code do...
Anyway, please see my answer.
--SA

I hope these code samples are just incomplete, but because of that the question makes no sense.

In first example, the value of objb becomes unreachable immediately, and hence the instance becomes a subject of Garbage Collection. It is ineffective only in one sense: you make redundant operation.

The second example makes no sense, because on second and next calls you simply replace the instance of an object with another instance, and it makes the previous instance unreachable and then — everything goes like in the previous case.

If you write the code which makes sense, you will eliminate the difference between the cases. You would have a collection of B instances and add one more instance each time you call Add. In this case, you cannot offer two different implementations, so your question disappears. This way, your question lacks subject.

But your idea to analyze performance in different ways of implementation is good. My advice is this: in each case: 1) disassemble your code using ILDASM.EXE, 2) time execution using System.Diagnostics.Stopwatch. Please see:
http://msdn.microsoft.com/en-us/library/f7dy01k1%28v=vs.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].

—SA
 
Share this answer
 
Comments
VJ Reddy 1-May-12 12:04pm    
Important points. 5!
Sergey Alexandrovich Kryukov 1-May-12 12:09pm    
Thank you, VJ.
--SA
Shahin Khorshidnia 1-May-12 16:18pm    
Perfect answer and good links.
+5
Sergey Alexandrovich Kryukov 1-May-12 17:20pm    
Thank you, Shahin.
--SA
samu4u 2-May-12 3:31am    
B objb; //This will allocate memory ??
objb=new B() //What it will do??
In addition to the Solution 2 and 3 you might feel bit interested reading followings,

and finally the Eric's blog(as a fan of Eric!),

What is the defining characteristic of a local variable?[^]

Hope it gives you bit more theoretical stuff about the .NET memory :)
 
Share this answer
 
Comments
VJ Reddy 2-May-12 22:54pm    
Good references 5!
Mohammad A Rahman 2-May-12 22:57pm    
Thank you very much VJ :)
If we go by the number of IL instructions generated, then the First one is more efficient as can be seen from the IL generated as shown below:
VB
'Inside add function.

A.Add:
IL_0000:  nop
IL_0001:  newobj
IL_0006:  stloc.0
IL_0007:  ret

A..ctor:
IL_0000:  ldarg.0
IL_0001:  ldc.i4.0
IL_0002:  stfld
IL_0007:  ldarg.0
IL_0008:  call
IL_000D:  nop
IL_000E:  ret


'outside Add function

A.Add:
IL_0000:  nop
IL_0001:  ldarg.0
IL_0002:  newobj
IL_0007:  stfld
IL_000C:  ret

A..ctor:
IL_0000:  ldarg.0
IL_0001:  ldc.i4.0
IL_0002:  stfld
IL_0007:  ldarg.0
IL_0008:  call
IL_000D:  nop
IL_000E:  ret


In the first case objb is a local variable and it will be garbage collected after the execution of Add method. In the second case the objb is the Field of the Class A and it will be in memory as long as the object of A is in memory.

For Garbage Collection generations 0,1,2 are kept and local variables are kept in 0 generation. The GC visits objects kept in generation 0 very fast. So in case 1 objb being a local variable it will garbage collected fast when compared to case 2.

However, in case 1 the objb will not be available after the method Add is executed.
 
Share this answer
 
Comments
Dylan Morley 1-May-12 11:59am    
Good answer - my 5
VJ Reddy 1-May-12 12:01pm    
Thank you, Dylan.
Sergey Alexandrovich Kryukov 1-May-12 12:12pm    
I voted 4, because, as I tried to explain, the code defeats the notion of efficiency. Efficiency is something like the work done divided by time. If the work done is zero or negative, like in the OP code samples...
--SA
VJ Reddy 1-May-12 12:15pm    
Thank you, SA.
Shahin Khorshidnia 1-May-12 16:19pm    
+5
I don't think there is any difference in two cases as far as memory allocation is concerned as memory is not allocated until the object is instanciated. Others Please correct me if I am wrong.

One difference between two cases is in case 1 - objb is declared inside function so it will be accessible only in that function where as in case 2 - objb is declared globally so it will be accessible outside function also.
 
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