I have a general question about desktop applications. My friend and I were discussing about how application data should be managed while the application is running. We were arguing over two methods and couldn't come to a conclusion. Those two methods were:
- Read data when it is needed, write data to the disk as soon as it gets changed.
- Load all application data into memory when the application starts, modify it in memory, and finally write onto the disk when the application closes.
As far as I understand, the main arguments are:
- #2 is faster because all data is in memory and can be accessed quickly.
- #2 is better because it uses minimal file IO, which is, in my knowledge, considered to be a slow and expensive operation.
- #1 is better because all data is synchronized at all times with the disk, so there is limited chance of data loss on abnormal program termination.
- #1 is better since the application then does not take up much memory.
EDIT: The specific application we were discussing was a Course Management System which involved 4 main aspects: an administrator, teachers, courses and students. Data for all the things above was stored in files on disk. Basically the application was largely data-driven.
Keep the data in memory where possible / desired (if your application works with only part of the data, then loading the unused parts would be waste of RAM), and write/flush the changes a soon as they are changed (or asynchronously in background thread after some delay of inactivity).
If the platform you are working with supports memory-mapped files, look at them. MMFs let you combine both approaches quite efficiently.