大致分为四点去回答。快、稳、小、省
1. 快
启动快,加载快,避免卡顿
基本操作
-
主线程不做耗时操作
-
application里对必要的三方库延迟初始化(延迟加载,异步加载,分布加载)
-
启动白屏优化
View优化
-
View 布局(viewstub,include,merge,层级深)
-
复杂页面细分优化
-
过度绘制的优化
-
xml中无用的背景不设置
-
控件无用属性删除
内存优化
-
页面切换,前后台切换
-
fragment的懒加载
-
必要的缓存
-
空间换时间
-
四大引用的合理使用
-
减小不必要的内存开销
-
数据bean的合理定义
-
ArrayList、HashMap的使用
-
线程池、bitmap、view的复用
-
不用的大对象主动设置null
代码优化
-
for循环内不定义对象
-
使用文件IO代替数据库
-
自定义Drawable不在draw()里面创建对象操作
-
类中没有使用到成员变量的方法可以设置static
2. 稳
稳定不崩溃,减小crash,避免anr
-
主线程不做耗时操作
-
activity 5秒、broadcast 10秒、service 20秒
-
资源对象及时关闭(Cursor,File)
-
Handler的处理
-
避免内存泄露
-
crash上传机制
-
WebView的内存泄露
3. 小
安装包小
-
代码混淆(proguard)
-
资源优化(lint)
-
图片优化(mipmap/webp)
4. 省
省电省流量
-
接口定义
-
接口缓存
**性能分析工具:**MAT/TracView/LeakCanary/blockCanary/MemoryMonitor/HeapViewer
HashMap分析
- 基础知识
-
可以接受null键和值,而Hashtable则不能
-
非synchronized,所以很快
-
存储的是键值对
-
使用数组+链表的方式存储数据
-
对key进行hash(散列)算法,所以顺序不固定
-
实际使用Node存储
- 常量&变量
// public class HashMap extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable {}
/**
- The default initial capacity - MUST be a power of two.
默认数组长度
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
/**
-
The maximum capacity, used if a higher value is implicitly specified
-
by either of the constructors with arguments.
-
MUST be a power of two <= 1<<30.
-
数组最大长度
*/
static final int MAXIMUM_CAPACITY = 1 << 30;
/**
-
The load factor used when none specified in constructor.
-
默认装填因子
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
}
/**
- The number of key-value mappings contained in this map.
*/
transient int size;
/**
-
阈值
-
The next size value at which to resize (capacity * load factor).
-
@serial
*/
// (The javadoc description is true upon serialization.
// Addit

最低0.47元/天 解锁文章
313

被折叠的 条评论
为什么被折叠?



