AndroidSeveral common bottlenecks in software development
Different resolution Adaption
Debugging of different versions
Language? Performance? Touch? Animation?
Memory, memory, memory...
AndroidSoftware Memory limit
AndroidWhat the system can use for each softwareRamSpace Restriction(For example: Nexus OneThe memory limit for each software is24 m)
JavaThe language itself consumes more memory.
DalvikVirtual machines also need to occupy a certain amount of memory space
OOMHero-Bitmap
WhenAndroidDirect Reading on mobile phone4 mWhen there are images, death usually falls.
Often, photos taken by mobile phones cannot be directly read.
OOMHero-Bitmap
Tip: Check the size of an image before reading it.
Bitmapfactory. Options opts = new bitmapfactory. Options ();
Opts. injustdecodebounds = true;
Bitmap bitmap = bitmapfactory. decodefile (imagefile, opts );
Calculate the original width and height of the image.Smaplesize
Bitmapfactory. Options opts = new bitmapfactory. Options ();
Opts. insamplesize = 4;
Bitmap bitmap = bitmapfactory. decodefile (imagefile, opts );
OOMHero-Bitmap
For outdatedBitmapObjects must be timelyRecycle
And assign this objectNull
Bitmap. Recycle ();
Bitmap = NULL;
ViewOptimization
Use9 patchFormat Image Resources
UsePNGImage compression Tool9 patchFormat Image Compression
Use-Land,-mdpi,-hdpiEtc.LayoutAnd of courseWableSuffix,Image adaptation and resolution adaptation
ViewOptimization
UseLayoutoptOptimization
$ Layoutoptsamples/samples/compound. xml
The root-level <framelayout/> can be replaced with <merge/>
This linearlayoutlayout or its framelayoutparent is useless samples/simple. xml
The root-level <framelayout/> can be replaced with <merge/> samples/too_deep.xml
This layout has too implements nested layouts: 13 levels, it shoshould have <= 10!
ReferenceHttp://android-developers.blogspot.com/2009/11/optimize-your-layouts.html
Reduce LogicCodeWorkload
Class myactivityextends activity {
Public void myclickhandler (view v ){
// Do stuff
}}
InLayout XMLAdd to file:
<Button Android: onclick = "myclickhandler"/>
You can create a file in the code.Onclicklistener
Least usedView
Linearlayoutxmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width =" fill_parent "Android: layout_height ="? Android: ATTR/listpreferreditemheight "Android: padding =" 6dip ">
<Imageviewandroid: Id = "@ + ID/icon" Android: layout_width = "wrap_content" Android: layout_height = "fill_parent" Android: layout_marginright = "6dip" Android: src = "@ drawable/icon"/>
<Linearlayoutandroid: Orientation = "vertical" Android: layout_width = "0dip" Android: layout_weight = "1" Android: layout_height = "fill_parent">
<Textviewandroid: layout_width = "fill_parent" Android: layout_height = "0dip" Android: layout_weight = "1" Android: gravity = "center_vertical" Android: text = "my application"/>
<Textviewandroid: layout_width = "fill_parent" Android: layout_height = "0dip" Android: layout_weight = "1" Android: singleline = "true" Android: ellipsize = "marquee" Android: TEXT = "simple application that shows how to use relativelayout"/>
</Linearlayout>
</Linearlayout>
Remove unnecessaryView
<Resources>
<Style name = "theme. nobackground" parent = "Android: Theme">
<Item name = "Android: windowbackground"> @ null </item> </style>
</Resources>
InManifest. xmlOfActivityOrApplicationAddAndroid: theme = "@ style/theme. nobackground"
Reduce image size
Res/drawable/background_shelf.xmlUsed in:
<Bitmap xmlns: Android = "http://schemas.android.com/apk/res/android" Android: src = "@ drawable/shelf_panel" Android: tilemode = "repeat"/>
UseStub
A viewstub is an invisible, zero-sized view that can be used to lazily inflate layout resources at runtime
<Viewstub Android: Id = "@ + ID/stub" Android: inflatedid = "@ + ID/subtree"Android: Layout = "@ layout/mysubtree" Android: layout_width = "120dip" Android: layout_height = "40dip"/>
Viewstub stub = (viewstub) findviewbyid (R. Id. stub );
View inflated = stub. Inflate ();
Code-level memory optimization
AndroidProvides a sound message transmission mechanism(Intent)And task model(Handler ),You can prevent unnecessary global variables by passing or events.
You can useSoftrerenceOrWeakrerenceReference them,It can ensure that when the system memory is insufficient, they will first release rather than appearOOM
If a large number of objects or combined data are involvedAlgorithmOperation, it is best to use C language to encapsulate these objects and perform operations, throughJNIForJavaLanguage for the simplest data communication. Because even if it is nullJavaLanguage objects also occupy20Multiple bytes
AndroidSoftware Performance Optimization
AndroidPerformance killer:
1. GC,SystemGCInefficiency
2. LayoutLayout andViewDisplay
3. LoadReading images and files
AndroidSoftware Performance Optimization
Tips for improving performance:
1.Reduce frequent generation and destruction of temporary objects
2.UseViewstubDelayed loading of some complexLayout
3.The operations in a loop or multi-layer loop are as simple as possible and more enjoyable.JITAcceleration
4.OccupiedCPUAs much data operations as possible in a separate thread, throughHandlerAnd other methods to deliver the execution resultUIThread display
Summary
AndroidThe use of lifecycle is everywhere in the system, includingActivity,ViewAnd so on. This method has positive significance in thread synchronization andProgramThe stages of execution are clearly divided.
We only need to strictly followAndroidThe life cycle of program execution. When Processing Memory-consuming tasks, compression, buffering, and streaming are used.CPUAdditional threads are used for consumable tasks,UIThread display mode, you can compileAndroidAvoid a lot of weird issues during the program.