Problem closing images

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem closing images

strahli
This post was updated on .
Hei everybody.

I'm working with ImageJ and Java for a month now. My task is it to read an Image every 10 seconds from a camera, compare it to a reference image and create a result Image (shows the differences). Means that I need to have 3 images open at once. I can repeat this process about 260 times until the <Out of memory> exception comes. My problem is that I tried to close the Images, but if I do this, I get another NullPointerException.

================================
My code is like this:

start timer
if (timerEvent) {
  loadNewPicture();
}

loadNewPicture{
  result = manipulate(refImage, snapshot);
  ...
}

manipulate (refImage, snapshot) {
  if (refWindow == null) {
    refWindow = new ImageWindow(refImage)
  } else {
    refWindow.setImage(refImage)
  }
  // the same for the snapshot

  // execution of some macros
  Image result = WindowManager.getCurrentImage().getImage();
  return result;
}

================================

My problem is that I tried to close the images or the windows but if I do this , I get an exception in the class "ImageWindow" at row 526 (method "windowActivated" line: setMenuBar(Menus.getMenuBar());).

Btw: the image size is ca.70 KB.

Has anybody a hint for me?

Thanks in advance,
strahli

EDIT: And I've searched the older topics, but I didn't really found a solution except for closing the images, windows and using the garbage collector, but I can't really adapt it to my programm without getting other problems.
Reply | Threaded
Open this post in threaded view
|

Re: Problem closing images

Nathaniel Ryckman
It would help if you were able to give the whole stack trace. My guess is that this line is giving you the problem:

Image result = WindowManager.getCurrentImage().getImage();

From my experience, the getCurrentImage() command doesn't work very consistently. Maybe you could keep references of the images that you are opening up or encapsulate the getCurrentImage() command in a while loop that checks to see if the return value is valid.

strahli wrote
Hei everybody.

I'm working with ImageJ and Java for a month now. My task is it to read an Image every 10 seconds from a camera, compare it to a reference image and create a result Image (shows the differences). Means that I need to have 3 images open at once. I can repeat this process about 260 times until the <Out of memory> exception comes. My problem is that I tried to close the Images, but if I do this, I get another NullPointerException.

================================
My code is like this:

start timer
if (timerEvent) {
  loadNewPicture();
}

loadNewPicture{
  result = manipulate(refImage, snapshot);
  ...
}

manipulate (refImage, snapshot) {
  if (refWindow == null) {
    refWindow = new ImageWindow(refImage)
  } else {
    refWindow.setImage(refImage)
  }
  // the same for the snapshot

  // execution of some macros
  Image result = WindowManager.getCurrentImage().getImage();
  return result;
}

================================

My problem is that I tried to close the images or the windows but if I do this , I get an exception in the class "ImageWindow" at row 526 (method "windowActivated" line: setMenuBar(Menus.getMenuBar());).

Btw: the image size is ca.70 KB.

Has anybody a hint for me?

Thanks in advance,
strahli

EDIT: And I've searched the older topics, but I didn't really found a solution except for closing the images, windows and using the garbage collector, but I can't really adapt it to my programm without getting other problems.
Reply | Threaded
Open this post in threaded view
|

Re: Problem closing images

strahli
Here is the stack trace:

>>>>>>>>>>>>>>>>>>>>>>>>>>>
<Out of memory>
<All available memory (247MB) has been>
<used. Instructions for making more>
<available can be found in the "Memory" >
<sections of the installation notes at>
<http://imagej.nih.gov/ij/docs/install/>
>>>>>>>>>>>>>>>>>>>>>>>>>>>
load new Picture at Tue Jun 28 09:55:58 CEST 2011, number: 256
<Out of memory>
load new Picture at Tue Jun 28 09:55:59 CEST 2011, number: 257
Exception in thread "Run$_AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
        at ij.process.ColorProcessor.<init>(ColorProcessor.java:31)
        at ij.ImagePlus.setupProcessor(ImagePlus.java:644)
        at ij.ImagePlus.isInvertedLut(ImagePlus.java:1039)
        at ij.measure.Calibration.<init>(Calibration.java:83)
        at ij.ImagePlus.getLocalCalibration(ImagePlus.java:1763)
        at ij.ImagePlus.setType(ImagePlus.java:996)
        at ij.ImagePlus.setImage(ImagePlus.java:473)
        at ij.ImagePlus.<init>(ImagePlus.java:106)
        at ij.io.Opener.openJpegOrGif(Opener.java:522)
        at ij.io.Opener.openImage(Opener.java:268)
        at ij.io.Opener.openImage(Opener.java:303)
        at ij.ImagePlus.<init>(ImagePlus.java:125)
        at AutomaticMode.getNewSnapshot(AutomaticMode.java:85 --   ImagePlus snapshot = new ImagePlus("..");)
        at AutomaticMode.access$0(AutomaticMode.java:83 -- loadNewPicture())
        at AutomaticMode$1.actionPerformed(AutomaticMode.java:75 -- loadNewPicture()-call in startTimer())
        at AutomaticMode.startTimer(AutomaticMode.java:63 -- if(timerEvent))
        at AppGui$1.actionPerformed(AppGui.java:329 -- startTimer())
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

================================
and on the otherhands side I tried to remove many parts and check if they are the problem, but I don't think I found it. The things is I've worked with a Timer but yesterday I was told that I should better move the timing loop to its own thread .. so I tried this but still I get the exception.

A difference I can see between 2 computers, one with WinXP and 2GB RAM and my own with Win7 and 4GB RAM. With the first one I can run about 250 pictures with the second 750 before I get the exception. I hope that this is not a problem with the "low" RAM size of this computer. At least I think a program should be able to work with "only" 2GB.
Reply | Threaded
Open this post in threaded view
|

