Designing your program needs a lot of thought, and big aspect is whether to choose between RAM and CPU. You don't actually have to choose which one you are going to use the most, you need to pick the best balance between those. It is the old question about speed vs efficiency.
I actually experienced why this is so important, because you simple want your RAM to be stable. Sometimes this gets overlooked while testing, as usually small files/queries are used in a testing environment. Once it is deployed on production with actual production data, one either gets
OutOfMemory errors or is using nearly all the RAM.
For myself I use the following questions to keep a stable RAM:
- Do I need the complete file to be read in memory or can I process it line by line/ word by word?
- Could reading this file in memory result in exponential grow? (e.g. when an undetermined amount of files need to be read)
- Do I want to use this data later on?
I once wrote an application which used a 3 mb database and decided for some reason that I had to read it completely for each iteration. In that case I better could have loaded the whole database into RAM and use it over and over again, in stead of reading it over and over again. This resulted in an actual performance gain!
How do you manage to maintain a stable RAM?