Pers.narod.ru. Тексты. Определяем количество свободной памяти на Java2 ME

Получить количество как свободной, так и вообще имеющийся в виртуальной ява-машине памяти, легко из методов стандартного класса Runtime. Ниже приводится законченный мидлет, определяющий эти и ещё кое-какие системные свойства.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
 
public class PropExample extends MIDlet implements CommandListener {
 private Display display;
 private Form propertiesForm;
 private StringBuffer propertyBuffer;
 private Command exitCommand = new Command("Exit", Command.EXIT, 1);
 private boolean firstTime;
 
 public PropExample() {
  display = Display.getDisplay(this);
  firstTime = true;
  propertiesForm = new Form("System Properties");
 }
 
 public void startApp() {
  Runtime runtime = Runtime.getRuntime();
  runtime.gc();
  long free = runtime.freeMemory();
  if (firstTime) {            
   long total = runtime.totalMemory();
   propertyBuffer = new StringBuffer(50);
   propertiesForm.append("Free Memory = " + free + "\n");
   propertiesForm.append("Total Memory = " + total + "\n");
   propertiesForm.append(showProperty("microedition.configuration"));
   propertiesForm.append(showProperty("microedition.profiles"));
   propertiesForm.append(showProperty("microedition.platform"));
   propertiesForm.append(showProperty("microedition.locale"));
   propertiesForm.append(showProperty("microedition.encoding"));
   propertiesForm.addCommand(exitCommand);
   propertiesForm.setCommandListener(this); 
   display.setCurrent(propertiesForm); 
   firstTime = false;
  } 
  else { //Можно вызывать класс неоднократно
   propertiesForm.set(0, new StringItem("", "Free Memory = " + free + "\n"));
  }
  display.setCurrent(propertiesForm);      
 }
 
 public void commandAction(Command c, Displayable s) {
  if (c == exitCommand) {
   destroyApp(false);
   notifyDestroyed();
  }	
 }
 
 String showProperty (String property) {
  String value = System.getProperty(property);
  propertyBuffer.setLength(0);
  propertyBuffer.append(property);
  propertyBuffer.append(" = ");
  if (value == null) {
   propertyBuffer.append("<undefined>");
  } 
  else {
   propertyBuffer.append("\"");
   propertyBuffer.append(value);
   propertyBuffer.append("\"");
  }
  propertyBuffer.append("\n");
  return propertyBuffer.toString();
 }
 
 public void pauseApp() { }
 public void destroyApp(boolean unconditional) { }
}
скриншот мидлета

См. также мой мидлет GetProperties - там можно более гибко вводить и сохранять список проверяемых свойств.

Рейтинг@Mail.ru
вверх гостевая; E-mail
Hosted by uCoz