Eclipse下内存溢出错误(OutOfMemoryError)
写了一个图片缩放程序,当图片尺寸过大时会报错:
Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space
解决方法:
在Eclipse里选:Window->Preference->Installed JREs->Edit(选中jre),
在Default VM Arguments里输入-Xms256m -Xmx1024m,表示最小内存256M,最大1G,然后运行就可以了。
程序源码ImageBrowser.java :
import java.awt.Container; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JScrollPane; public class ImageBrowser extends JFrame { private static final long serialVersionUID = 1L; JLabel lab = new JLabel(); JFileChooser chooser = new JFileChooser(); private String filePath; private int height; private int width; private final double ratio = 0.2; public ImageBrowser() { initial(); setTitle("Image Browser --- No image"); setSize(600, 400); setLocation(200, 150); setVisible(true); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public void initial() { // Create Menu JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu1 = new JMenu("File"); JMenu menu2 = new JMenu("Edit"); menuBar.add(menu1); menuBar.add(menu2); JMenuItem openItem = new JMenuItem("Open"); menu1.add(openItem); openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int r = chooser.showOpenDialog(null); if(r == JFileChooser.APPROVE_OPTION) { filePath = chooser.getSelectedFile().getPath(); setTitle("Image Browser --- " + filePath); spread(); } } }); JMenuItem spreadItem = new JMenuItem("Spread"); menu2.add(spreadItem); spreadItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { spread(); } }); JMenuItem resumeItem = new JMenuItem("Resume"); menu2.add(resumeItem); resumeItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { resume(); } }); JMenuItem largeItem = new JMenuItem("Large"); menu2.add(largeItem); largeItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { height = (int)(height*(1+ratio)); width = (int) (width*(1+ratio)); lab.setIcon(getFixedBoundIcon()); } }); JMenuItem reduceItem = new JMenuItem("Reduce"); menu2.add(reduceItem); reduceItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { height = (int)(height*(1-ratio)); width = (int) (width*(1-ratio)); lab.setIcon(getFixedBoundIcon()); } }); JMenuItem exitItem = new JMenuItem("Exit"); menu1.add(exitItem); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.exit(0); } }); Container contentPane = getContentPane(); JScrollPane sc = new JScrollPane(lab); contentPane.add(sc); } public void spread() { File file = new File(filePath); Icon ic = new ImageIcon(file.getAbsoluteFile().toString()); int icHeight = ic.getIconHeight(); int icWidth = ic.getIconWidth(); Container contentPane = getContentPane(); height = contentPane.getHeight(); width = contentPane.getWidth(); if((double)height/width>=(double)icHeight/icWidth) height = (int)(width*(double)icHeight/icWidth); else width = (int)(height*(double)icWidth/icHeight); lab.setIcon(getFixedBoundIcon()); } public void resume() { try { Icon ic = new ImageIcon(filePath); height = ic.getIconHeight(); width = ic.getIconWidth(); lab.setIcon(ic); } catch(Exception e) { e.printStackTrace(); } } public Icon getFixedBoundIcon(){ double Ratio=0.0; //缩放比例 try { File F = new File(filePath); Icon ret = new ImageIcon(filePath); BufferedImage Bi = ImageIO.read(F); if(1==1) { // ((Bi.getHeight()>height) || (Bi.getWidth()>width)) { if (Bi.getHeight()>Bi.getWidth()){ Ratio = (new Integer(height)).doubleValue() /Bi.getHeight(); } else { Ratio = (new Integer(width)).doubleValue()/Bi.getWidth(); } File ThF = new File(filePath+"_"+height +"_"+width); Image Itemp = Bi.getScaledInstance (width,height,Image.SCALE_SMOOTH); AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null); Itemp = op.filter(Bi, null); ImageIO.write((BufferedImage)Itemp, "jpg", ThF); ret = new ImageIcon(ThF.getPath()); } return ret; } catch (Exception ex) { ex.printStackTrace(); } return null; } public static void main(String[] args) { new ImageBrowser(); } }