many copy calls lead to OutOfMemoryError
in
Core Library Questions
•
9 months ago
Hi everyone,
I'm working on a program, which slices up images and draws these slices on the screen. But no matter if I try PGraphics or PImages after a while my program simply crashes because of the OutOfMemoryError.
I have simplified my program so that is easy go trough it. But it seems like if the copy call leaves something in the memory, which is not cleaned up.
- PImage s1, s2;
- int left = 1;
- int mode = 0;
- int sliceW = 50;
- PImage pg;
- @Override
- public void setup() {
- super.setup();
- s1 = loadImage("1.jpg");
- s2 = loadImage("2.jpg");
- size(s1.width * 2,s1.height);
- pg = createImage((int)( s1.width * .5f), s1.height , RGB);
- }
- @Override
- public void draw() {
- Object[] leftG = getSlice(getPart(false, left == 1 ? s1 : s2), sliceW);
- Object[] rightG = getSlice(getPart(true, left == 1 ? s2 : s1), sliceW);
- switch (mode) {
- case 0: // simple copy
- image((PImage) leftG[0], (Integer) leftG[1], 0);
- image((PImage) rightG[0], width - (Integer) rightG[1], 0);
- break;
- }
- }
- private Object[] getSlice(PImage pg, int size) {
- PImage slice = createImage(size, pg.height, RGB);
- int x = (int) random(pg.width - size);
- slice.copy(pg, x, 0, size, pg.height, 0, 0, size, pg.height);
- return new Object[] { slice, x };
- }
- private PImage getPart(boolean left, PImage img) {
- int pgX = (int) (img.width * .5f);
- int pgY = img.height;
- if (left)
- pg.copy(img, 0, 0, pgX, pgY, 0, 0, pgX, pgY);
- else
- pg.copy(img, (int) img.width - pgX, 0, pgX, pgY, 0, 0, pgX, pgY);
- return pg;
- }
1