Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Do you need to dipose of stuff when exiting application?  (Read 1042 times)
0 Members and 1 Guest are viewing this topic.
Offline trollwarrior1
« Posted 2014-01-14 11:55:15 »

So, I never really dispose of anything. Is that a bad idea not to dispose of stuff? I mean, doesn't OS or OpenGL automatically do that for me?

I just started messing around with libgdx and noticed that "dispose" method which disposes of texture and batch. So do I need to manually dispose stuff upon application exit?
Offline Rickmeister

Senior Member


Medals: 9
Projects: 1
Exp: 15 years


Don't eat yellow snow!


« Reply #1 - Posted 2014-01-14 13:09:26 »

Resources allocated by native code are not always visible to the JRE, and if you don't dispose/release them back to the system you will cause memory leaks. If the Garbage Collector isn't aware of objects, it's not able to process them. Never rely on the OS to take care of your leaks, it's just bad practice.
Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2014-01-14 13:55:24 »

Er, opposite stance here. The OS releases everything when the process exits. Save yourself the hassle and just let it do it for you.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Sickan

Senior Member


Medals: 8



« Reply #3 - Posted 2014-01-14 14:02:24 »

Er, opposite stance here. The OS releases everything when the process exits. Save yourself the hassle and just let it do it for you.

Cas Smiley
Yes - it makes the OS more robust. If it didn't, our computers would be at the mercy of C programmers and their memory management.

I'm just here to learn.
Offline Roquen
« Reply #4 - Posted 2014-01-14 14:04:30 »

Since we're talking about stuff stored in VRAM instead of virtual memory...you have dispose of it yourself.
Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2014-01-14 15:39:55 »

Process exits -> OS destroys OpenGL context -> driver frees VRAM.

Cas Smiley

Offline Roquen
« Reply #6 - Posted 2014-01-14 15:56:56 »

Me bad...no read title
Offline Oskuro

JGO Coder


Medals: 32
Exp: 6 years


Coding in Style


« Reply #7 - Posted 2014-01-14 15:58:20 »

I'm guessing then that the disposal methods are there in case you need to clean up memory while still running the process?

Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #8 - Posted 2014-01-14 16:03:20 »

Yes, that's right. When it comes to games, it is unlikely you'll ever need to worry about it.

Cas Smiley

Offline gouessej

« In padded room »



TUER


« Reply #9 - Posted 2014-01-14 20:41:09 »

Personally, I destroy direct NIO buffers when I no longer need them, for example when I go from a level to another one. If I didn't do that, I would run out of native memory. I will probably have to go that in one of my applications too.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #10 - Posted 2014-01-15 10:36:58 »

You should really just set them up at app startup and leave them.

Cas Smiley

Offline gouessej

« In padded room »



TUER


« Reply #11 - Posted 2014-01-15 11:02:19 »

If I do so, I'll have to increase a lot the maximum size of the direct memory (-XX:MaxDirectMemorySize) except if I succeed in reusing large direct NIO buffers. Maybe I see what you mean.

Offline theagentd
« Reply #12 - Posted 2014-01-15 12:08:34 »

Personally, I destroy direct NIO buffers when I no longer need them, for example when I go from a level to another one.
How do you even do that?

Myomyomyo.
Offline gouessej

« In padded room »



TUER


« Reply #13 - Posted 2014-01-15 12:54:06 »

As I use Ardor3D, I know the root node of the game "state" (Fettle API), I visit all its children. When I find a leaf node, I look at its mesh data which contain several direct NIO buffers for vertices, colors, texture coordinates, fog, ... Then I have to destroy the both parts of these resources, i.e the data stores on the GPU (by using glDeleteBuffers) and the direct NIO buffers not allocated on the GPU (I don't use glMapBuffer/glUnmapBuffer). I use the cleaner of each direct NIO buffer to release its native memory in the meantime (a public API will be added into Java 1.9 to do that without relying on classes that might be removed).

References:
http://ardor3d.forumatic.com/viewtopic.php?f=10&t=5182&hilit=NIO&start=10#p25407

Offline seismic

Senior Member


Medals: 3
Projects: 1



« Reply #14 - Posted 2014-01-15 13:39:26 »

Same as cas. Don't care about disposing stuff. That's the job of the modern OS' Smiley

If it were that important I couldn't kill processes which don't support Alt+F4. Well I could, but not for long then Cheesy
Offline gouessej

« In padded room »



TUER


« Reply #15 - Posted 2014-01-15 14:11:14 »

Disposing stuff when exiting an application doesn't seem to be mandatory (except maybe in some particular cases) but disposing stuffs that use some memory on the native heap to avoid running out of native memory at runtime isn't a bad idea.

Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #16 - Posted 2014-01-15 14:35:06 »

There aren't many use cases for creating and destroying native byte buffers. Typically what you should be doing is re-using them. For example, loading textures in a "fast" way: you have one native buffer big enough for any texture you're going to use; then you repeatedly load your texture data from disk into that one buffer to send it to OpenGL.

Cas Smiley

Offline gouessej

« In padded room »



TUER


« Reply #17 - Posted 2014-01-15 17:52:22 »

I could create a single very big direct NIO byte buffer as the very beginning and slice it, it would eliminate the need of destroying any direct buffer. It is doable when you have the full control on the whole software stack but not in several major scenegraphs which encapsulate the creation of those direct NIO buffers. They have no mechanism allowing to override that, either they already have a manual build-in feature for the disposal of native resources that you may use at your own risk (JMonkeyEngine 3, LibGDX?) or they are "smart" enough to do it automatically partially (only the OpenGL part) when the garbage collector takes care of some buffers, they let you extend this mechanism and call it whenever you want which is useful when you run out of native memory before running out of Java memory (Ardor3D).

Offline StumpyStrust
« Reply #18 - Posted 2014-01-15 18:09:25 »

I did quite a bit of research on the whole Nio bytebuffers things and found that they actually do get garbage collected but only when the normal stack/heap fill up. This means that because they are allocated natively they are not seen as taking much up and do not add anything when a garbage requests happens and thus do not get cleaned up.

Offline gouessej

« In padded room »



TUER


« Reply #19 - Posted 2014-01-15 23:08:26 »

You're right but it concerns only direct NIO buffers, indirect NIO buffers are allocated on the Java heap, I explained that here, here and several times on JGO too.

Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (28 views)
2014-04-15 18:08:23

BurntPizza (25 views)
2014-04-15 03:46:01

UprightPath (40 views)
2014-04-14 17:39:50

UprightPath (22 views)
2014-04-14 17:35:47

Porlus (38 views)
2014-04-14 15:48:38

tom_mai78101 (62 views)
2014-04-10 04:04:31

BurntPizza (121 views)
2014-04-08 23:06:04

tom_mai78101 (221 views)
2014-04-05 13:34:39

trollwarrior1 (188 views)
2014-04-04 12:06:45

CJLetsGame (195 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!