使用java代码清除缓存内存

使用java代码清除缓存内存,java,Java,在linux操作系统中,当我们发送此命令时: free -m 此结果已显示: total used free shared buffers cached Mem: 32182 31902 280 0 658 16802 -/+ buffers/cache: 14442 17740 Swap: 65538

在linux操作系统中,当我们发送此命令时:

 free -m
此结果已显示:

               total       used       free     shared    buffers     cached
Mem:         32182      31902        280          0        658      16802
-/+ buffers/cache:      14442      17740
Swap:        65538       7463      58075
当我们发送一个清除缓存的命令时,缓存已经被减少了,我想做同样的事情,但是使用java,我可以这样做吗? 谢谢你……

你试过了吗

final Runtime rt = Runtime.getRuntime();
rt.exec("free -m");

但是,根据这里的一些答案,我想对于您想要实现的目标,可能存在一些混淆。

您需要使用Runtime.exec方法

基本示例:

Runtime run = Runtime.getRuntime(); // get OS Runtime 
Process pr = run.exec("free -m"); // execute a system command and give back the process
pr.waitFor(); // wait for the process to complete

您可以尝试System.gc();和System.runFinalization();可能的重复以及如何测试它是否工作?是否有适用于windows 7的命令行?您所说的“清除缓存的命令”是什么?以及您实际所说的缓存是什么?free报告的一个与java完全无关。它是linux用于磁盘缓存的内存量。