2

I've read several posts here and here on how cache can be cleared using external applications.

I'd like to run a performance benchmark and would like to clear the operating file system cache before each run. I'm running a program in Java with many parameters, I need an automated method of doing this i.e. programatically in Java.

Any suggestions?

Community
  • 1
  • 1
kami
  • 361
  • 3
  • 15
  • You could use JNA to call [`SetSystemFileCacheSize`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa965240%28v=vs.85%29.aspx)`((SIZE_T) -1, (SIZE_T) -1, 0)`, which flushes the cache. But I don't think that's sufficient because it simply transitions the pages from the working set to the standby list, without actually purging them. – Eryk Sun Mar 10 '15 at 12:51
  • RAMMap uses an undocumented system call to purge the standby list, which I believe requires NT 6. See [this page](http://processhacker.sourceforge.net/doc/ntexapi_8h_source.html) for more info on the declarations required by the following snippet: `SYSTEM_MEMORY_LIST_COMMAND command = MemoryPurgeStandbyList;` `NtSetSystemInformation(SystemMemoryListInformation, &command, sizeof(command));` This requires `"SeProfileSingleProcessPrivilege"` to be enabled, for which an elevated process needs to call `OpenProcessToken`, `LookupPrivilegeValue`, and `AdjustTokenPrivileges`. – Eryk Sun Mar 10 '15 at 12:58
  • I forgot to mention that calling `SetSystemFileCacheSize` to flush the cache requires enabling `"SeIncreaseQuotaPrivilege"`. – Eryk Sun Mar 10 '15 at 13:05

1 Answers1

1

You can use batch file to clear cache and call that batch file in java.

here's a command to call batch file in java: more here

Runtime.getRuntime().exec("cmd /c start clearCache.bat");

this batch file will clear all java cache in windows. just call this batch file in your java code to execute

@Echo Off

echo =========================

echo Now clearing all users Java cache folder

set "docandset=%homedrive%\users"

     >> c:\Delete.log echo/ "Java\Deployment\cache\6.0\"

for /f "delims=" %%a in ('dir "%docandset%" /ad /b') do (

for %%b in (

"%docandset%\%%a\AppData\LocalLow\Sun\Java\Deployment\cache\6.0"

) do (

echo %%b >> c:\Delete.log

cd /d %%b >> c:\Delete.log  2>&1

rd /s /q %%b >> c:\Delete.log  2>&1

del /f /s /q %%b >> c:\Delete.log  2>&1

)

)

echo =========================

echo COMPLETE!!!! 

echo =========================

echo All users Java cache folder has been cleared.

PAUSE

found it here: http://community.spiceworks.com/scripts/show/1928-clear-all-users-java-cache

you can check this also, clear windows temporary files: Batch delete temporary Windows files (system, browsers, cache, etc) for all users

Community
  • 1
  • 1
Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
  • This comment appears to describe how to clear the Java cache from the disk, but I believe the question is asking how to clear the system file cache from memory. – Aaron Campbell Mar 17 '16 at 19:37