Java 世界的盘古和女娲 —— Zygote,Java语言自学书籍

ZygoteServer.java

void registerServerSocketFromEnv(String socketName) {
if (mServerSocket == null) {
int fileDesc;
final String fullSocketName = ANDROID_SOCKET_PREFIX + socketName;
try {
// 从环境变量中获取 socket 的 fd
String env = System.getenv(fullSocketName);
fileDesc = Integer.parseInt(env);
} catch (RuntimeException ex) {
throw new RuntimeException(fullSocketName + " unset or invalid", ex);
}

try {
FileDescriptor fd = new FileDescriptor();
fd.setInt$(fileDesc); // 设置文件描述符
mServerSocket = new LocalServerSocket(fd); // 创建服务端 socket
mCloseSocketFd = true;
} catch (IOException ex) {
throw new RuntimeException(
“Error binding to local socket '” + fileDesc + “’”, ex);
}
}
}

首先从环境变量中获取 socket 的文件描述符 fd,然后根据 fd 创建服务端 LocalServerSocket,用于 IPC 通信。这里的环境变量是在 init 进程创建 Zygote 进程时设置的。

preload()

ZygoteInit.java

static void preload(TimingsTraceLog bootTimingsTraceLog) {

preloadClasses(); // 预加载并初始化 /system/etc/preloaded-classes 中的类

preloadResources(); // 预加载系统资源

nativePreloadAppProcessHALs(); // HAL?

preloadOpenGL(); // 预加载 OpenGL

preloadSharedLibraries(); // 预加载 共享库,包括 android、compiler_rt、jnigraphics 这三个库
preloadTextResources(); // 预加载文字资源
// Ask the WebViewFactory to do any initialization that must run in the zygote process,
// for memory sharing purposes.
// WebViewFactory 中一些必须在 zygote 进程中进行的初始化工作,用于共享内存
WebViewFactory.prepareWebViewInZygote();
warmUpJcaProviders();

sPreloadComplete = true;
}

preload() 方法主要进行一些类,资源,共享库的预加载工作,以提升运行时效率。下面依次来看一下都预加载了哪些内容。

preloadClasses()

ZygoteInit.java

private static void preloadClasses() {

InputStream is;
try {
// /system/etc/preloaded-classes
is = new FileInputStream(PRELOADED_CLASSES);
} catch (FileNotFoundException e) {
Log.e(TAG, "Couldn’t find " + PRELOADED_CLASSES + “.”);
return;
}

try {
BufferedReader br
= new BufferedReader(new InputStreamReader(is), 256);

int count = 0;
String line;
while ((line = br.readLine()) != null) {
// Skip comments and blank lines.
line = line.trim();
if (line.startsWith("#") || line.equals("")) {
continue;
}

try {
// Load and explicitly initialize the given class. Use
// Class.forName(String, boolean, ClassLoader) to avoid repeated stack lookups
// (to derive the caller’s class-loader). Use true to force initialization, and
// null for the boot classpath class-loader (could as well cache the
// class-loader of this class in a variable).
Class.forName(line, true, null);
count++;
} catch (ClassNotFoundException e) {
Log.w(TAG, "Class not found for preloading: " + line);
} catch (UnsatisfiedLinkError e) {
Log.w(TAG, "Problem preloading " + line + ": " + e);
} catch (Throwable t) {

}
}
} catch (IOException e) {
Log.e(TAG, "Error reading " + PRELOADED_CLASSES + “.”, e);
} finally {
IoUtils.closeQuietly(is);

}
}

只保留了核心逻辑代码。读取 /system/etc/preloaded-classes 文件,并通过 Class.forName() 方法逐行加载文件中声明的类。提前预加载系统常用的类无疑可以提升运行时效率,但是这个预加载常用类的工作通常都会很重。搜索整个源码库,在 /frameworks/base/config 目录下发现一份 preloaded-classes 文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值