为什么下面的代码会报内存溢出呢

[code="java"]
Robot ro = new Robot();
Rectangle rec = ScreenPanel.select;
BufferedImage img = ro.createScreenCapture(rec);
Image image = img.getScaledInstance(w, h, Image.SCALE_DEFAULT);
BufferedImage bufImg = new BufferedImage(w,h,Image.SCALE_DEFAULT);
bufImg.getGraphics().drawImage(image, 0, 0, null);
q.push(bufImg);
[/code]
上面的代码在定时器里面50毫秒执行一次,执行几次之后就会报错:
Exception in thread "Timer-2" java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferInt.(DataBufferInt.java:75)
at java.awt.image.Raster.createPackedRaster(Raster.java:470)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
at sun.awt.image.ImageRepresentation.createBufferedImage(ImageRepresentation.java:253)
at sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:554)
at java.awt.image.ReplicateScaleFilter.setPixels(ReplicateScaleFilter.java:250)
at sun.awt.image.OffScreenImageSource.sendPixels(OffScreenImageSource.java:137)
at sun.awt.image.OffScreenImageSource.produce(OffScreenImageSource.java:187)
at sun.awt.image.OffScreenImageSource.addConsumer(OffScreenImageSource.java:66)
at sun.awt.image.OffScreenImageSource.startProduction(OffScreenImageSource.java:80)
at java.awt.image.FilteredImageSource.startProduction(FilteredImageSource.java:183)
at sun.awt.image.ImageRepresentation.startProduction(ImageRepresentation.java:727)
at sun.awt.image.ImageRepresentation.drawToBufImage(ImageRepresentation.java:802)
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:1018)
at sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:186)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3084)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3069)
at ideamoment.Recorde.queue.CutScreen.run(CutScreen.java:31)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
我是想不停的截图,然后缩放,然后放进一个list中,这个list是队列,我会1毫秒检查一次读取第一个然后remove第一个,理论上应该没问题啊......

查看全部
a97263047
a97263047
2012/10/17 16:59
  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

0个回复