类中的内存分配..哪个效率高?与内存分配相比,两者之间有什么区别? [英] Memory allocation inside the class..Which is efficient?And what is the difference between two, as compare to the memory allocation?
问题描述
案例1:
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();
}
情况2:
Case 2:
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();
}
两种情况有什么区别?在内存分配和利用的情况下这是有效的(第一个在方法内部创建对象,以便在调用它的方法之后将其设置为free ...但是在第二个中,我认为仅在对象的类取消分配之后A,它将释放内存,所以我认为第一种方法是好的....)??任何建议??
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??
推荐答案
我希望这些代码示例只是不完整,但是因此这个问题没有道理.
在第一个示例中,objb
的值立即变为不可访问,因此该实例成为垃圾回收"的主题.它仅在一种意义上是无效的:您进行了多余的操作.
第二个示例没有意义,因为在第二个和下一个调用中,您只需将一个对象的实例替换为另一个实例,这会使先前的实例不可达,然后-一切都与前面的情况类似.
如果编写有意义的代码,则将消除这两种情况之间的差异.您将拥有一个B
实例的集合,并在每次调用Add
时再添加一个实例.在这种情况下,您不能提供两种不同的实现,因此您的问题消失了.这样,您的问题就没有主题了.
但是您以不同的实现方式分析性能的想法是好的.我的建议是:在每种情况下:1)使用ILDASM.EXE分解代码,2)使用System.Diagnostics.Stopwatch
执行时间.请参阅:
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
I hope these code samples are just incomplete, but because of that the question makes no sense.
In first example, the value ofobjb
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 ofB
instances and add one more instance each time you callAdd
. 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 usingSystem.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
除了解决方案2和3外,您可能还会对以下内容感兴趣,
In addition to the Solution 2 and 3 you might feel bit interested reading followings,
- Object Overhead: The Hidden .NET Memory Allocation Cost[^]
- The Top 5 .NET Memory Management Misconceptions[^]
如果我们按生成的IL instructions
的数量进行操作,则First
效率会更高从生成的IL中可以看到,如下所示:
If we go by the number ofIL instructions
generated, then theFirst
one is more efficient as can be seen from the IL generated as shown below:
'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
在第一种情况下,objb
是局部变量,并且在执行Add
方法后将被垃圾回收.在第二种情况下,objb
是Class A
的Field
,只要A的对象在内存中,它就会在内存中.
对于Garbage Collection
代,将保留0,1,2,并且将局部变量保留为0代. GC非常快速地访问了第0代中保存的对象.因此,在情况1 objb是局部变量的情况下,与情况2相比将快速进行垃圾回收.
但是,在情况1中,在执行Add方法之后,objb将不可用.
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.
这篇关于类中的内存分配..哪个效率高?与内存分配相比,两者之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!