Reputation: 189
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");
Upvotes: 1
Views: 352
Reputation: 159086
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.
Upvotes: 3