1

我的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;
}

我在我的应用程序中多次调用它来在 sdcard 上创建目录,路径基于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 秒。

我想可能是内存造成的,可能不够: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应用程序测试了我的sdcard,写入速度为3.x M/s,读取速度为7.x M/s,已经足够快了。

所以我猜原因是:如果内存不够,IO操作有时会很慢,是真的吗?


更新:我只是重写了这个问题,删除了那些不必要的信息。

4

0 回答 0