Java, WPI, Image Processing, and OMG I got a memory leak...

I created the targeting code for our robot today. Everything was working great until about 20 seconds in … WHAM! Out of memory… I’m pretty sure I freed both the BinaryImage I used and the ColorImage I used correctly. Please don’t tell me this is a memory leak in the WPILibJ because targeting will be impossible since all the code is precompiled for Java and would be extra-tricky to change.

You may not be running into a memory leak, but instead memory fragmentation. To minimize the number of (re)allocations done, you can try something like this:

// Inside the class, outside of any methods
Image image = new RGBImage();
BinaryImage bin = new BinaryImage();

Then later:

camera.getImage(image);
NIVision.colorThreshold(bin.image, image.image, ... );
// Other processing ...

Thank you for a positive response, Ill try this when Im with the robot next. I am curious though, is this how you do your targeting?

I used that technique last year to minimize memory consumption, but this year our tracking software is running as a SmartDashboard extension.

Just a notice that the SmartDashboard v1.05 revision is up as of a few hours ago, and it solves several issues with the vision plugin.

I’m not sure if that would help you (it sounds like you’re doing it robot-side?) but it may help others.

I don’t want the delay of the data transfer, but thank you for that wonderful news. I hope it fixes the SmartDashboard crashing bug.

What do I do about the exception handling?

try{
//
}catch(SomethingException ex){
ex.printStackTrace();
}

You can’t put try statements outside of a method. I found that simply creating the image objects outside of a method fixed my memory issues, I did not need to initialize them outside the method or call methods from NIVision. Thank you for the help Joe.