Re: Problem closing images

Nathaniel Ryckman
at ij.io.Opener.openJpegOrGif(Opener.java:522)

Yeah, actually, the problem is the RAM. As you can see from the error message above, the error <Out of memory> happens when the program tries to open a Jpeg image.

I would normally tell you to look at the online tutorial for increasing memory, but it sounds like you are opening a lot more then 2-4GB of images.

You can't store an entire hard drive worth of images in RAM -- there just isn't enough room. The work around is to only store the images that you are currently working on in RAM while temporarily saving the images that you aren't working on onto the hard drive.

Here's a tool to help you with storing the images in "virtual memory" (aka your hard drive):
http://rsbweb.nih.gov/ij/plugins/virtual-opener.html

strahli wrote
Here is the stack trace:

>>>>>>>>>>>>>>>>>>>>>>>>>>>
<Out of memory>
<All available memory (247MB) has been>
<used. Instructions for making more>
<available can be found in the "Memory" >
<sections of the installation notes at>
<http://imagej.nih.gov/ij/docs/install/>
>>>>>>>>>>>>>>>>>>>>>>>>>>>
load new Picture at Tue Jun 28 09:55:58 CEST 2011, number: 256
<Out of memory>
load new Picture at Tue Jun 28 09:55:59 CEST 2011, number: 257
Exception in thread "Run$_AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
        at ij.process.ColorProcessor.<init>(ColorProcessor.java:31)
        at ij.ImagePlus.setupProcessor(ImagePlus.java:644)
        at ij.ImagePlus.isInvertedLut(ImagePlus.java:1039)
        at ij.measure.Calibration.<init>(Calibration.java:83)
        at ij.ImagePlus.getLocalCalibration(ImagePlus.java:1763)
        at ij.ImagePlus.setType(ImagePlus.java:996)
        at ij.ImagePlus.setImage(ImagePlus.java:473)
        at ij.ImagePlus.<init>(ImagePlus.java:106)
        at ij.io.Opener.openJpegOrGif(Opener.java:522)
        at ij.io.Opener.openImage(Opener.java:268)
        at ij.io.Opener.openImage(Opener.java:303)
        at ij.ImagePlus.<init>(ImagePlus.java:125)
        at AutomaticMode.getNewSnapshot(AutomaticMode.java:85 --   ImagePlus snapshot = new ImagePlus("..");)
        at AutomaticMode.access$0(AutomaticMode.java:83 -- loadNewPicture())
        at AutomaticMode$1.actionPerformed(AutomaticMode.java:75 -- loadNewPicture()-call in startTimer())
        at AutomaticMode.startTimer(AutomaticMode.java:63 -- if(timerEvent))
        at AppGui$1.actionPerformed(AppGui.java:329 -- startTimer())
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

================================
and on the otherhands side I tried to remove many parts and check if they are the problem, but I don't think I found it. The things is I've worked with a Timer but yesterday I was told that I should better move the timing loop to its own thread .. so I tried this but still I get the exception.

A difference I can see between 2 computers, one with WinXP and 2GB RAM and my own with Win7 and 4GB RAM. With the first one I can run about 250 pictures with the second 750 before I get the exception. I hope that this is not a problem with the "low" RAM size of this computer. At least I think a program should be able to work with "only" 2GB.
Reply | Threaded
Open this post in threaded view
|

Re: Problem closing images

strahli
this Virtual_stack_opener sounds interesting but I'm not sure if I really need this.

I don't want to open that amount of pictures. I just have one reference image that I load only once during the whole execution. Further I load every ten seconds an image from the camera and I create a result image out of the reference image and the camera image. After that I could close the camera and the result image because I don't need them anymore. I just need 3 images at once. And shouldn't the RAM can handle it if only three images are opened? Thats the thing I don't really get!
Reply | Threaded
Open this post in threaded view
|

Re: Problem closing images

Nathaniel Ryckman
After that I could close the camera and the result image because I don't need them anymore.

That might solve your problem.

The way Java works is that it will keep anything in memory that has a reference pointing to it. In other words, the images that you are temporarily opening up probably have never been closed, so they all are still in your RAM. You have to close the images in order to make sure that they are no longer in your ram.

There are many articles on the Java garbage collector, but I found a fairly concise description on this blog if you would like to read more about Java memory management:

http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html

Good luck! Feel free to ask more questions if that doesn't help.

strahli wrote
this Virtual_stack_opener sounds interesting but I'm not sure if I really need this.

I don't want to open that amount of pictures. I just have one reference image that I load only once during the whole execution. Further I load every ten seconds an image from the camera and I create a result image out of the reference image and the camera image. After that I could close the camera and the result image because I don't need them anymore. I just need 3 images at once. And shouldn't the RAM can handle it if only three images are opened? Thats the thing I don't really get!
Reply | Threaded
Open this post in threaded view
|

Re: Problem closing images

strahli
Hei there,

because I saw no other chance I wanted to try with the virtual stack opener. But then I saw that the Virtual Stack opener opens a folder of images and not only one. That's a problem because I only have one image at a time!

So initially I load the reference image which isn't changed during the whole process. And then I fetch every 10 seconds an image from the camera, but I don't create a new Imageplus for this but I do: oldCameraImage.setImage(new ImageIcon(path)). I don't see a good use for opening a folder.

Despite this I also tried to use the garbage collector, but this doesn't change anything! Even if I call the garbagecollector my memory usage is increasing continiously!

Any other ideas? I'm really clueless. Thanks in advance!!!

Reply | Threaded
Open this post in threaded view
|

Re: Problem closing images

strahli
Hei guys ..

hasn't anybody a idea how to fix this??? Please I'm stuck but I really need to fix it!

Thanks!