[英]Does having more methods in a class mean that object uses more memory at runtime
Say I have one class ClassBig
with 100 methods inside, and a second with only 10 methods ClassSmall
假设我有一个
ClassBig
,里面有100个方法,第二个只有10个方法ClassSmall
When I have objects at runtime 当我在运行时有对象时
ClassBig big = new ClassBig();
ClassSmall small = new ClassSmall();
Does the larger class take up more memory space? 较大的类是否占用更多的内存空间?
If both classes contained an identical method, does the larger class take longer to execute it? 如果两个类都包含相同的方法,那么较大的类是否需要更长的时间来执行它?
The in-memory representation of an instance of a class is mainly just its internal state plus a pointer to an in-memory representation of the class itself. 类的实例的内存中表示主要是它的内部状态加上指向类本身的内存表示的指针。 The internal representation of an instance method has one more argument than you specified in the class definition - the implicit
this
reference. 实例方法的内部表示比您在类定义中指定的参数多一个 - 隐式的
this
引用。 This is how we can store only one copy of the instance method, rather than a new copy for every instance. 这就是我们只能存储实例方法的一个副本,而不是每个实例的新副本。
So a class with more methods will take up more memory than a class with less methods (the code has to go somewhere), but an instance of a class with more methods will use the same amount of memory, assuming the classes have the same data members. 因此,具有更多方法的类将比具有更少方法的类占用更多内存(代码必须在某处),但是具有更多方法的类的实例将使用相同数量的内存,假设类具有相同的数据成员。
Execution time will not be affected by the number of other methods in the class. 执行时间不受类中其他方法数量的影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.