使用继承与使用同一对象的属性之间是否存在内存差异 [英] Is there a memory difference between using inheritance vs using properties of that same object

查看:86
本文介绍了使用继承与使用同一对象的属性之间是否存在内存差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定处理此设置的最佳方法是什么,我不确定是使用继承还是使用属性来最有效地利用内存

如果我使用继承,我可能会有以下内容:
公共类ObjectA
{
}

公共类ObjectB:ObjectA
{{}}
公共类ObjectC:ObjectB
{
}

如果我使用Properties,它将更符合以下几行:
公共类ObjectD
{



公共类ObjectE
{{private ObjectD instanceObjD = null
public ObjectD InstaceObjD
{
get
{
if(instanceObjD == null)
{
instanceObjD = new ObjectD()
}
返回instaceObjD
}
}
}

从内存消费的角度来看,以及一个服装年龄集合视角,使用继承1或属性2是否更有效?我知道使用延迟实例化所有使用属性的对象都不是预先创建的,但为了参数的缘故,让我们说所有属性物体最终被创造出来。

1。 private ObjectC oC = new ObjectC()




2。 private ObjectE oE = new ObjectE()
private ObjectD oD = oE.InstanceObjD

我想要了解的是:
1。在这两种情况下,每个对象是作为单个对象还是一个大对象处理?内存管理的最佳方法是什么?
3。每个人如何收集垃圾是否有任何利弊?你为什么要和另一个一起去?

提前感谢

I'm trying to determine what is the best way to approach this setup, and I'm not sure whether to use inheritance or use properties to most efficiently utilize memory

If I use inheritance, I could potentially have the following:
public class ObjectA
{
}

Public class ObjectB : ObjectA
{
}

Public class ObjectC : ObjectB
{
}

If I use Properties it would be more along the lines of:
Public class ObjectD
{
}

Public class ObjectE
{
    private ObjectD instanceObjD = null
    public ObjectD InstaceObjD
    {
        get
        {
            if (instanceObjD == null)
            {
                  instanceObjD = new ObjectD()
            }
            return instaceObjD
        }
     }
}

So from a memory consumption perspective as well as a garbage collection perspective, is it more efficient to go with Inheritance 1 or Properties 2?
I know that with lazy instantiation all the objects using properties aren't created up front, but for the sake of argument here let's say all property objects wind up being created. 

1. private ObjectC oC = new ObjectC()

or

2. private ObjectE oE = new ObjectE()
    private ObjectD oD = oE.InstanceObjD

What I'm trying to understand is:
1. In both cases, does each object get treated as individual objects or one large object?
2. What is the best approach for memory management? 
3. Are there any pros/cons with how each gets garbage collected?
4. Why would you go with one over the other? 

Thanks in advance

推荐答案

嗨tyler

内存优化主要取决于你如何编写特定的逻辑设计。这项技能主要取决于软件开发人员优化设计逻辑以获得更好的内存性能并减少不必要的冗余软件逻辑(例如:意大利面条代码)。

此致,
Phijo Mathew Philip。
Hi tyler

Memory optimization mainly depends how you code the logic for a particular design .  It is a skill that mainly depends on the software developer who optimizes the design logic for better memory performance and reducing the unnecessary redundant software logic(for example :Spaghetti code ).

Regards,
Phijo Mathew Philip.


这篇关于使用继承与使用同一对象的属性之间是否存在内存差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