3

I am trying to test a Java program that I wrote, in order to prove its efficiency. I have many different tests to run, and I am trying to be precise in the runtime analysis. The problem is that different tests may access information on the disk that is common. So, in order to be "fair" in my experimental results I would like to somehow programmatically clear the internal memory being used by my Java program, in between experiments. In other words, I want each experiment to have the same "empty memory/cache".

I tried reading in a large file in between experiments. I also tried restarting my machine. Interestingly, the times are much much worse when I read in a large file, then when I simply restart the machine (say 40 sec to 5 sec). What is the correct way to clear internal memory (i.e., avoid the artificial speedup for experiments from common disk accesses) beyond restarting my machine between each experiment, which is not feasible?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    But why do you think an empty disk cache is a realistic scenario? You'd just be testing the very rare worst case. – Marko Topolnik Nov 04 '12 at 09:35
  • Thanks. I agree that I am testing the worst case, and that in practice, things will be better. But, I would still like to show that even in that worst case scenario, my program works well. (I dont want the referees of the paper I am writing about my system to have an excuse to complain about the cache issue.) – user1797753 Nov 04 '12 at 09:40
  • There's no Java API for this, but there might possibly be a OS specific way to do it. So which operating system are you using ? – nos Nov 04 '12 at 09:56
  • See http://stackoverflow.com/questions/478340/clear-file-cache-to-repeat-performance-testing for a few options. – nos Nov 05 '12 at 15:00
  • This is hard. Modern operating systems cache files and directory pages that have been recently used, and from what I've read controlling this in Windows is difficult at best. And even if the OS doesn't cache a file, the disk drive may. – Hot Licks Nov 08 '13 at 17:09
  • It should be noted that some Java implementations *do* cache files in the sense that if a class in loaded in one Java process the file image may be "shared" with other Java processes that come along later. But I don't think any standard Windows JVM does this -- it's mainly for big server setups. – Hot Licks Nov 08 '13 at 17:13

1 Answers1

0

What is the correct way to clear internal memory

Restart the computer. As you say this might not be the slowest option.

Computers are design to be efficient as possible, making them deliberately inefficient means making some assumption about how inefficient you want to make it, so there is no standard way of doing this.

One way of clearing the cache for files you have written is to delete them. If you want to clear the cache for file in disk you want to keep, you can write a large set of files, larger than your main memory and delete them.

If you are using Windows it doesn't use all the memory to cache files in any case. What you can do is to write large files of a few hundred MB at a time and time how long this takes. When the cache is exhausted you see a sudden jump in the time it takes from Java and you know at this point you have probably clearer the cache. After this happens, delete them and your cache is likely to be empty as a result.

This can help you get reproducible results but this may not be exactly the same as rebooting. Note: unless you expect to reboot your computer every time you run your application, the time it takes after a reboot may not be meaningful anyway.

BTW: The disk cache you should be worrying about the in the OS. Java doesn't cache files except in buffers you create and in the code you write so these should be under your control.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130