引言
性能优化是 Android 开发中的重要环节,直接影响用户体验和应用的成功。一个性能优异的应用不仅能提供流畅的用户交互,还能减少电池消耗、降低内存占用,提高用户满意度。本文将深入探讨 Android 性能优化的各个方面,包括内存优化、CPU 优化、网络优化、UI 渲染优化等,并提供实用的优化策略和工具使用指南。
性能优化概述
性能指标
在进行性能优化之前,我们需要了解关键的性能指标:
- 启动时间:应用从点击图标到完全可用的时间
- 内存使用:应用运行时的内存占用情况
- CPU 使用率:处理器资源的使用效率
- 帧率(FPS):UI 渲染的流畅度
- 网络性能:数据传输的效率和延迟
- 电池消耗:应用对设备电量的影响
性能优化原则
// 性能优化的基本原则
class PerformanceOptimizationPrinciples {
// 1. 测量优先:先测量,再优化
fun measureFirst() {
// 使用 Profiler 工具测量性能
// 确定性能瓶颈所在
}
// 2. 避免过早优化
fun avoidPrematureOptimization() {
// 专注于算法和架构层面的优化
// 避免微观层面的过度优化
}
// 3. 用户体验优先
fun userExperienceFirst() {
// 优化用户可感知的性能问题
// 如启动时间、响应速度等
}
}
内存优化
内存泄漏检测和修复
// 常见的内存泄漏场景及解决方案
class MemoryLeakPrevention {
// 1. 静态引用导致的内存泄漏
class BadExample {
companion object {
// 不好的做法:静态引用 Activity
private var sActivity: Activity? = null
}
}
class GoodExample {
companion object {
// 好的做法:使用 WeakReference
private var sActivityRef: WeakReference<Activity>? = null
fun setActivity(activity: Activity) {
sActivityRef = WeakReference(activity)
}
fun getActivity(): Activity? {
return sActivityRef?.get()
}
}
}
// 2. Handler 内存泄漏
class ActivityWithHandler : AppCompatActivity() {
// 不好的做法:非静态内部类持有外部类引用
private val badHandler = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
// 处理消息
}
}
// 好的做法:使用静态内部类 + WeakReference
private val goodHandler = GoodHandler(this)
override fun onDestroy() {
super.onDestroy()
goodHandler.removeCallbacksAndMessages(null)
}
private class GoodHandler(activity: ActivityWithHandler) : Handler(Looper.getMainLooper()) {
private val activityRef = WeakReference(activity)
override fun handleMessage(msg: Message) {
activityRef.get()?.let { activity ->
// 处理消息
}
}
}
}
// 3. 监听器内存泄漏
class ListenerLeakPrevention {
private var locationManager: LocationManager? = null
private var locationListener: LocationListener? = null
fun startLocationUpdates(context: Context) {
locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
locationListener = object : LocationListener {
override fun onLocationChanged(location: Location) {
// 处理位置更新
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {}
override fun onProviderEnabled(provider: String) {}
override fun onProviderDisabled(provider: String) {}
}
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
locationManager?.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
1000L,
1f,
locationListener!!
)
}
}
fun stopLocationUpdates() {
locationListener?.let { listener ->
locationManager?.removeUpdates(listener)
}
locationListener = null
locationManager = null
}
}
}
内存使用优化
// 内存使用优化策略
class MemoryOptimization {
// 1. 对象池模式
class ObjectPool<T>(private val factory: () -> T, private val reset: (T) -> Unit) {
private val pool = mutableListOf<T>()
fun acquire(): T {
return if (pool.isNotEmpty()) {
pool.removeAt(pool.size - 1)
} else {
factory()
}
}
fun release(obj: T) {
reset(obj)
pool.add(obj)
}
}
// 使用对象池
private val stringBuilderPool = ObjectPool(
factory = { StringBuilder() },
reset = { it.clear() }
)
fun processStrings(strings: List<String>): String {
val sb = stringBuilderPool.acquire()
try {
strings.forEach { sb.append(it) }
return sb.toString()
} finally {
stringBuilderPool.release(sb)
}
}
// 2. 懒加载
class LazyInitialization {
// 懒加载重量级对象
private val expensiveObject by lazy {
createExpensiveObject()
}
private val databaseHelper by lazy {
DatabaseHelper(context)
}
fun useExpensiveObject() {
// 只有在真正需要时才创建对象
expensiveObject.doSomething()
}
private fun createExpensiveObject(): ExpensiveObject {
// 创建重量级对象的逻辑
return ExpensiveObject()
}
}
// 3. 内存缓存优化
class MemoryCache<K, V>(private val maxSize: Int) {
private val cache = object : LinkedHashMap<K, V>(0, 0.75f, true) {
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<K, V>?): Boolean {
return size > maxSize
}
}
@Synchronized
fun get(key: K): V? {
return cache[key]
}
@Synchronized
fun put(key: K, value: V): V? {
return cache.put(key, value)
}
@Synchronized
fun clear() {
cache.clear()
}
}
}
CPU 优化
算法和数据结构优化
// CPU 性能优化策略
class CpuOptimization {
// 1. 选择合适的数据结构
class DataStructureOptimization {
// 不好的做法:频繁的列表查找
fun findUserBad(users: List<User>, userId: String): User? {
return users.find { it.id == userId } // O(n) 时间复杂度
}
// 好的做法:使用 Map 进行快速查找
private val userMap = mutableMapOf<String, User>()
fun findUserGood(userId: String): User? {
return userMap[userId] // O(1) 时间复杂度
}
// 批量操作优化
fun updateUsersEfficiently(updates: List<UserUpdate>) {
// 批量更新而不是逐个更新
val batchUpdates = updates.groupBy { it.category }
batchUpdates.forEach { (category, categoryUpdates) ->
processBatchUpdate(category, categoryUpdates)
}
}
}
// 2. 避免重复计算
class ComputationOptimization {
// 使用缓存避免重复计算
private val fibonacciCache = mutableMapOf<Int, Long>()
fun fibonacci(n: Int): Long {
return fibonacciCache.getOrPut(n) {
when {
n <= 1 -> n.toLong()
else -> fibonacci(n - 1) + fibonacci(n - 2)
}
}
}
// 预计算常用值
companion object {
private val COMMON_CALCULATIONS = (1..100).associateWith { it * it }
}
fun getSquare(n: Int): Int {
return COMMON_CALCULATIONS[n] ?: (n * n)
}
}
// 3. 异步处理
class AsyncProcessing {
suspend fun processDataAsync(data: List<DataItem>): List<ProcessedData> {
return withContext(Dispatchers.Default) {
data.chunked(100) // 分块处理
.map { chunk ->
async {
chunk.map { processItem(it) }
}
}
.awaitAll()
.flatten()
}
}
private suspend fun processItem(item: DataItem): ProcessedData {
// CPU 密集型处理
return ProcessedData(item)
}
}
}
多线程优化
// 多线程性能优化
class ThreadOptimization {
// 1. 线程池管理
class ThreadPoolManager {
// CPU 密集型任务线程池
private val cpuIntensiveExecutor = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors()
)
// I/O 密集型任务线程池
private val ioExecutor = Executors.newCachedThreadPool()
// 自定义线程池
private val customExecutor = ThreadPoolExecutor(
2, // 核心线程数
4, // 最大线程数
60L, // 空闲线程存活时间
TimeUnit.SECONDS,
LinkedBlockingQueue(100), // 任务队列
ThreadFactory { runnable ->
Thread(runnable, "CustomThread").apply {
isDaemon = true
priority = Thread.NORM_PRIORITY
}
}
)
fun executeCpuTask(task: Runnable) {
cpuIntensiveExecutor.execute(task)
}
fun executeIoTask(task: Runnable) {
ioExecutor.execute(task)
}
fun shutdown() {
cpuIntensiveExecutor.shutdown()
ioExecutor.shutdown()
customExecutor.shutdown()
}
}
// 2. 协程优化
class CoroutineOptimization {
// 使用合适的调度器
suspend fun performOptimizedOperations() {
// CPU 密集型任务
val cpuResult = withContext(Dispatchers.Default) {
performCpuIntensiveTask()
}
// I/O 操作
val ioResult = withContext(Dispatchers.IO) {
performNetworkRequest()
}
// UI 更新
withContext(Dispatchers.Main) {
updateUI(cpuResult, ioResult)
}
}
// 并行处理
suspend fun processInParallel(items: List<Item>): List<Result> {
return items.map { item ->
async(Dispatchers.Default) {
processItem(item)
}
}.awaitAll()
}
}
}
UI 渲染优化
布局优化
// UI 渲染性能优化
class UIOptimization {
// 1. ViewHolder 模式优化
class OptimizedAdapter(private val items: List<Item>) : RecyclerView.Adapter<OptimizedAdapter.ViewHolder>() {
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val titleView: TextView = itemView.findViewById(R.id.title)
private val subtitleView: TextView = itemView.findViewById(R.id.subtitle)
private val imageView: ImageView = itemView.findViewById(R.id.image)
fun bind(item: Item) {
titleView.text = item.title
subtitleView.text = item.subtitle
// 使用图片加载库优化
Glide.with(itemView.context)
.load(item.imageUrl)
.placeholder(R.drawable.placeholder)
.into(imageView)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_layout, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(items[position])
}
override fun getItemCount(): Int = items.size
}
// 2. 减少过度绘制
class OverdrawOptimization {
fun optimizeBackground() {
// 移除不必要的背景
// 在 XML 中设置 android:background="@null"
// 或在代码中设置 view.background = null
}
fun useClipToPadding() {
// 在 RecyclerView 中使用 clipToPadding
// android:clipToPadding="false"
}
}
// 3. 自定义 View 优化
class OptimizedCustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val bounds = Rect()
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
// 避免在 onDraw 中创建对象
canvas?.let { c ->
// 使用预先创建的对象
c.getClipBounds(bounds)
c.drawRect(bounds, paint)
}
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
// 优化测量逻辑
val desiredWidth = 100
val desiredHeight = 100
val width = resolveSize(desiredWidth, widthMeasureSpec)
val height = resolveSize(desiredHeight, heightMeasureSpec)
setMeasuredDimension(width, height)
}
}
}
Jetpack Compose 性能优化
// Compose 性能优化
class ComposeOptimization {
// 1. 避免不必要的重组
@Composable
fun OptimizedList(items: List<Item>) {
LazyColumn {
items(
items = items,
key = { item -> item.id } // 提供稳定的 key
) { item ->
ItemCard(
item = item,
onClick = { /* 处理点击 */ }
)
}
}
}
@Composable
fun ItemCard(
item: Item,
onClick: () -> Unit
) {
// 使用 remember 缓存计算结果
val formattedDate = remember(item.timestamp) {
formatDate(item.timestamp)
}
Card(
modifier = Modifier
.fillMaxWidth()
.clickable { onClick() }
) {
Column {
Text(text = item.title)
Text(text = formattedDate)
}
}
}
// 2. 使用 derivedStateOf 优化计算
@Composable
fun SearchableList(items: List<Item>) {
var searchQuery by remember { mutableStateOf("") }
val filteredItems by remember {
derivedStateOf {
if (searchQuery.isBlank()) {
items
} else {
items.filter { it.title.contains(searchQuery, ignoreCase = true) }
}
}
}
Column {
SearchField(
query = searchQuery,
onQueryChange = { searchQuery = it }
)
LazyColumn {
items(filteredItems) { item ->
ItemCard(item = item, onClick = { })
}
}
}
}
// 3. 稳定的参数
@Stable
data class StableItem(
val id: String,
val title: String,
val description: String
)
@Composable
fun StableItemCard(
item: StableItem, // 稳定的参数
onClick: (String) -> Unit // 使用具体类型而不是 () -> Unit
) {
Card(
modifier = Modifier.clickable { onClick(item.id) }
) {
Text(text = item.title)
}
}
}
网络优化
网络请求优化
// 网络性能优化
class NetworkOptimization {
// 1. 连接池和缓存配置
class OptimizedNetworkClient {
private val okHttpClient = OkHttpClient.Builder()
.connectionPool(ConnectionPool(5, 5, TimeUnit.MINUTES))
.cache(Cache(File(context.cacheDir, "http_cache"), 10 * 1024 * 1024)) // 10MB 缓存
.addInterceptor(HttpLoggingInterceptor().apply {
level = if (BuildConfig.DEBUG) {
HttpLoggingInterceptor.Level.BODY
} else {
HttpLoggingInterceptor.Level.NONE
}
})
.addNetworkInterceptor { chain ->
val response = chain.proceed(chain.request())
response.newBuilder()
.header("Cache-Control", "public, max-age=300") // 5分钟缓存
.build()
}
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build()
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
// 2. 请求合并和批处理
class BatchRequestManager {
private val pendingRequests = mutableListOf<String>()
private val batchHandler = Handler(Looper.getMainLooper())
fun requestData(id: String, callback: (Data?) -> Unit) {
pendingRequests.add(id)
// 延迟执行,收集更多请求
batchHandler.removeCallbacks(batchRunnable)
batchHandler.postDelayed(batchRunnable, 100)
}
private val batchRunnable = Runnable {
if (pendingRequests.isNotEmpty()) {
val ids = pendingRequests.toList()
pendingRequests.clear()
// 批量请求
executeBatchRequest(ids)
}
}
private fun executeBatchRequest(ids: List<String>) {
// 实现批量请求逻辑
}
}
// 3. 图片加载优化
class ImageLoadingOptimization {
fun setupGlide(context: Context) {
val glideModule = object : AppGlideModule() {
override fun applyOptions(context: Context, builder: GlideBuilder) {
// 配置内存缓存
val memoryCacheSizeBytes = 1024 * 1024 * 20 // 20MB
builder.setMemoryCache(LruResourceCache(memoryCacheSizeBytes.toLong()))
// 配置磁盘缓存
val diskCacheSizeBytes = 1024 * 1024 * 100 // 100MB
builder.setDiskCache(InternalCacheDiskCacheFactory(context, diskCacheSizeBytes.toLong()))
}
}
}
fun loadImageOptimized(imageView: ImageView, url: String) {
Glide.with(imageView.context)
.load(url)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.transform(CenterCrop(), RoundedCorners(16))
.into(imageView)
}
}
}
启动优化
应用启动时间优化
// 启动性能优化
class StartupOptimization {
// 1. Application 优化
class OptimizedApplication : Application() {
override fun onCreate() {
super.onCreate()
// 关键初始化
initCriticalComponents()
// 延迟初始化非关键组件
Handler(Looper.getMainLooper()).post {
initNonCriticalComponents()
}
// 异步初始化
Thread {
initBackgroundComponents()
}.start()
}
private fun initCriticalComponents() {
// 只初始化启动必需的组件
// 如崩溃报告、基础配置等
}
private fun initNonCriticalComponents() {
// 延迟初始化的组件
// 如分析工具、广告 SDK 等
}
private fun initBackgroundComponents() {
// 后台初始化的组件
// 如数据库预热、缓存初始化等
}
}
// 2. Activity 启动优化
class OptimizedMainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 使用 ViewStub 延迟加载复杂布局
setContentView(R.layout.activity_main_optimized)
// 异步加载数据
lifecycleScope.launch {
loadInitialData()
}
}
private suspend fun loadInitialData() {
withContext(Dispatchers.IO) {
// 在后台线程加载数据
val data = repository.getInitialData()
withContext(Dispatchers.Main) {
// 在主线程更新 UI
updateUI(data)
}
}
}
}
// 3. 启动画面优化
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 检查是否需要显示启动画面
if (isAppReady()) {
startMainActivity()
finish()
return
}
setContentView(R.layout.activity_splash)
// 异步准备应用
lifecycleScope.launch {
prepareApp()
startMainActivity()
finish()
}
}
private suspend fun prepareApp() {
// 执行必要的初始化工作
withContext(Dispatchers.IO) {
// 数据库初始化、配置加载等
}
}
private fun startMainActivity() {
startActivity(Intent(this, MainActivity::class.java))
}
}
}
性能监控和分析
性能监控工具
// 性能监控实现
class PerformanceMonitoring {
// 1. 自定义性能监控
class CustomPerformanceMonitor {
private val performanceData = mutableMapOf<String, Long>()
fun startTiming(tag: String) {
performanceData[tag] = System.currentTimeMillis()
}
fun endTiming(tag: String): Long {
val startTime = performanceData.remove(tag) ?: return 0
val duration = System.currentTimeMillis() - startTime
// 记录性能数据
logPerformance(tag, duration)
return duration
}
private fun logPerformance(tag: String, duration: Long) {
if (BuildConfig.DEBUG) {
Log.d("Performance", "$tag took ${duration}ms")
}
// 发送到分析服务
analyticsService.trackPerformance(tag, duration)
}
}
// 2. 内存监控
class MemoryMonitor {
fun logMemoryUsage(tag: String) {
val runtime = Runtime.getRuntime()
val usedMemory = runtime.totalMemory() - runtime.freeMemory()
val maxMemory = runtime.maxMemory()
val availableMemory = maxMemory - usedMemory
Log.d("Memory", "$tag - Used: ${usedMemory / 1024 / 1024}MB, Available: ${availableMemory / 1024 / 1024}MB")
}
fun checkMemoryPressure(): Boolean {
val runtime = Runtime.getRuntime()
val usedMemory = runtime.totalMemory() - runtime.freeMemory()
val maxMemory = runtime.maxMemory()
return (usedMemory.toDouble() / maxMemory) > 0.8 // 80% 内存使用率
}
}
// 3. FPS 监控
class FpsMonitor {
private var frameCount = 0
private var lastTime = System.currentTimeMillis()
fun onFrame() {
frameCount++
val currentTime = System.currentTimeMillis()
if (currentTime - lastTime >= 1000) { // 每秒计算一次
val fps = frameCount.toFloat() / ((currentTime - lastTime) / 1000f)
Log.d("FPS", "Current FPS: $fps")
if (fps < 30) {
Log.w("FPS", "Low FPS detected: $fps")
}
frameCount = 0
lastTime = currentTime
}
}
}
}
最佳实践总结
性能优化检查清单
// 性能优化最佳实践
class PerformanceBestPractices {
// 1. 内存管理
fun memoryBestPractices() {
// ✅ 使用 WeakReference 避免内存泄漏
// ✅ 及时释放资源(Cursor、Stream、Bitmap 等)
// ✅ 使用对象池重用对象
// ✅ 避免在循环中创建对象
// ✅ 使用 SparseArray 替代 HashMap(小数据集)
}
// 2. UI 优化
fun uiBestPractices() {
// ✅ 使用 ViewHolder 模式
// ✅ 减少布局层级
// ✅ 使用 merge、include、ViewStub 标签
// ✅ 避免过度绘制
// ✅ 使用硬件加速
}
// 3. 网络优化
fun networkBestPractices() {
// ✅ 使用连接池
// ✅ 启用 HTTP 缓存
// ✅ 压缩请求和响应
// ✅ 批量处理请求
// ✅ 使用 CDN 加速
}
// 4. 代码优化
fun codeBestPractices() {
// ✅ 选择合适的数据结构
// ✅ 避免不必要的计算
// ✅ 使用缓存机制
// ✅ 异步处理耗时操作
// ✅ 代码混淆和压缩
}
}
总结
Android 性能优化是一个系统性工程,需要从多个维度进行考虑和实施。关键要点包括:
- 测量驱动:使用 Profiler 等工具准确识别性能瓶颈
- 内存管理:避免内存泄漏,合理使用缓存和对象池
- UI 优化:减少过度绘制,优化布局层级,使用高效的列表组件
- 网络优化:使用缓存、连接池,批量处理请求
- 启动优化:延迟非关键组件初始化,优化启动流程
- 持续监控:建立性能监控体系,及时发现和解决问题
性能优化是一个持续的过程,需要在开发过程中始终关注性能指标,并根据实际使用情况不断调整和改进。通过系统性的性能优化,可以显著提升应用的用户体验和市场竞争力。
上一篇 下一篇