Should I leave methods that I don't use in the class?

I have a class that is used heavily (> 400,000 instances) in a high performance program. Does leaving these methods in a class seriously affect memory usage for each object, or does it not really matter?

+3


source to share


2 answers


The class is loaded into memory the first time it is used, and it is loaded only once in normal situations. In fact, the method is equals

Class

written as ==

, which means that Java expects it to be the same object.

This is in contrast to a class of instances that get memory allocated after instantiation and reclaimed by garbage collection.



More importantly, if your class didn't use fields . Each field will consume a small amount of memory, but when multiplied by the number of live instances can make up a large chunk of memory. This is true even if the field is value-initialized null

. If the field is initialized with a new object, then it can consume even more memory, depending on how large the objects are.

+3


source


The memory consumed by loading the class will match the size of the code, but the code will not be duplicated for each instance of the class.

An instance will require as much memory as the instance attributes + some overhead to manage the object instance itself.



as stated, there are maintenance costs and what not. it is usually best to remove the dead code, however the change also has a cost. consider these aspects.

+4


source







All Articles