What would happened if I allocate ofFbo in every frame

In my opinon, If there was something allocate every frame, eventually the program would run out of the memory.

But the truth is, even I allocate ofFbo in every frame

void ofApp::update()
{
	fbo.allocate(1000, 1000);
}

memory that this program costs didn’t increase, on the contrary as time goes on memory that this program didn’t change at all.

And after that I read the source code of ofFbo.

glGenFramebuffers(1, &fbo);

I guess this code causes this phenomenon, but I don’t know why.

since you are allocating memory for the same object each time (the variable fbo), it just keeps deallocating and reallocating the same memory each frame.

Also, I guess you should be checking your GPU’s memory instead of CPU’s.

Thx, I got it!

Oh thx !I would check that.

I think you’re going to destroy the universe (the only worse thing you could do is divide by zero…)

More seriously you should avoid allocating an FBO on every frame, I believe it’s pretty inefficient performance-wise. You may want to only clear your fbo if you just need a clean slate on every frame, or maybe you could have a pool of FBOs that you switch from (if you want to do something with the last n frames for instance). It definitely depends on what you’re doing, but I can barely see any case where allocating an FBO on every frame is the best approach…
As a rule of thumb the less you create/destroy objects in your main loop, the better your software will perform.

Actually, I’m curious about the way that fbo allocating.And at first, what I only want to do is just clear my fbo.But I found it interesting that the usage of my memory doesn’t change at all.So I’m quite curious about it.

I really agree with the idea of “a pool of FBOs”, but I havn’t tried that yet.

In fact, I really did the thing like divide by zero when I was learning C++…

You could totally look into the source code of oF to see what going on under the hood (with most dev environment, you can click on a function and ask to see its definition). The idea is the first thing allocate do is to destroy the previously stored fbo, if there was one. So that if you call allocate twice it won’t leak memory. On the other hand if you allocate your fbo on the stack (ie you have something like ofApp::update() { ofFbo myFbo; (…) } it will get automatically destroyed at the end of the update function.

I hope it makes sense !

1 Like

“The idea is the first thing allocate do is to destroy the previously stored fbo” after reading this passage, I completely understand this phenomenon, thx!