Will increasing the RAM be helpful?
Hard to say without knowing why your tests succeed after running those two commands (
drop_caches). TBH I don't think that dropping the filesystem cache is what's enabling your tests to work unless your tests are failing due to latency issues. When the kernel notices things dip below
min_free_kbytes it starts evicting the oldest entries from the cache to get actually-free memory back to at least
min_free_kbytes. The only exception to that process is for pages marked as "dirty". So the existence of a filesystem cache usually isn't an issue unless your applications are allocating memory faster than the kernel can locate entries to prune (in which case bumping up
min_free_kbytes is in order).
The command that's probably doing the most for you is the
sync command which is probably freeing memory dedicated to the buffers used for asyncronous disk I/O (as well removing
dirty from all entries in the filesystem cache, making them candidates for eviction in low memory conditions).
I put the odds at about 80-90% that adding more RAM would be helpful, but I don't know what tests you're running so I can't say for sure.
Is it ok to clean up cache periodically? Will it cause other issues?
What you're doing here is basically performance tuning (or at least you're playing with tunables made available for that purpose). Tunables are usually made available when knowing the "right" answer in general is considered impossible/impractical. If it were possible to create general rules about how things ought to run, they would just make the software do that thing and would leave the sysadmin out of the question.
All that to say that we can't really say with absolute certainty that it won't cause problems for you. We can say it probably won't cause loss of functionality but we can't be certain. I would say that the filesystem cache is designed to improve performance so forcing applications to waiting on data to come in from disk whenever they read anything probably isn't desirable (though it could sometimes be undesirable but necessary).
Just read @Emmanuel's comment. He brings up a good point. If you have unused swap space, you might try increasing
vm.swappiness to increase the kernel's preference to swap out the less-used application memory. If you need more swap space (or don't have any) you can find instructions online for creating swap partitions and swap files.
I haven't played with this one, but looking at the kernel docs, there's a
dirty_ratio sysctl tunable, that controls how much dirty data will be held in memory before the kernel does its own
sync, you may try lowering that so that as few pages in the filesystem cache are marked "dirty" and thus can be evicted when the memory is needed.