I have an Android Service which updates a notification every second using this Thread (comments are not really relevant):
thread = new Thread() {
@Overri
In finally block you can do the following thing
finally {
try {
freeOut.close();
} catch (IOException e) {
e.printStackTrace();
}
how can I lower memory usage in a more eficient way?
The big thing would be to get rid of all of this process-forking nonsense and replace it with a simple call to getMemoryInfo(). Or, use JNI to call some C APIs to get your memory information. Forking a process every second is exceedingly wasteful on a mobile device. Besides, you have no way of knowing if free
exists on every device, as on-device command-line binaries are not part of the Android SDK and are not guaranteed to be consistent.
Secondarily, use StringBuilder
or String.format()
rather than string concatenation to build up your Notification
text.