-2

in the linux os, when we send this command:

 free -m

this result has been appear:

               total       used       free     shared    buffers     cached
Mem:         32182      31902        280          0        658      16802
-/+ buffers/cache:      14442      17740
Swap:        65538       7463      58075

and when we send a command to clear cache,the cache has been decreased ,i want to do the same but using java,can i do this? thank you...

mimo
  • 97
  • 1
  • 2
  • 6
  • You can try System.gc(); and System.runFinalization(); – Dariusz Mazur Sep 11 '13 at 08:28
  • 2
    possible duplicate of [How to run linux commands in java code?](http://stackoverflow.com/questions/3403226/how-to-run-linux-commands-in-java-code) – Rong Nguyen Sep 11 '13 at 08:29
  • and how can test if it works?is there a command line for windows 7? – mimo Sep 11 '13 at 08:29
  • lol. Of Course there is a command line in Windows, but linux commands will not work on windows. So, if you do a Runtime.exec, you need to implemnt CrossOS behaviour by yourself. – mondjunge Sep 11 '13 at 08:34
  • 1
    What "command to clear cache" are you talking about? – chrylis -cautiouslyoptimistic- Sep 11 '13 at 08:39
  • i want to write a program which work on linux and windows.i know that i can use the Runtime class to send a command but i work on only one operating system.so i want to write a java code wich do this.and i want to test this code if it work on windows so i want to a windows command to test it. – mimo Sep 11 '13 at 08:50
  • 1
    and what cache are you actually talking about? the one reported by free, is completely unrelated to java. its the amount of memory used by linux for disk caching. – JustDanyul Sep 11 '13 at 08:51

2 Answers2

4

You need to use the Runtime.exec method.

Basic example:

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
mondjunge
  • 1,219
  • 14
  • 24
0

Have you tried

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

But based upon some of the answers here, I am guess that there is confusion as to exactly what you are wanting to achieve.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64