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: ...
随机推荐
- CListCtrl选中某行
原文链接: http://blog.csdn.net/wxq1987525/article/details/7461461 1.设置CListCtrl选中行 m_list.SetItemState( ...
- php分享十八:网页抓取
一.抓取远程图片到本地,你会用什么函数? 方法1:利用readfile读取远程图片到缓冲中,然后写入新的文件 function grabImage($url, $filename = '') { if ...
- PhotoShop CS6实现照片背景虚化效果
在摄影实践中,虚化背景是突出主体的常用手段.但是由于消费级DC镜头的实际焦距都很短,因此实现浅景深而虚化背景的难度较大.如果我们希望用消费级DC也能达到虚化背景突出主体的效果,那么,Photoshop ...
- Ubuntu java install & config
im:http://jingyan.baidu.com/article/08b6a591cb06f114a8092209.html http://www.cnblogs.com/zknublx/p/5 ...
- eclipse自动切换到debug视图
原文出自:http://blog.csdn.net/yizhizouxiaqu/article/details/7594502 当弹出"Confir Perspective Switch&q ...
- 各个框架下的aop
http://www.cnblogs.com/neverc/p/5241466.html
- HTML5学习笔记(三):语义化和新增结构元素
在HTML5之前,使用机器来阅读一个网页是非常困难的,我们使用不同样式的div来标记不同的内容,所以实际上机器无法得知页面的哪个部分是正文,哪个部分是标题,那么在HTML5里,针对这个问题就引入了语义 ...
- 菜鸟学SSH(十三)——Spring容器IOC解析及简单实现
最近一段时间,“容器”两个字一直萦绕在我的耳边,甚至是吃饭.睡觉的时候都在我脑子里蹦来蹦去的.随着这些天一次次的交流.讨论,对于容器的理解也逐渐加深.理论上的东西终归要落实到实践,今天就借助Sprin ...
- CCDictionary
/** * CCDictionary is a class like NSDictionary in Obj-C . * * @note Only the pointer of CCObject or ...
- Flink 中的kafka何时commit?
https://ci.apache.org/projects/flink/flink-docs-release-1.6/internals/stream_checkpointing.html @Ove ...