HEAPSPACE ERROR:无法找出导致错误的原因
有人介意看一下这些代码,看看它们是否存在内存泄漏,它不会太大,但我的程序运行一段时间后不断崩溃,我不知道如何使用viualvm,尽管我已经阅读了好几天了,并且完全不知道我在堆转储等中寻找什么。 感谢您的帮助, ps 我知道我已经发布了很多代码,但我不知道还能做什么,以便您能够看到我出现问题的地方。 如果有帮助,我可以通过电子邮件将完整的程序发送给您以供查看。 非常感谢您提供的任何帮助。
public class extraScreenPanel {
static JPanel screenPanel = new JPanel(new BorderLayout());
public static JPanel extraScreenPanel(int dispNum)
{
JLabel label = new JLabel("" + dispNum + "");
label.setPreferredSize(new Dimension(800, 600));
label.setVerticalAlignment( SwingConstants.TOP );
screenPanel = imgDisp(dispNum);
label.setForeground(Color.white);
label.setFont(new Font("Serif", Font.BOLD, 200));
screenPanel.add(label, BorderLayout.PAGE_END );
return screenPanel;
}
public static JPanel imgDisp(int picNum) {
String ref = "C:/PiPhotoPic/pic16.jpg";;
BufferedImage loadImg = loadImage(ref);
JImagePanel panel = new JImagePanel(loadImg, 0, 0);
panel.setPreferredSize(new Dimension(800, 600));
return panel;
}
public static class JImagePanel extends JPanel{
private BufferedImage image;
int x, y;
public JImagePanel(BufferedImage image, int x, int y) {
super();
this.image = image;
this.x = x;
this.y = y;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, x, y, null);
}
}
public static BufferedImage loadImage(String ref) {
BufferedImage bimg = null;
try {
bimg = javax.imageio.ImageIO.read(new File(ref));
} catch (Exception e) {
e.printStackTrace();
}
BufferedImage bimg2 = resize(bimg,800,600);//set these to the resolution of extra screens
return bimg2;
}
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
return dimg;
}
}
该方法的另一部分
public class piPhoto
{
static int mostRecent = 0;
static int dispNum = 0;
static int lastDisp = 0;
static JPanel picPanel = imgDisp.imgDisp(dispNum);
static JFrame frame = new JFrame("Pi Photography");
static JPanel cornerPanel = new JPanel();
static JPanel bottomPanel = new JPanel();
static JPanel menuPanel = new JPanel(new BorderLayout());
static JPanel currentNumPanel = currentNumDisp.currentNumDisp(dispNum);
static JPanel printPanel = printOptions.printOptions();
static JPanel buttonPanel = updateButtonPanel.updateButtonPanel(mostRecent);
static JPanel screen1Panel = new JPanel();
static JPanel screen2Panel = new JPanel();
static JPanel screen3Panel = new JPanel();
static JPanel screen4Panel = new JPanel();
static JPanel screenPanel12 = new JPanel(new BorderLayout());
static JPanel screenPanel123 = new JPanel(new BorderLayout());
static JPanel screenPanel1234 = new JPanel(new BorderLayout());
static JPanel screensPanel = new JPanel(new BorderLayout());
static JPanel deskScreen = new JPanel();
static JPanel wholePanel = new JPanel(new BorderLayout());
static JPanel wholePanel2 = new JPanel(new BorderLayout());
public static void launchPiPhoto()
{
launchNC4.launch();
bottomPanel.setPreferredSize(new Dimension(1440, 200));
buttonPanel.setPreferredSize(new Dimension(1120, 200));
cornerPanel.setPreferredSize(new Dimension(300,200));
screen1Panel.setPreferredSize(new Dimension(800,600));
screen2Panel.setPreferredSize(new Dimension(800,600));
screen3Panel.setPreferredSize(new Dimension(800,600));
screen4Panel.setPreferredSize(new Dimension(800,600));
screensPanel.setPreferredSize(new Dimension(3200,600));
deskScreen.setPreferredSize(new Dimension(800,600));
wholePanel.setPreferredSize(new Dimension(4640,900));
wholePanel2.setPreferredSize(new Dimension(5440,900));
cornerPanel.setLayout(new BoxLayout(cornerPanel, BoxLayout.PAGE_AXIS));
picPanel.setPreferredSize(new Dimension(1120, 620));
//Menu Panel Set-up
cornerPanel.add(currentNumPanel);
bottomPanel.add(buttonPanel);
bottomPanel.add(cornerPanel);
menuPanel.setPreferredSize(new Dimension(1440, 840));
menuPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
menuPanel.add(bottomPanel, BorderLayout.PAGE_END);
menuPanel.add(picPanel, BorderLayout.LINE_START);
menuPanel.add(printPanel, BorderLayout.LINE_END);
screen1Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent - 3);
screen2Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent - 2);
screen3Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent - 1);
screen4Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent);
screenPanel12.add(screen1Panel, BorderLayout.LINE_START);
screenPanel12.add(screen2Panel, BorderLayout.LINE_END);
screenPanel123.add(screenPanel12, BorderLayout.LINE_START);
screenPanel123.add(screen3Panel, BorderLayout.LINE_END);
screenPanel1234.add(screenPanel123, BorderLayout.LINE_START);
screenPanel1234.add(screen4Panel, BorderLayout.LINE_END);
screensPanel.add(screenPanel1234, BorderLayout.LINE_END);
deskScreen = extraScreenPanel.extraScreenPanel(dispNum);
wholePanel.add(menuPanel, BorderLayout.LINE_START);
wholePanel.add(screensPanel, BorderLayout.LINE_END);
wholePanel2.add(wholePanel, BorderLayout.LINE_START);
wholePanel2.add(deskScreen, BorderLayout.LINE_END);
frame.add(wholePanel2);
//Frame set-up and Initializing
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
newImageRecieved.runCheck();
}
public static void repaintButtonPanel()
{
bottomPanel.removeAll();
bottomPanel.setPreferredSize(new Dimension(1440, 200));
buttonPanel = updateButtonPanel.updateButtonPanel(mostRecent);
buttonPanel.setPreferredSize(new Dimension(1120, 200));
cornerPanel.add(currentNumPanel);
bottomPanel.add(buttonPanel);
bottomPanel.add(cornerPanel);
menuPanel.add(bottomPanel, BorderLayout.PAGE_END);
frame.validate();
}
public static void repaintScreens()
{
wholePanel.remove(screensPanel);
screen1Panel.removeAll();
screen1Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent);
screen2Panel.removeAll();
screen2Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent - 1);
screen3Panel.removeAll();
screen3Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent - 2);
screen4Panel.removeAll();
screen4Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent - 3);
screenPanel12.add(screen1Panel, BorderLayout.LINE_START);
screenPanel12.add(screen2Panel, BorderLayout.LINE_END);
screenPanel123.add(screenPanel12, BorderLayout.LINE_START);
screenPanel123.add(screen3Panel, BorderLayout.LINE_END);
screenPanel1234.add(screenPanel123, BorderLayout.LINE_START);
screenPanel1234.add(screen4Panel, BorderLayout.LINE_END);
screensPanel.add(screenPanel1234, BorderLayout.LINE_END);
wholePanel.add(menuPanel, BorderLayout.LINE_START);
wholePanel.add(screensPanel, BorderLayout.LINE_END);
frame.validate();
}
}
是检查新图像是否已添加到文件中的部分
public class newImageRecieved {
static int count = 0;
public static void runCheck()
{
int delay = 500;
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
newImageRecieved(piPhoto.mostRecent+1);
}
};
new Timer(delay, taskPerformer).start();
}
public static void newImageRecieved(int picNum)
{
String url[] = new String[5000];
String part1;
url[0] = "C:/PiPhotoPic/pic16.jpg";
for(Integer i=1;i<5000;i++){
if(i<10){part1 = "C:/temp/new0000000";}
else if(i<100){part1 = "C:/temp/new000000";}
else if(i<1000){part1 = "C:/temp/new00000";}
else {part1 = "C:/temp/new00000";}
String num = Integer.toString(i);
url[i]= part1 + num + ".jpg";
}
if(picNum<0){picNum=0;}
String ref = url[picNum];
piPhoto.frame.validate();
boolean exists = (new File(ref)).exists();
if(exists == true)
{
while (exists == true)
{
piPhoto.updateMostRecent(picNum);
ref = url[picNum + 1];
picNum = picNum + 1;
exists = (new File(ref)).exists();
}
piPhoto.updateMostRecent(picNum-1);
piPhoto.repaintButtonPanel();
piPhoto.repaintScreens();
count=0;
}
// }
}
}
Would anyone mind having a look at these bits of code and see if there is a memory leak in them, it isn't going to be overly large but my program keeps crashing after a while of running and I don't know how to use viualvm even though I have been reading up on it for several days now and haven't the slightest idea what I am looking for in the heap dumps and so on. thanks for your help,
ps I know I have posted far to muchg code but i dont know what else to do so that you can hopefully see where the problem arises for me. If it helps I can email you the full program to take a look at. Thanks very much for any help you can give.
public class extraScreenPanel {
static JPanel screenPanel = new JPanel(new BorderLayout());
public static JPanel extraScreenPanel(int dispNum)
{
JLabel label = new JLabel("" + dispNum + "");
label.setPreferredSize(new Dimension(800, 600));
label.setVerticalAlignment( SwingConstants.TOP );
screenPanel = imgDisp(dispNum);
label.setForeground(Color.white);
label.setFont(new Font("Serif", Font.BOLD, 200));
screenPanel.add(label, BorderLayout.PAGE_END );
return screenPanel;
}
public static JPanel imgDisp(int picNum) {
String ref = "C:/PiPhotoPic/pic16.jpg";;
BufferedImage loadImg = loadImage(ref);
JImagePanel panel = new JImagePanel(loadImg, 0, 0);
panel.setPreferredSize(new Dimension(800, 600));
return panel;
}
public static class JImagePanel extends JPanel{
private BufferedImage image;
int x, y;
public JImagePanel(BufferedImage image, int x, int y) {
super();
this.image = image;
this.x = x;
this.y = y;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, x, y, null);
}
}
public static BufferedImage loadImage(String ref) {
BufferedImage bimg = null;
try {
bimg = javax.imageio.ImageIO.read(new File(ref));
} catch (Exception e) {
e.printStackTrace();
}
BufferedImage bimg2 = resize(bimg,800,600);//set these to the resolution of extra screens
return bimg2;
}
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
return dimg;
}
}
Another part of the method
public class piPhoto
{
static int mostRecent = 0;
static int dispNum = 0;
static int lastDisp = 0;
static JPanel picPanel = imgDisp.imgDisp(dispNum);
static JFrame frame = new JFrame("Pi Photography");
static JPanel cornerPanel = new JPanel();
static JPanel bottomPanel = new JPanel();
static JPanel menuPanel = new JPanel(new BorderLayout());
static JPanel currentNumPanel = currentNumDisp.currentNumDisp(dispNum);
static JPanel printPanel = printOptions.printOptions();
static JPanel buttonPanel = updateButtonPanel.updateButtonPanel(mostRecent);
static JPanel screen1Panel = new JPanel();
static JPanel screen2Panel = new JPanel();
static JPanel screen3Panel = new JPanel();
static JPanel screen4Panel = new JPanel();
static JPanel screenPanel12 = new JPanel(new BorderLayout());
static JPanel screenPanel123 = new JPanel(new BorderLayout());
static JPanel screenPanel1234 = new JPanel(new BorderLayout());
static JPanel screensPanel = new JPanel(new BorderLayout());
static JPanel deskScreen = new JPanel();
static JPanel wholePanel = new JPanel(new BorderLayout());
static JPanel wholePanel2 = new JPanel(new BorderLayout());
public static void launchPiPhoto()
{
launchNC4.launch();
bottomPanel.setPreferredSize(new Dimension(1440, 200));
buttonPanel.setPreferredSize(new Dimension(1120, 200));
cornerPanel.setPreferredSize(new Dimension(300,200));
screen1Panel.setPreferredSize(new Dimension(800,600));
screen2Panel.setPreferredSize(new Dimension(800,600));
screen3Panel.setPreferredSize(new Dimension(800,600));
screen4Panel.setPreferredSize(new Dimension(800,600));
screensPanel.setPreferredSize(new Dimension(3200,600));
deskScreen.setPreferredSize(new Dimension(800,600));
wholePanel.setPreferredSize(new Dimension(4640,900));
wholePanel2.setPreferredSize(new Dimension(5440,900));
cornerPanel.setLayout(new BoxLayout(cornerPanel, BoxLayout.PAGE_AXIS));
picPanel.setPreferredSize(new Dimension(1120, 620));
//Menu Panel Set-up
cornerPanel.add(currentNumPanel);
bottomPanel.add(buttonPanel);
bottomPanel.add(cornerPanel);
menuPanel.setPreferredSize(new Dimension(1440, 840));
menuPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
menuPanel.add(bottomPanel, BorderLayout.PAGE_END);
menuPanel.add(picPanel, BorderLayout.LINE_START);
menuPanel.add(printPanel, BorderLayout.LINE_END);
screen1Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent - 3);
screen2Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent - 2);
screen3Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent - 1);
screen4Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent);
screenPanel12.add(screen1Panel, BorderLayout.LINE_START);
screenPanel12.add(screen2Panel, BorderLayout.LINE_END);
screenPanel123.add(screenPanel12, BorderLayout.LINE_START);
screenPanel123.add(screen3Panel, BorderLayout.LINE_END);
screenPanel1234.add(screenPanel123, BorderLayout.LINE_START);
screenPanel1234.add(screen4Panel, BorderLayout.LINE_END);
screensPanel.add(screenPanel1234, BorderLayout.LINE_END);
deskScreen = extraScreenPanel.extraScreenPanel(dispNum);
wholePanel.add(menuPanel, BorderLayout.LINE_START);
wholePanel.add(screensPanel, BorderLayout.LINE_END);
wholePanel2.add(wholePanel, BorderLayout.LINE_START);
wholePanel2.add(deskScreen, BorderLayout.LINE_END);
frame.add(wholePanel2);
//Frame set-up and Initializing
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
newImageRecieved.runCheck();
}
public static void repaintButtonPanel()
{
bottomPanel.removeAll();
bottomPanel.setPreferredSize(new Dimension(1440, 200));
buttonPanel = updateButtonPanel.updateButtonPanel(mostRecent);
buttonPanel.setPreferredSize(new Dimension(1120, 200));
cornerPanel.add(currentNumPanel);
bottomPanel.add(buttonPanel);
bottomPanel.add(cornerPanel);
menuPanel.add(bottomPanel, BorderLayout.PAGE_END);
frame.validate();
}
public static void repaintScreens()
{
wholePanel.remove(screensPanel);
screen1Panel.removeAll();
screen1Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent);
screen2Panel.removeAll();
screen2Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent - 1);
screen3Panel.removeAll();
screen3Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent - 2);
screen4Panel.removeAll();
screen4Panel = extraScreenPanel.extraScreenPanel(piPhoto.mostRecent - 3);
screenPanel12.add(screen1Panel, BorderLayout.LINE_START);
screenPanel12.add(screen2Panel, BorderLayout.LINE_END);
screenPanel123.add(screenPanel12, BorderLayout.LINE_START);
screenPanel123.add(screen3Panel, BorderLayout.LINE_END);
screenPanel1234.add(screenPanel123, BorderLayout.LINE_START);
screenPanel1234.add(screen4Panel, BorderLayout.LINE_END);
screensPanel.add(screenPanel1234, BorderLayout.LINE_END);
wholePanel.add(menuPanel, BorderLayout.LINE_START);
wholePanel.add(screensPanel, BorderLayout.LINE_END);
frame.validate();
}
}
the part that checks to see if new images are added to the file yet
public class newImageRecieved {
static int count = 0;
public static void runCheck()
{
int delay = 500;
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
newImageRecieved(piPhoto.mostRecent+1);
}
};
new Timer(delay, taskPerformer).start();
}
public static void newImageRecieved(int picNum)
{
String url[] = new String[5000];
String part1;
url[0] = "C:/PiPhotoPic/pic16.jpg";
for(Integer i=1;i<5000;i++){
if(i<10){part1 = "C:/temp/new0000000";}
else if(i<100){part1 = "C:/temp/new000000";}
else if(i<1000){part1 = "C:/temp/new00000";}
else {part1 = "C:/temp/new00000";}
String num = Integer.toString(i);
url[i]= part1 + num + ".jpg";
}
if(picNum<0){picNum=0;}
String ref = url[picNum];
piPhoto.frame.validate();
boolean exists = (new File(ref)).exists();
if(exists == true)
{
while (exists == true)
{
piPhoto.updateMostRecent(picNum);
ref = url[picNum + 1];
picNum = picNum + 1;
exists = (new File(ref)).exists();
}
piPhoto.updateMostRecent(picNum-1);
piPhoto.repaintButtonPanel();
piPhoto.repaintScreens();
count=0;
}
// }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用内存分析器,例如 YourKit
You might try a memory profiler such as YourKit
今天解决了。 事实证明,我没有从旧方法中删除图像面板,因此当我重新绘制它们时,我是在旧图像面板上绘画,而旧图像面板仍然在那里。 所以我只需要为所有正在更新的面板添加removeAll(),并且程序运行良好,没有堆堆栈错误。
Solved it today. Turns out I didnt remove the image panels from the old method, aso that when i was repainting them I was painting over the old ones and the old ones were still there. So I just needed to add in removeAll() for all the panels that were being updated and the program ran fine without heapstack errors.
好吧,您正在创建一个包含 5000 个字符串的数组,并开始填充其中的大部分。
Well, you are creating an array of 5000 strings and start fill up most of them.