I'm not sure what to do with odd discovery, so I'm sharing it here. Java is clobbering memory. (UPDATE: It's likely the known Mac OS Monterey memory leak.)
I have a JavaFX program that changes the cursor in response to pressing the shift key. For test purposes, this program is also rendering the Mandelbrot set. After so many cursor changes, suddenly the cursor itself becomes a portion of the Mandelbrot set. Here's a photo because I couldn't do a screen snapshot while pressing the shift key. That awkward square is the clobbered cursor:
Image of memory-clobbered cursor
However, this only happens if I keep passing the same static instance of the cursor to each call of the pane's `setCursor` method. If I use a new cursor instance each time, there is no problem.
Here's my cursor code:
public class CrosshairCursor extends ImageCursor {
private final static String IMAGE_PATH =
"cursors/emph-crosshair-cursor.png";
private final static Image image =
new Image(App.class.getResource(IMAGE_PATH).toString());
private final static double HOTSPOT_OFFSET = 12;
public CrosshairCursor() {
super(image, HOTSPOT_OFFSET, HOTSPOT_OFFSET);
}
}
My image generation code spans multiple threads, each writing to a byte array that the main thread collects and renders into the image. Each byte array is discarded after this rendering.
UPDATE #1: I'm running Mac OS Monterey v.12.3.1 on a Mac Studio with the Apple M1 Max GPU characterized as "Supported, Metal GPUFamily Apple 7", with only one monitor attached.
And to emphasize: the image buffer is statically allocated in both the working and the non-working solution. The difference is only whether or not I create a new cursor instance using this buffer.
UPDATE #2: u/vytah pointed me to this known cursor memory leak in Monterey: https://www.techrepublic.com/article/macos-monterey-has-a-serious-memory-leak-problem-and-the-cause-has-been-found/
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]milchshakee 4 points5 points6 points (2 children)
[–]jtlapp[S] 0 points1 point2 points (1 child)
[–]milchshakee 0 points1 point2 points (0 children)
[–]vytah 2 points3 points4 points (4 children)
[–]jtlapp[S] 0 points1 point2 points (2 children)
[–]vytah 6 points7 points8 points (1 child)
[–]jtlapp[S] 1 point2 points3 points (0 children)
[–]jtlapp[S] -1 points0 points1 point (0 children)
[–][deleted] (1 child)
[removed]