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();
}
}
Eclipse下内存溢出错误(OutOfMemoryError)的更多相关文章
- eclipse启动tomcat出现内存溢出错误 java.lang.OutOfMemoryError: PermGen space
发布工程后,启动tomcat出现如下内存溢出错误: java.lang.OutOfMemoryError: PermGen space ... java.lang.OutOfMemoryError: ...
- 一次apk打开时报内存溢出错误,故写下内存溢出的各种原因和解决方法
原转载:https://blog.csdn.net/cp_panda_5/article/details/79613870 正文内容: 对于JVM的内存写过的文章已经有点多了,而且有点烂了,不过说那么 ...
- java中三种常见内存溢出错误的处理方法
更多 10 相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的 ...
- java中三种常见内存溢出错误的处理方法(good)
相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的认识. 在解决j ...
- Xamarin Android提示内存溢出错误
Xamarin Android提示内存溢出错误 错误信息:java.lang.OutOfMemoryError, Consider increasing the value of $(JavaMaxi ...
- eclipse 出现内存溢出问题解决办法
1.eclipse.ini添加设置: -vm#eclipse启动使用的jdk设置,路径根据自己实际路径修改 C:/Program Files/Java/jdk1.6.0_45/bin/javaw.ex ...
- Eclipse中运行Tomcat遇到的内存溢出错误
使用Eclipse(版本Indigo 3.7)调试Java项目的时候,遇到了下面的错误: Exception in thread "main" Java.lang.OutOfMem ...
- Java抛出OutOfMemoryError:Java heap space堆内存溢出错误的分析方案
抛出堆内存溢出的错误一定要记得保留现场环境(导出堆内存信息到文件),否则如果无法进行分析,并从根本上解决问题,下次很有可能还会出现. 第一步:导出堆转储文件 我们可以使用Jdk自带的jmap工具.使用 ...
- eclipse启动Tomcat加载项目时报内存溢出错误解决办法
在eclipse中点击Window->Preferences打开全局属性设置对话框,如下图所示设置Tomcat运行时的JVM参数,添加这段JVM设置:-Xms256M -Xmx768M -XX: ...
随机推荐
- 一段js代码
原文地址 [].forEach.call($$("*"),function(a){ a.style.outline="1px solid #"+(~~(Math ...
- 跟我学SharePoint 2013视频培训课程——使用垃圾箱(5)
课程简介 第5天,在SharePoint 2013中 使用垃圾箱 视频 SharePoint 2013 交流群 41032413
- Set up development environment for apps for SharePoint 2013
SharePoint 2013 support app development pattern.An app for SharePoint is small and isolate applicati ...
- Java Nashorn--Part 4
Nashorn 和 javax.script 包 Nashorn 并不是第一个在 Java 平台上运行的脚本语言.在Java 6 就提供了 javax.script java 包,它为脚本语言引擎提供 ...
- SQLServer2012 (非)聚集索引存储探究
SQLServer2012 (非)聚集索引存储探究 Author:zfive5(zidong) Email:zfive5@163.com 引子 因为写了前一篇文字<SQLServer2012 表 ...
- (面试)写出下面switch语句的输出结果
(1) public static void main(String[] as) { int a = 0; switch (a) { case 1: System.out.println(&q ...
- linux 调用shell脚本传参
例子: boolean execResult = true; BufferedReader br = null; try { //lin ...
- 常用的代码之一:用StopWatch计算代码运行花费的时间。
先引用Diagnostics using System.Diagnostics; 然后: Stopwatch stopWatch = new Stopwatch(); stopWatch.Start( ...
- java项目中显示图表:struts2整合jfreechart
需要的包: struts2-jfreechart-plugin-2.2.1.1.jar jfreechart-1.0.13.jar jcommon-1.0.17.jar 前台jsp页面中可以使用ifr ...
- IOS 缓存方案(按需缓存 、 预缓存)及 低网速模拟
1,在设备中 设置开发者模式. 参照上面设置 自定义 添加.丢包率 35. 或者参照这个文章:http://ivoryxiong.org/devops/2013/05/24/ios_dev_handl ...