我的android应用程序代码中有一个mkDirs
方法,很简单:
public static File mkDirs(File dir) {
long start = System.currentTimeMillis();
if (!dir.exists()) {
dir.mkdirs();
System.out.println("*** mk dir, cost: " + (System.currentTimeMillis() - start) + "ms");
}
return dir;
}
我在应用程序中多次调用它来在SD卡上创建目录,路径基于Environment.getExternalStorageDirectory()
。
这是日志:
11-09 21:40:18.002: INFO/System.out(28259): *** mk dir, cost: 2181ms
11-09 21:40:20.504: INFO/System.out(28259): *** mk dir, cost: 1629ms
11-09 21:40:20.570: INFO/System.out(28259): *** mk dir, cost: 36ms
11-09 21:40:20.635: INFO/System.out(28259): *** mk dir, cost: 38ms
11-09 21:40:20.697: INFO/System.out(28259): *** mk dir, cost: 35ms
11-09 21:40:20.760: INFO/System.out(28259): *** mk dir, cost: 38ms
11-09 21:40:20.827: INFO/System.out(28259): *** mk dir, cost: 34ms
11-09 21:40:20.909: INFO/System.out(28259): *** mk dir, cost: 52ms
11-09 21:40:20.970: INFO/System.out(28259): *** mk dir, cost: 35ms
11-09 21:41:17.433: INFO/System.out(28259): *** mk dir, cost: 24184ms
11-09 21:42:42.570: INFO/System.out(28259): *** mk dir, cost: 1593ms
11-09 21:44:26.709: INFO/System.out(28259): *** mk dir, cost: 1254ms
你可以看到它有时非常快,但有些东西很慢 - 超过20秒。
我认为它可能是由ram造成的,这可能还不够:512M,还有一些其他应用程序在后台运行。
所以我重新启动手机并重新启动应用程序,这是日志:
11-09 22:17:27.680: INFO/System.out(1000): *** mk dir, cost: 1983ms
11-09 22:17:35.138: INFO/System.out(1000): *** mk dir, cost: 2752ms
11-09 22:17:36.020: INFO/System.out(1000): *** mk dir, cost: 37ms
11-09 22:17:36.080: INFO/System.out(1000): *** mk dir, cost: 34ms
11-09 22:17:36.159: INFO/System.out(1000): *** mk dir, cost: 54ms
11-09 22:17:36.221: INFO/System.out(1000): *** mk dir, cost: 38ms
11-09 22:17:36.281: INFO/System.out(1000): *** mk dir, cost: 34ms
11-09 22:17:38.294: INFO/System.out(1000): *** mk dir, cost: 1988ms
11-09 22:17:39.499: INFO/System.out(1000): *** mk dir, cost: 413ms
11-09 22:17:39.577: INFO/System.out(1000): *** mk dir, cost: 49ms
11-09 22:18:41.583: INFO/System.out(1000): *** mk dir, cost: 810ms
11-09 22:18:46.768: INFO/System.out(1000): *** mk dir, cost: 811ms
这次好多了,你可以看到最长时间少于3秒。
我在另一个有1G ram的android pad上测试它,日志是:
11-09 21:58:36.770: INFO/System.out(13647): *** mk dir, cost: 2ms
11-09 21:58:36.949: INFO/System.out(13647): *** mk dir, cost: 3ms
11-09 21:58:37.027: INFO/System.out(13647): *** mk dir, cost: 2ms
11-09 21:58:37.035: INFO/System.out(13647): *** mk dir, cost: 1ms
11-09 21:58:37.035: INFO/System.out(13647): *** mk dir, cost: 1ms
11-09 21:58:38.723: INFO/System.out(13647): *** mk dir, cost: 2ms
11-09 21:58:38.730: INFO/System.out(13647): *** mk dir, cost: 2ms
11-09 21:58:38.754: INFO/System.out(13647): *** mk dir, cost: 1ms
11-09 21:58:38.770: INFO/System.out(13647): *** mk dir, cost: 1ms
11-09 21:58:38.785: INFO/System.out(13647): *** mk dir, cost: 1ms
11-09 21:58:38.855: INFO/System.out(13647): *** mk dir, cost: 5ms
11-09 21:58:38.887: INFO/System.out(13647): *** mk dir, cost: 2ms
11-09 21:58:38.926: INFO/System.out(13647): *** mk dir, cost: 7ms
11-09 21:58:38.934: INFO/System.out(13647): *** mk dir, cost: 1ms
11-09 21:58:38.949: INFO/System.out(13647): *** mk dir, cost: 2ms
11-09 21:58:59.871: INFO/System.out(13647): *** mk dir, cost: 4ms
11-09 21:59:06.176: INFO/System.out(13647): *** mk dir, cost: 7ms
你总能看到它。
我还通过android sdcard-testing应用程序测试了我的SD卡,写入速度为3.x M / s,读取速度为7.x M / s,这足够快。
所以我猜原因是:如果ram不够,有时候IO操作会非常慢,是真的吗?
更新:我只是重写了这个问题,删除了那些不必要的信息。