Record & Play windows system process memory consumption

Posted: Friday, August 19, 2011 by Venkat Pola in Labels:
2

I don't find any tool which can be used for recording windows system process memory and used for later review.

There are some tools like process explorer but these are live it won't allow us to record the memory graph. So i decided to write java client which will poll periodically using DOS command WMIC and get the private bytes of a system process.


Usage :-

Paste the below code and put in MemoryMonitor.java

javac MemoryMonitor.java
java MemoryMonitor process_name poll_interval test_duration file

java MemoryMonitor firefox.exe 5 60 d:\\test.csv

So ideally, test.csv contains private bytes data of all intervals later you can use this file for drawing graphs.


Cheers!!!!!!!!!!

Here is the program code:-


import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


//java MemoryMonitor firefox.exe 5 60 d:\\test.csv

public class MemoryMonitor {

public String PROCESS_NAME = "eclipse.exe";
public int TIME_INTERVAL = 5; // in seconds
public int TOTAL_TEST_TIME = 60; // in seconds
public String DESTINATION_FLE = "d:\\memory_usage.csv";

Toolkit toolkit;

Timer timer;
Timer timer1;
List memoryList;

public MemoryMonitor(String args[]){
PROCESS_NAME = args[0] ; TIME_INTERVAL = Integer.parseInt(args[1]) ; TOTAL_TEST_TIME = Integer.parseInt(args[2]) ; DESTINATION_FLE = args[3];
memoryList = new ArrayList();
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer1 = new Timer();
timer1.schedule(new ExitTimerTask(), TOTAL_TEST_TIME * 1000);
scheduleTimer();
}

public void scheduleTimer(){
getMemoryDetails();
timer.schedule(new MemoryDetailsTask(), TIME_INTERVAL * 1000);
}

public void getMemoryDetails(){
try {
Process proc = Runtime.getRuntime().exec("WMIC PROCESS where Caption=\""+PROCESS_NAME+"\" get PageFileUsage");
InputStream procOutput = proc.getInputStream();
String value = readInputStreamAsString(procOutput);
String array[] = value.split("\n");
double privateBytesValue = 0;
for(int i = 1 ; i < array.length ; i++){
if(array[i].trim().length()>0){
String privateBytes = array[i].substring(0, array[i].indexOf(" "));
privateBytesValue+= Double.parseDouble(privateBytes) / 1024;
}
}
System.out.println(privateBytesValue);
memoryList.add(privateBytesValue);
} catch (Exception e) {
System.out.println("Error occured");
e.printStackTrace();
System.exit(0);
}
}

public String join(List list, String conjunction)
{
StringBuilder sb = new StringBuilder();
boolean first = true;
for (Object item : list)
{
if (first)
first = false;
else
sb.append(conjunction);
sb.append(item);
}
return sb.toString();
}

public String readInputStreamAsString(InputStream in) throws IOException {

BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while (result != -1) {
byte b = (byte)result;
buf.write(b);
result = bis.read();
}
return buf.toString();
}

class MemoryDetailsTask extends TimerTask {
public void run() {
getMemoryDetails();
scheduleTimer();
}
}

class ExitTimerTask extends TimerTask {
public void run() {
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(DESTINATION_FLE),"UTF8"));
out.write(join(memoryList,","));
out.close();
System.out.println("Please find this file for the report "+DESTINATION_FLE);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Not able to write to the file "+DESTINATION_FLE);
e.printStackTrace();
};
System.exit(0);
}
}

public static void main(String args[]) {
new MemoryMonitor(args);
}

}

2 comments:

  1. HTML5 development is a growing field these days because of the huge variety of hybrid and native applications that can be created using it.
    HTML5 Development company

  1. HTML5 Developers incorporate great features in HTML5 application development giving quick responsiveness.