Log in

View Full Version : Java, WPI, Image Processing, and OMG I got a memory leak...


Team3266Spencer
13-02-2013, 23:02
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.

Ginto8
14-02-2013, 06:06
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 ...

Team3266Spencer
14-02-2013, 10:03
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?

Ginto8
14-02-2013, 22:24
I used that technique last year to minimize memory consumption, but this year our tracking software is running as a SmartDashboard extension.

F22Rapture
15-02-2013, 00:31
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.

Team3266Spencer
15-02-2013, 07:53
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.

Team3266Spencer
16-02-2013, 12:44
What do I do about the exception handling?

Arhowk
16-02-2013, 17:54
What do I do about the exception handling?

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

Team3266Spencer
16-02-2013, 22:35
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.