-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory Leak #2373
Comments
i think this is by-design. graphics objects are not automatically garbage-collected. in this case, you should be calling |
You are calling You can either remove the graphics when you no longer need them or even better, create a global graphics buffer on setup then just reuse that in your code. |
@Spongman and @limzykenneth: your explanation are clear, I didn't know the Using a global once-instantiated And even if I use a global Here is the new script following the useful but non efficient previous advices: var graphicBuffer = null;
function setup() {
createCanvas(500, 500);
// it seems the graphicBuffer has a different pixel density (2) than
// the screen. So it has to be twice the size of the screen.
graphicBuffer = createGraphics(width * 2, height * 2);
// slow the things down to be able to keep control over
//frameRate(1);
}
function draw() {
background(100);
drawHollowedShape();
}
function drawHollowedShape() {
graphicBuffer.background('white');
graphicBuffer.fill(color('blue'));
// draw a shape changing randomly
graphicBuffer.ellipse(graphicBuffer.width / 4, graphicBuffer.height / 4,
random(graphicBuffer.width / 2), random(graphicBuffer.height / 2));
chromaKey(graphicBuffer, color('blue'), 0.1);
// graphicBuffer has to be scaled down to fit the screen
image(graphicBuffer, 0, 0, width, height);
}
function chromaKey(image, keyColor, threshold) {
let pixelCount = image.width * image.height * 2;
let keyRedComponent = red(keyColor);
let keyGreenComponent = green(keyColor);
let keyBlueComponent = blue(keyColor);
image.loadPixels();
for (let pixelIndex = 0; pixelIndex < pixelCount; pixelIndex++) {
let pixelComponentsIndex = pixelIndex * 4;
let pixelRedComponent = image.pixels[pixelComponentsIndex];
let pixelGreenComponent = image.pixels[pixelComponentsIndex + 1];
let pixelBlueComponent = image.pixels[pixelComponentsIndex + 2];
let pixelToKeyDistance = dist(
pixelRedComponent, pixelGreenComponent, pixelBlueComponent,
keyRedComponent, keyGreenComponent, keyBlueComponent);
if(pixelToKeyDistance < threshold) {
// set the pixel alpha channel to full transparency
image.pixels[pixelComponentsIndex + 3] = 0;
}
}
image.updatePixels();
} |
@jlp6k There isn't really nothing wrong with using global variables fundamentally, the only problem will be with variable name collisions but that will apply to all the global functions as well. However if you really want to avoid polluting the global namespace, you can have a look at instance mode and with it you can have the graphics buffer local only to the instance of p5 you are creating. I did some profiling with your updated code and although it seemed like My advice would be to use a smaller graphics buffer, the one you are creating now is 2000x2000 which is really big even for retina screens. Having said that, I will look into the source for some optimization, especially around calls to |
I'll probably be able to optimize the size of the hollowed/transparent shape. Thanks a lot for your investigations and advices. |
I don't want to create a new issue, if not necessary. I ran into the same issue today. I have a graphics context which I'm keeping and constantly add lines to. This context is then drawn onto the main canvas. As soon as I either draw the context using
@limzykenneth I read your statement that it is a quite big buffer. Maybe I'm missing something here, but should the buffer of that "size" really be almost 5GB and if so, how does it differ to the normal buffer used for the main canvas? Oh and in the p5 web editor the memory footprint is extremely small compared. About 250MB which would equate to 500max, assuming it is handled in the iFrame as 1x pixel density (is that the case?) Thanks |
@julian-weinert same here. |
Nature of issue?
Most appropriate sub-area of p5.js?
Which platform were you using when you encountered this?
Details about the bug:
The following code produces a memory leak when the
chromaKey()
function is called.I can't figure out what I may have done wrong except using the
graphicBuffer
- created with thecreateGraphics()
function - as a pixel buffer in thechromaKey()
function.Even if the browser crashes after a while, the code seems to work fine. The ellipse is hollowed and let see the canvas background through it.
The text was updated successfully, but these errors were encountered: