简体   繁体   English

Windows与Unix运行相同的Java代码时的性能差异

[英]Performance difference when running same Java code Windows vs Unix

I am running below code via eclipse in windows and then in Unix as standalone java. 我正在Windows下通过Eclipse运行以下代码,然后在Unix中作为独立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. 00017741_repository.dat-它是57Mb文件。
In windows obj = in.readObject(); 在Windows中obj = in.readObject(); - it takes me 4-5 seconds. -我需要4-5秒
But in Unix obj = in.readObject(); 但是在Unix中obj = in.readObject(); it takes me 19 - 25 sec! 我花了19到25秒!

I am using VM args -Xmx512m to execute in both cases. 我在两种情况下都使用VM args -Xmx512m执行。

In unix: 在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: 在Windows中:

jre 1.6.0_26

What am I missing here? 我在这里想念什么? Any suggestions to improve the performance in unix? 有什么建议可以改善UNIX的性能吗?

It is not all together that shocking to find differences like this between platforms. 在平台之间发现这样的差异并不令人震惊。 Understand that while the bytecode is the same the JVM is platform specific. 要知道,虽然字节码相同,但JVM是平台特定的。

There are a number of areas like this, where you have code that works perfectly on one platform and fails on another. 有很多类似的领域,在这些领域中,您的代码可以在一个平台上完美运行,而在另一个平台上则失败。 One case I ran into was in drag n drop with files, where unix (at least ubuntu) drops files with a different dataflavor than windows. 我遇到的一种情况是文件拖拽,unix(至少是ubuntu)拖拽数据风格与Windows不同的文件。

You must test on all platforms you intend for your code to run in 您必须在要运行代码的所有平台上进行测试

Also, in your specific case, you should either try to read from your inputstream more efficiently (using a byte[]) or create a BufferedInputStream to wrap your FileInputStream in 另外,在您的特定情况下,您应该尝试更有效地从输入流中读取(使用byte []),或者创建BufferedInputStream来将FileInputStream包装在其中

Here are two specific things you can do to figure this one out: 您可以执行以下两项具体操作来解决这一问题:

  1. make sure that on Unix the file resides on a local file system and not on a network mount; 确保在Unix上,文件位于本地文件系统上,而不位于网络安装上;
  2. use a profiler (such as YourKit ) to find out where the time is spent. 使用探查器(例如YourKit )来找出时间在哪里。

You also have to differ between different JVMs used. 您还必须在所使用的不同JVM之间有所不同。 For example on *nix systems there are vms like "icedtea" or "OpenJDK" widely used(because they are the default installation). 例如,在* nix系统上,有诸如icedtea或OpenJDK之类的vm被广泛使用(因为它们是默认安装)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM