Does FileOpen load whole file into RAM? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
serg
Posts: 56
Joined: 21 Mar 2015, 05:33

Does FileOpen load whole file into RAM?

15 Jul 2021, 06:36

Hi folks,

I have ~100 files of data weighting about 500MB total, I need to access each of them about dozen times during a day. So I'm wondering, is it worth loading them all (0.5GB) into RAM in the morning, so they can be quickly accessible for the rest of the day. If FileOpen loads whole file into RAM each time, then it is. So that is my question: does FileOpen load whole file into RAM?

I looked thru similar posts on this forum, but didn't find clear answer. If someone could answer it, I would greatly appreciate it!
Thanks!
User avatar
mikeyww
Posts: 27196
Joined: 09 Sep 2014, 18:38

Re: Does FileOpen load whole file into RAM?

15 Jul 2021, 07:25

Although I do not have a specific answer to your question, a practical approach to your question about speed is to time the two different versions of your scripts, to see which is faster. If you have a SSD, the outcome might depend less on opening the file and more on what you are doing with the file after that.
User avatar
boiler
Posts: 17215
Joined: 21 Dec 2014, 02:44

Re: Does FileOpen load whole file into RAM?  Topic is solved

15 Jul 2021, 07:32

FileOpen() itself does not read the file contents. It opens a file for reading and/or writing. There isn’t even a variable identified with a FileOpen() call to read the file contents into. The return value is a file object, which provides the interface for subsequent operations, such as .Read() or .Write().

FileRead reads a file’s contents into memory (into a variable). If you want to work with the contents repeatedly directly in RAM, that might be the approach you want to take. The following are essentially equivalent:

Code: Select all

FileRead, FileText, MyFile.txt

Code: Select all

MyFile := FileOpen("MyFile.txt", "r")
FileText := MyFile.Read()
MyFile.Close()
serg
Posts: 56
Joined: 21 Mar 2015, 05:33

Re: Does FileOpen load whole file into RAM?

15 Jul 2021, 08:35

@mikeyww , @boiler Thank you for answers!

@boiler, so, does File.Read read only needed part of the file from disk, not the whole file?

@mikeyww, my concern is more about the toll it takes on disk (reading large amount of data from disk many times over), and less about speed of the process.
User avatar
boiler
Posts: 17215
Joined: 21 Dec 2014, 02:44

Re: Does FileOpen load whole file into RAM?

15 Jul 2021, 08:50

serg wrote: @boiler, so, does File.Read read only needed part of the file from disk, not the whole file?
It can read part of the file from the disk. You can tell it how much of the file to read, the unit of length depending on the method used. The file pointer would need to be at or moved to the desired location. See File.Read() as well as the other read-related methods on that page for more details.
serg
Posts: 56
Joined: 21 Mar 2015, 05:33

Re: Does FileOpen load whole file into RAM?

15 Jul 2021, 13:00

boiler wrote:
15 Jul 2021, 08:50
You can tell it how much of the file to read
Yes, that's what my script does, it reads only specific part of large file, finding needed location by "table of contents" stored in other file.
I just was not sure whether File.Open/Read reads the whole file into RAM and then lets user to read any part of it, OR it just gets handle and then reads only requested part of it while the rest of file remains unloaded into RAM. As I understand now, not-requested part of file remains unloaded into RAM, correct?
Thank you for taking time to answer, your help is very appreciated!
User avatar
boiler
Posts: 17215
Joined: 21 Dec 2014, 02:44

Re: Does FileOpen load whole file into RAM?

15 Jul 2021, 13:33

Yes, it only reads the part you tell it to read. If you use File.Read(100), it will only read 100 characters into memory, not the rest of the file.
serg
Posts: 56
Joined: 21 Mar 2015, 05:33

Re: Does FileOpen load whole file into RAM?

15 Jul 2021, 14:07

@boiler , thanks again! :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 118 guests