写了一个图片缩放程序,当图片尺寸过大时会报错:

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();
}
}

Eclipse下内存溢出错误(OutOfMemoryError)的更多相关文章

  1. eclipse启动tomcat出现内存溢出错误 java.lang.OutOfMemoryError: PermGen space

    发布工程后,启动tomcat出现如下内存溢出错误: java.lang.OutOfMemoryError: PermGen space ... java.lang.OutOfMemoryError: ...

  2. 一次apk打开时报内存溢出错误,故写下内存溢出的各种原因和解决方法

    原转载:https://blog.csdn.net/cp_panda_5/article/details/79613870 正文内容: 对于JVM的内存写过的文章已经有点多了,而且有点烂了,不过说那么 ...

  3. java中三种常见内存溢出错误的处理方法

    更多 10   相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的 ...

  4. java中三种常见内存溢出错误的处理方法(good)

    相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的认识. 在解决j ...

  5. Xamarin Android提示内存溢出错误

    Xamarin Android提示内存溢出错误 错误信息:java.lang.OutOfMemoryError, Consider increasing the value of $(JavaMaxi ...

  6. eclipse 出现内存溢出问题解决办法

    1.eclipse.ini添加设置: -vm#eclipse启动使用的jdk设置,路径根据自己实际路径修改 C:/Program Files/Java/jdk1.6.0_45/bin/javaw.ex ...

  7. Eclipse中运行Tomcat遇到的内存溢出错误

    使用Eclipse(版本Indigo 3.7)调试Java项目的时候,遇到了下面的错误: Exception in thread "main" Java.lang.OutOfMem ...

  8. Java抛出OutOfMemoryError:Java heap space堆内存溢出错误的分析方案

    抛出堆内存溢出的错误一定要记得保留现场环境(导出堆内存信息到文件),否则如果无法进行分析,并从根本上解决问题,下次很有可能还会出现. 第一步:导出堆转储文件 我们可以使用Jdk自带的jmap工具.使用 ...

  9. eclipse启动Tomcat加载项目时报内存溢出错误解决办法

    在eclipse中点击Window->Preferences打开全局属性设置对话框,如下图所示设置Tomcat运行时的JVM参数,添加这段JVM设置:-Xms256M -Xmx768M -XX: ...

随机推荐

  1. Smarty标签运算,控制结构[if,for,foreach,section,while]

    Smarty标签运算: 在页面上做简单的运算[temp5.html] 条件判断if 循环结构 for foreach用得比较多,foreach例子从数据库取出的数据 section功能和foreach ...

  2. Markdown 使用教程

    前言 以前经常在 github 中看到 .md 格式的文件,一直没有注意,也不明白为什么文本文档的后缀不是 .txt ,后来无意中看到了 Markdown,看到了用这个东西写得一些web界面等特别的规 ...

  3. tuple与list

    tuple是一个引用之后就不可以修改的类型,是一个immutable类型 list是一个mutable的类型,引用之后是可以修改的.同时可以通过索引来修改list中各个元素.这一点是tuple做不到的 ...

  4. Vivado抓取信号

    作者:桂. 时间:2018-05-03  21:16:03 链接:www.cnblogs.com/xingshansi/p/8987608.html 前言 FPGA调试需要抓取特定信号,一个直观的思路 ...

  5. 使用Method swizzling (也就是运行时交换两个方法的imp ,实现重写方法)

    贴上资源.很简单 https://gist.github.com/rudyjahchan/2191796 http://itony.me/592.html http://stackoverflow.c ...

  6. OpenUDID 实现UDID替代

    http://www.cnblogs.com/zhulin/archive/2012/03/26/2417860.html 补充:还有两个比较通用的开源解决方案: OpenUDID:https://g ...

  7. hdu-2045 递归

    #include <cstdio> #include <iostream> using namespace std; long long a[55] = {0,3,6}; lo ...

  8. 给Java开发人员的Play Framework(2.4)介绍 Part1:Play的优缺点以及适用场景

    1. 关于这篇系列 这篇系列不是Play框架的Hello World,由于这样的文章网上已经有非常多. 这篇系列会首先结合实际代码介绍Play的特点以及适用场景.然后会有几篇文章介绍Play与Spri ...

  9. Shader中ColorMask的使用

    ColorMask可以对输出颜色进行Mask处理 使用方法和Cull这些标记差不多 SubShader { ColorMask R Cull Off .... 如果ColorMask填0就什么都不显示

  10. Android 录音获取分贝值的办法

    参考:http://blog.csdn.net/greatpresident/article/details/38402147 public class MediaRecorderDemo { pri ...