简体   繁体   中英

First RandomAccessFile initialization slow, subsequently fast

I've noticed that the first initialization of a RandomAccessFile object in Java is much slower than subsequent initializations of RandomAccessFile objects to the same file on disk. Is there some background caching that the OS does to make this possible?

Case in point: I'm loading images from disk and allowing the user to flip through them. I was hoping that the bottleneck would be the display of the images, but on first load, the bottleneck was loading of the images (bottleneck was found using JProfiler; RandomAccessFile<Init> ~8ms per call). If I flipped back through images I'd already viewed, the calls to RandomAccessFile<Init> was only several microseconds.

Has anyone ever seen something like this? Are there any workarounds? A dataset may contain 100,000's of images, so initializing a bunch of dummy RandomAcessFile objects may not be feasible.

The line of code for initialization is simply:

RandomAccessFile fileIn = new RandomAccessFile(abspath, "r");

Yes, the OS caches.

If you bypass the OS caching, the subsequent opens of a file will be as slow as the first one, so why would you want that?

Caching is not slowing the first time you open a file, it's improving the performance of re-opening the file, by not having to wait for the slow harddrive to read the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM