简体   繁体   中英

OOP design vs performance on smartphones

I recommend this book: Beginning Android 4 Games Development by Mario Zechner and Robert Green, for complete beginners in android games.

The book states on page 192:

Method calls have a larger associated cost in Dalvik than in other VMs. Use static methods if you can, as those perform best. Static methods are generally regarded as evil, much like static variables, as they promote bad design, so try to keep your design as clean as possible. Perhaps you should avoid getters and setters as well. Direct field access is about three times faster than method invocations without the JIT, and about seven times faster with the JIT. Nevertheless, think of your design before removing all your getters and setters.

Now, does this have a huge impact nowadays? What is really the best between performance and design? Because if im going to have static variables and methods, its gonna be in the RAM until the application terminates, that would be bad if my application is too large and android 2.3 will be part of the market.

What developers often forget when doing micro -optimizations is optimization should be done without breaking the design . Whether a field should be static or non-static is purely a design choice . Likewise whether having getters and setters makes sense is also based on choice of whether you want to make the field private or public .

So, once you complete your designing phase and come to implementation phase, then look at optimizations like using StringBuilder over String#concat . When you enter the implementation phase , you would have already decided on whether a field has to be private , whether it has to be static etc.

Now, to the technical part - actually static method calls always ensure compile time binding. So calls to them are less expensive .

Usually direct field access is faster than using a getter / setter (the JIT might inline getters / setters, but it still is expensive than direct accesses).

So, the book is correct about performance. But you should not compromise on design to improve performance .

Performance is a great factor to be considered while developing on android powered devices , as we need to consider low configuration devices as well.

As far as static variables and methods usage is considered , it totally depends on what particular devices , the app is targeted to.

You are absolutely correct - static variables and methods, will reside in the RAM until the app terminates , but you see thats not a problem if your android device is powered with 1+ GB of RAM and trust me if you are targeting 2.3 , the app will have a lot of ANR's as the memory required by variouse objects will not be available freely.

Best shot for you is to focus more on :

  • Modular design , preferably MVC with singelton or factory pattern.
  • Use correct high level optimizations (by code or by using 3rd party libs eg: use jackson for parsing instead of traditional way or use Volley for network connections).
  • Do proper profiling using tools like MAT , DDMs.
  • release memory when UI becomes hidden

Post development : Check how much memory your app is using Each Android-powered device has a different amount of RAM available to the system and thus provides a different heap limit for each app.

You can call getMemoryClass() to get an estimate of app's available heap in megabytes.

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