Android performance: useless global variables and inheritance

Suppose I have objects a and B that extend A. B has global variables (an array and some counters) that are not related to a

Due to the high cost of explicit conversion (I'm not sure how much), from a purely performance point of view, it would be better to create class a only and create arrays only when needed, so I don't have to convert?

I think the question is, even if the object's global variables are not used, will it pay any price?

Editor: obviously, I forgot to add the most important function

resolvent:

In recent years, inheritance is often regarded as code smell because it causes different problems: https://dzone.com/articles/is-inheritance-dead

In pure performance terms, an empty array needs about 8 bytes in RAM (4-byte storage length and 4-byte reference, but it is platform related: how much space does an array occupancy?). Therefore, even if you have 1000 such objects, an array field will occupy about 1 in RAM_ 000 * 8 bytes ~ 8 KB

As you may know, today's phones usually contain > 1 GB of ram. However, don't forget that your application can usually occupy 60 to 192 MB of RAM (Detect application heap size in Android)

In any case, it is sufficient not to calculate each small field to be added to the class

However, back to my first sentence, I suggest you consider using composition instead of inheritance to solve the problem, as suggested in effective Java

Update about performance, I suggest you read the following topic: the performance impact of using instanceof in Java. Are you sure you need this type of premature optimization? Or is it more theoretical than practical?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>