做的是一个生成BufferedImage的小程序,其中进行Image编码的代码如下:
InputStream in = new FileInputStream(file);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
this.mBufferedImage = decoder.decodeAsBufferedImage();
in.close();上述代码是在一个循环中使用的,第一次时,没有问题,可第二次时,运行到this.mBufferedImage = decoder.decodeAsBufferedImage();会出一个java.lang.OutOfMemoryError: Java heap space异常,一开始以为是自己的虚拟机内存设置的额太小了,更改了之后,还是出一样的问题,请教各位大虾。。

解决方案 »

  1.   

    Out of Memory 也没什么好说的。
    1:扩大你 JVM 内存
    2:即时释放你程序中占用的内存。
    你读的图片有多大?为 JVM 设置的内存有多大?
    你把 this.mBufferedImage = decoder.decodeAsBufferedImage(); 放到循环中了?那你在循环体的开头加上 this.mBufferedImage = null; 看是否有效果。
      

  2.   

    没找到问题,呵呵,试试另一种方法生成生成BufferedImage
    File image= new File("d:/a.jpeg");
    BufferedImage imgbuffer = ImageIO.read(image);
      

  3.   

    我在程序初始化时,已经把this.mBufferedImage设置为null了,并且把jvm的内存调整到了512M,单个图片的大小也只有1.5M左右,还是出这个问题啊,纠结。
      

  4.   

    3楼的做法不是我需要的,因为要获得的是一个扫描仪扫描的图像,最终就是要保存成JPG文件。
      

  5.   

    可是你在循环中又给他赋值了,所以在循环开始的时候要重新把它设置为 null 。你先看看有没有作用再说吧。如果单个文件只有 1.5 那么,你的内存问题不仅仅在循环体中,这里可能只是一个触发点。程序中的其它地方肯定也有“没有及时释放”的问题。
      

  6.   


    /**
     * Acquire the actual image through the JTwain interface
     */
    protected void acquireImage() {
    String localPath = null;
    String _filename = null;
    if (this.mRecordId == null) {
    JOptionPane.showMessageDialog(this, "请输入档案号!");
    return;
    }
    else {
    localPath = this.mfloderPath + "/" + mRecordId;
    } if (JTwain.getInstance().isTwainAvailble()) {
    if (mSourcesCombo.getItemCount() > 0) {
    String _source = (String) mSourcesCombo.getSelectedItem();
    if (_source != null) {
    JTwain myTwain = JTwain.getInstance(); myTwain.setResolution(100.0);
    // myTwain.setUIEnable(true); if (ftpUtil.connectServer(ipAdress, username, password)) {

    do {
    this.mBufferedImage = null;
    _filename = null;

    _filename =  myTwain.acquire(_source); if (_filename != null && _filename.length() > 0) {
    File file = new File(_filename);

    openFile(file);
    }
    else {
    JOptionPane.showMessageDialog(this, "扫描失败");
    }
    }
    while (nCount < 3); try {
    ftpUtil.upLoadDirectory(localPath, "");
    } catch (IOException e) {
    e.printStackTrace();
    }
    ftpUtil.closeConnection();
    }
    else {
    System.out.println("connect server failed!");
    }
    }
    }
    }
    } /**
     * Used to open the file and then display in the JPEG Panel
     * 
     * @param file
     *            The file object to display
     */
    protected void openFile(final File file) {
    if (file == null || !file.exists()) {
    return;
    }
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); try {
    InputStream in = new FileInputStream(file);
    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
    this.mBufferedImage = decoder.decodeAsBufferedImage();
    in.close(); saveImage(this.mBufferedImage, nCount); nCount++; if (this.mBufferedImage != null) {
    mJpegPanel.setBufferedImage(this.mBufferedImage);
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    } protected void saveImage(BufferedImage bi, int nCount) {
    String filePath = mfloderPath + "/" + mRecordId;
    filePath = filePath.toString();
    File myFilePath = new File(filePath); try {
    if (!myFilePath.isDirectory()) {
    myFilePath.mkdir();
    }
    } catch (Exception e) {
    e.printStackTrace();
    } /* 存储文件 */
    if (bi != null) {
    String formattName = "JPEG";
    File file = new File(mfloderPath + "/" + mRecordId + "/" + nCount + ".jpeg");
    try {
    ImageIO.write(bi, formattName, file);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    else {
    return;
    }
    }
    这是从获取图像到保存的代码,各位大虾帮小弟看下,弄了很久,还是那个异常