10

Do I have to clean up all DisplayLists, Textures, (Geometry-)Shaders and so on by hand via the glDelete* functions, or does the GPU mem get freed automagically when my Program exits/crashes?

Note: GPU mem refers to dedicated memory on a dedicated Graphics card, not CPU memory.

genpfault
  • 51,148
  • 11
  • 85
  • 139
drahnr
  • 6,782
  • 5
  • 48
  • 75
  • Depends on your definition of "have to". You don't "have to" do anything. If you want to be "correct", yes; you need to always release your resources when you're done. – GManNickG Feb 06 '10 at 23:14
  • I rather wanted to know if it will get cleanup up automatically, *shortly* after the Program closes, or if the GPU memory is lost till the next reboot. I hoped someone could point me towards OpenGL specification and it was defined there (though my favorite search engine didn't spit anything usefull at me). – drahnr Feb 06 '10 at 23:29
  • Both! Really, clean up after yourself, even if the OS does it for you. You don't need to ever `delete` any `new`ed memory either, the OS will just free it when the program ends, but still that's not something I'd suggest. – Christian Rau Jun 21 '13 at 08:05

5 Answers5

17

Free the context, everything else is local to the context (unless you enabled display list sharing) and will go away along with it.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Could you help to comment what if wglDeleteContext does not release all memory ? – SOUser Feb 26 '18 at 15:30
  • @SOUser: Did you enable resource sharing between contexts? Then resources will be freed when the last of the linked contexts is deleted. If you have evidence that deleting the context isn't cleaning up, file a bug with the GPU driver. – Ben Voigt Feb 26 '18 at 15:44
12

As others mentioned, your OS (in collaboration with the driver resource manager) should release the resources. That's what OSes are for. It's worth noting that this has nothing to do with OpenGL, but is something that is part of the charter of well behaved OSes and their associated drivers. The OS is there to handle all the system resources. OpenGL ones are just but a subset of them, and they are no different from, say a file handle. Now to get more concrete, you should specify which OS you care about.

BTW, This is where I take exception with ChrisF's answer. It should not be up to the driver to decide it needs to do cleanup. OS driver models will have a clear interface between the user-mode OpenGL driver (that shouldn't do actual gfx resource allocation, since it's shared in the machine), the OS (that provides the equivalent of system calls to allocate resources) and the kernel-mode driver (that is merely there to execute the OS orders in a way that is compatible with the gpu). This is at least the case with the WIN2K and WDDM models.

So... if your process crashes or otherwise terminates, in those models, it's the OS responsibility to call the kernel-mode driver to free all the resources that were associated with the process.

Now, whether you should or not is really something that is a little like asking tabs-or-spaces in source code. Different people have different beliefs here. "the OS will do it anyways, quitting immediately is a better end-user experience" vs "I want to know whether I'm leaking memory because if my program is long-running, I really don't want it to hit OOM errors. Best way to do that is to be leak-free throughout" are the 2 main lines of thought I'm aware of.

Bahbar
  • 17,760
  • 43
  • 62
  • Note that you are contradicting yourself. Yes, the OS sends an event to the driver when cleanup is needed... but it is still the driver that does the actual work, and failure to clean up is the fault of a buggy driver. – Ben Voigt Feb 26 '18 at 15:49
  • @BenVoigt: tried to clarify what I meant. Is that better? – Bahbar Feb 28 '18 at 23:04
2

When your program exits (or crashes) then any memory it currently has allocated should be freed eventually in the same way that main memory is usually freed when a program exits. It may be some time before the GPU "realises" that the memory is available for use again.

However, you shouldn't rely on this behaviour as it may depend on how the graphics card drivers have been implemented. It's far better to make explicit clean up calls when you (as programmer) know that you won't need that memory again.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
0

All your GPU resources will be released when your program exits. An easy way to test is to not delete things, and run your app repeatedly to see if it fails the allocations after a few iterations.

Alan
  • 4,897
  • 2
  • 24
  • 17
-1

In Opengl, there is no memory to store the drawing information. here, when we execute the opengl program, that time calling draw frame method calling at sequentially. Anyway, if we draw a line or circle means, anytime its calling draw frame method for drawing at specified place.but, opengl does not store that line in memory. Anytime, its drawing only. but, when we saw, that line is dawned successfully.

Ex: In Android Opengl es2.0 used this renderer class inside drawframe method(inside draw method) to draw the lines or circles etc.. i used this Opengl es2.0 Program in android autocad app development.. if you want to clear the dawned lines use this method in renderer class inside onDrawframe method GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

harikrishnan
  • 1,985
  • 4
  • 32
  • 63
  • Well, of course it doesn't store any drawn geometry, but it stores a whole bunch of other things, both in CPU and GPU memory. Best example, where is that line drawn to and why can you see it? – Christian Rau Jun 21 '13 at 08:09
  • Hi Christian Rau, you are correct. I said about GPU Memory storage only.. I am facing problem for above mentioned. please see this link and if possible please help me from this struggle. http://stackoverflow.com/questions/17187032/why-my-opengl-output-differs-for-various-devices Please see this sample code also from this link http://tuxbalaji.wordpress.com/2013/06/19/draw-a-line-using-opengles2-0-by-touch-event-in-android/ – harikrishnan Jun 21 '13 at 09:37