简体   繁体   English

什么使用更多的内存,一个Class对象或该类的实例?

[英]What uses more memory, a Class object or an instance of that class?

I am modeling dishes that a restaurant serves. 我正在模拟一家餐厅提供的菜肴。 I might make a Dish class something like this: 我可能会在Dish课上做这样的事情:

class Dish  
  attr_accessor :name, :price, :ingredients, etc...
  def initialize(dish_name, dish_price, etc...)
    name= dish_name
    price= dish_price
  end
end

With respect to memory usage, would it be better to create instances of Dish for each dish, Dish.new("Chicken Curry", ...) , or create a new class that subclasses Dish, class ChickenCurry < Dish; ...; end; 关于内存使用情况,最好为每个菜创建Dish实例Dish.new("Chicken Curry", ...) ,或者创建一个新类Dish的子类,即class ChickenCurry < Dish; ...; end; class ChickenCurry < Dish; ...; end; ? Are there any other things to consider when choosing between these two methods with respect to hardware resources? 在硬件资源的这两种方法之间进行选择时,还需要考虑其他因素吗?

For clarification the ChickenCurry class would only contain a constructor where it sets the appropriate fields, like so: 为了澄清起见,ChickenCurry类将只包含一个在其中设置适当字段的构造函数,如下所示:

class ChickenCurry < Dish
  def initialize
    super("Chicken Curry", ...) 
  end
end

Which uses more resources, Dish.new or ChickenCurry.new ? 哪个使用了更多资源Dish.newChickenCurry.new Is the difference negligible? 差异可忽略不计吗? I plan to have thousands of these objects so even a 10 KB difference is worth considering. 我计划拥有数千个此类对象,因此即使相差10 KB也值得考虑。

I am using JRuby so please consider the JVM when answering but answers pertaining to Matz's Ruby are welcome too. 我正在使用JRuby,因此在回答时请考虑JVM,但也欢迎与Matz Ruby有关的答案。

This is NOT a question about design. 这不是关于设计的问题。 I'm only interested in resource usage. 我只对资源使用感兴趣。

Whole purpose of inheritance is that sub classes inheriting common things from super class and adding more specific things they need. 继承的全部目的是子类从超类继承通用事物,并添加它们需要的更具体的事物。

Why a Chicken Curry should have a field file sugarQuantity when it would not have any sugar. 为什么Chicken Curry在没有任何糖的情况下应该有一个字段field sugarQuantity So, IMHO you should use inheritance to make a good OO design, so have a super class which will have common things and then each subclass will inherit the common and add more specific to it. 因此,恕我直言,您应该使用继承来进行良好的OO设计,因此,拥有一个具有共同点的超类,然后每个子类都将继承该共同点并为其添加更多特定内容。

If you will put everything in one class then it would result in code abuse with so many unnecessary field. 如果将所有内容都放在一个类中,则会导致代码滥用,并包含许多不必要的字段。

In short, you need to use concept of Inheritance and Cohesion for make a good OO design, as per your case. 简而言之,您需要根据情况使用继承内聚的概念来进行良好的OO设计。

What uses more memory, a Class object or an instance of that class? 什么使用更多的内存,一个Class对象或该类的实例?

Definitely using inheritance will result in lesser memory usage when you will construct the objects of your dishes. 当您构造餐具的对象时,绝对使用继承将导致较少的内存使用。

An object is the runtime representation of a class and it will occupy memory as per memory requirement of fields in the class. 对象是类的运行时表示形式,它将根据类中字段的内存要求占用内存。 Try to minimize the number of fields (through inheritance and actual implementation) and you will have good memory footprint. 尝试最小化字段数(通过继承和实际实现),您将拥有良好的内存占用。 If you will put everything in one class then definitely there will be a very bad memory foot print. 如果将所有内容都放在一个类中,那么肯定会有非常糟糕的记忆足迹。

Consider you have 1000 fields in same dish class, and while creating object, all 1000 fields will occupy some memory space, as per their data types. 假设您在同一个菜品类中有1000个字段,并且在创建对象时,根据它们的数据类型,所有1000个字段都将占用一些内存空间。

With inheritance and cohesion, you can have far more better memory footprint + OO design. 通过继承和内聚,您可以拥有更好的内存占用+ OO设计。

PS: I am a vegetarian :) PS:我是素食主义者:)

Don't worry about memory, the main question to ask yourself is: Does your ChickenCurry has any behaviour that extends Dish 's behaviour? 不用担心记忆,要问自己的主要问题是: ChickenCurry是否有任何行为可以扩大Dish的行为? If your answer is no (like having only the constructor as you said) then use only one class. 如果您的答案是否定的(就像您说的那样只有构造函数),则仅使用一个类。

Make your system clear to be understood. 使您的系统清晰易懂。

Keep this in your heart: 请记住这一点:

"Premature optimization is the root of all evil" - DonaldKnuth “过早的优化是万恶之源”-DonaldKnuth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM