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

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. Android 关于导航栏(虚拟按键)遮挡PopupWindow底部布局的问题

    我们自定义popupWindow的时候,一般会设置这些参数 setContentView(contentView); //设置高度为屏幕高度 setWidth(UIUtils.getScreenHei ...

  2. 【转】Google 的眼光

    Google 的眼光 你知道吗,Google(Alphabet)要卖掉 Boston Dynamics,一个它收购才没多久的机器人公司.这也意味着,Google 准备完全退出机器人的领域.新闻传言说, ...

  3. 图床神器:七牛云 + Mpic + FScapture

    概述 最近在搞Markdown的东西,遇到了一个很棘手的问题,即图片的显示:通用的图片,可以直接网上搜索,但有时候需要自己截一些图或者对下载的图片进行修改,在本地存储完全没有问题,但Markdown写 ...

  4. Mac下安装mysql8.0.11

    1.下载MySQL Community 版本:8.0.11,本次例子是以dmg安装的方式,下载的文件名为:mysql-8.0.11-macos10.13-x86_64.dmg 下载地址:https:/ ...

  5. ext: gridpanel中的点击事件的参数是什么意思?

    listeners: {      // 当用户单击列表项时激发该函数      itemclick: function(view, record, item, index, evt)  //①    ...

  6. linux环境下matlab连接mysql

    因为matlab是基于java的,但是原生的matlab是没有jdbc的,这是一个java的mysql connection. 只有matlab有这个包,才能正确的连接mysql. 1.在http:/ ...

  7. C# Split() 去除 \r\n 分组

    str为读入的文本string[] ReadText = str.Replace("\r\n", "@").Split('@'); 转自 http://zhid ...

  8. 【Android开发】交互界面布局详解

    原文:http://android.eoe.cn/topic/summary Android 的系统 UI 为构建您自己的应用提供了基础的框架.主要包括主屏幕 (Home Screen).系统 UI ...

  9. Android Eclipse Libs 的 jar 源码查看 (或者新版本ADT无法查看jar的源码)

    问题背景:在使用比较新的ADT的时候,无法导入Jar中的源码包查看源码.只好自己打开压缩包,实在恼火.在半年前,只好这样. 问题解决方案:我就以 " android-support-v4.j ...

  10. 从头认识多线程-1.8 迫使线程停止的方法-暴力Stop方法

    这一章节我们来讨论一下暴力Stop方法. 1.使用样例 package com.ray.deepintothread.ch01.topic_8; public class StopByStopMeth ...