简体   繁体   中英

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

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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