I am running below code via eclipse in windows and then in Unix as standalone java.
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.sql.SQLException;
import java.text.ParseException;
public class readObjectBoeing {
/**
* @param args
* @throws ParseException
* @throws SQLException
*/
public static void main(String[] args) {
//File file = new File("/opt/app/d1ebp1m1/dv01/Vibhor/test/00017741_repository.dat");
File file = new File("C:/_Vibhor/00017741_repository.dat");
InputStream is;
try {
is = new FileInputStream(file);
byte[] b = toByteArray(is);//read from file;
Object o1 =null;
o1 = convertByteArrayToObject(b);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Object convertByteArrayToObject(byte[] buf) throws Exception
{
if (buf.length == 0)
{
return null;
}
long startTime = -1;
long step1=-1,step2=-1;
Object obj = null;
ByteArrayInputStream bis = null;
ObjectInputStream in = null;
try
{
bis = new ByteArrayInputStream(buf);
in = new ObjectInputStream(bis);
startTime = System.currentTimeMillis()/1000;
obj = in.readObject();
step1 = System.currentTimeMillis()/1000 - startTime ;
System.out.println("in.readObject()!! : " + step1);
}
catch (Exception e)
{
throw e;
}
finally
{
if (in != null)
{
in.close();
}
if (bis != null)
{
bis.close();
}
in = null;
bis = null;
}
return obj;
}
public static byte[] toByteArray(InputStream input) throws IOException
{
ByteArrayOutputStream output = new ByteArrayOutputStream();
long count = 0L;
byte[] buffer =new byte[4096];
for(int n = 0; -1 != (n = input.read(buffer));){
output.write(buffer, 0, n);
count += n;
}
return output.toByteArray();
}
}
00017741_repository.dat – it is 57Mb file.
In windows obj = in.readObject();– it takes me 4-5 seconds.
But in Unix obj = in.readObject(); it takes me 19 – 25 sec!
I am using VM args -Xmx512m to execute in both cases.
In unix:
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) Server VM (build 20.4-b02, mixed mode)
In Windows:
jre 1.6.0_26
What am I missing here? Any suggestions to improve the performance in unix?
Here are two specific things you can do to figure this one out: