http://source.android.com/devices/low-ram.html
在低内存设备上运行 Android
Android 现在支持只有 512MB RAM 内存的设备。这篇文档帮助大家配置 Android 4.4 ,使其能够在低内存设备上运行。以下的优化有些是通用的,你也可以把它们应用在以前的版本上。
Android 4.4 的平台优化
Android 4.4 本身对低内存设备做了很多系统平台上的优化。
- 改进了内存管理
- 减少了系统内存占用
编译时配置
- 使能低内存配置标记
- 禁止 JIT
- 启动器配置
内核配置
- 调整内核/ ActivityManager,以减少直接回收
- 调整 LowMemoryKiller
- KSM (Kernel samepage merging)
- 交换到ZRAM
- 离子和连续的内存分配(CMA)
Application optimization tips
应用程序可以检查运行的系统设备是不是一个低内存设备,然后根据设备采取不同的计算和处理措施。
例如,我们在自己的设备上做了低内存设备标记:
PRODUCT_PROPERTY_OVERRIDES += ro.config.low_ram=true \
wifi.interface=wlan0 \
dalvik.vm.jit.codecachesize=0
property_get("ro.config.low_ram", propBuf, "");
if (strcmp(propBuf, "true") == 0) {
opt.optionString = "-XX:LowMemoryMode";
mOptions.add(opt);
}
public static boolean isLowRamDeviceStatic() {
return "true".equals(SystemProperties.get("ro.config.low_ram", "false"));
}
public boolean isLowRamDevice() {
return isLowRamDeviceStatic();
}
低内存设备意味着更少的内存能够被应用使用,往往也代表着系统的计算和图形处理能力相对比较弱。应用程序可以在这些设备上去掉一些耗费 cpu 和 内存的操作,避免程序卡顿,造成用户体验不佳。
与低内存设备相反的一个属性支持硬件加速绘图,这将耗费大量内存,故该特性只能在非低内存设备上使能:
/**
* Used by persistent processes to determine if they are running on a
* higher-end device so should be okay using hardware drawing acceleration
* (which tends to consume a lot more RAM).
* @hide
*/
static public boolean isHighEndGfx() {
return !isLowRamDeviceStatic() &&
!Resources.getSystem().getBoolean(com.android.internal.R.bool.config_avoidGfxAccel);
}
例如:系统壁纸,将会检查这一个特性来决定是否启用 opengl 绘图:
@Override
public void onCreate() {
super.onCreate();
mWallpaperManager = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
//noinspection PointlessBooleanExpression,ConstantConditions
if (FIXED_SIZED_SURFACE && USE_OPENGL) {
if (!isEmulator()) {
mIsHwAccelerated = ActivityManager.isHighEndGfx(); }
}
}
if (mIsHwAccelerated) {
if (!drawWallpaperWithOpenGL(sh, availw, availh, xPixels, yPixels)) {
drawWallpaperWithCanvas(sh, availw, availh, xPixels, yPixels);
}
} else {
drawWallpaperWithCanvas(sh, availw, availh, xPixels, yPixels);
if (FIXED_SIZED_SURFACE) {
// If the surface is fixed-size, we should only need to
// draw it once and then we'll let the window manager
// position it appropriately. As such, we no longer needed
// the loaded bitmap. Yay!
// hw-accelerated path retains bitmap for faster rotation
mBackground = null;
mWallpaperManager.forgetLoadedWallpaper();
}
}
}