Récupérer informations systèmes
Bonjour,
Je souhaite savoir comment faire pour récupérer des informations systèmes dont une précise : les rams dispo.
J'ai trouvé de quoi récupérer le nom de l'os, sa version, etc, mais pas la mémoire vive restante.
Est-ce que quelqu'un sait comment faire pour récupérer cette information.
Merci d'avance.
NB : j'ai vu que GlobalMemoryStatus est censé le faire, mais je dois faire une appli multi plateforme, et cette fonction est pure windows.
Pareil pour ce bout de code qui est pure linux :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
import java.io.*;
public class test{
public static void main(String [] args){
String r="ERREUR";
try {
LineNumberReader lnr=new LineNumberReader
(new FileReader("/proc/meminfo"));
long total=0,free=0;
while((total==0)||(free==0)) {
String s=lnr.readLine();
if(s==null)
break;
else if(s.startsWith("MemTotal:")) {
s=s.substring(10,s.length()-2).trim();
total=Long.valueOf(s).longValue();
}
else if(s.startsWith("MemFree:")) {
s=s.substring(10,s.length()-2).trim();
free=Long.valueOf(s).longValue();
}
}
lnr.close();
r=(100l*(total-free)/total)+"%";
System.out.println("Free Memory:"+free+" Ko");
System.out.println("Total Memory:"+total+" Ko");
System.out.println(r+" of Memory is used");
} catch(Exception ex) {System.out.println("An error occured when reading Memory statut "+ex);}
//***************************************************************************
try {
LineNumberReader lnr=new LineNumberReader
(new FileReader("/proc/cpuinfo"));
long cpu=0;
while(cpu==0) {
String s=lnr.readLine();
if(s==null)
break;
else if(s.startsWith("cpu MHz")) {
//System.out.println(s+"\n");
int pos=s.indexOf(':');
s=s.substring(pos+1);
//System.out.println("["+s+"]\n");
// s=s.substring(10,s.length()-2).trim();
double cpu_speed=Double.valueOf(s).longValue();
System.out.println("CPU speed: "+cpu_speed);
// System.out.println("["+cpu_speed+"].......NOT CPU....\n");
}
}
lnr.close();
} catch(Exception ex) {System.out.println("An error occured when reading Cpu statut "+ex);}
//***************************************************************************
}
} |
Précision sur la solution
La technique précédente ne fait que récupérer la taille totale de la RAM et la taille de la swap. Mais l'objet OperatingSystemMXBean possède d'autre méthodes, en voici la liste :
( Extrait de l'URL : http://java.sun.com/javase/6/docs/jr...temMXBean.html )
long getCommittedVirtualMemorySize() : Returns the amount of virtual memory that is guaranteed to be available to the running process in bytes, or -1 if this operation is not supported.
long getFreePhysicalMemorySize() : Returns the amount of free physical memory in bytes.
long getFreeSwapSpaceSize() : Returns the amount of free swap space in bytes.
long getProcessCpuTime() : Returns the CPU time used by the process on which the Java virtual machine is running in nanoseconds.
long getTotalPhysicalMemorySize() : Returns the total amount of physical memory in bytes.
long getTotalSwapSpaceSize() : Returns the total amount of swap space in bytes.