0

I load my assets in create and dispose them pause.

But when a game is closed, the process may or may not terminate. i.e., it could be just the activity that got stopped and not the process. In this case, when the game is quickly reopened, create will not be called. Only resume will be called. Then, shouldn't I be reloading my assets in resume also?

batman
  • 5,022
  • 11
  • 52
  • 82

3 Answers3

0

The initialization using life cycle methods of an activity in Android goes like this:

  • onCreate() -> onDestroy()
  • onStart() -> onStop()
  • onResume() -> onPause()

Meaning that, for example, what I init in onCreate, I should get rid off in onDestroy. And same for all three couple of methods.

In your case, I would load the assets in onStart and dispose them in onStop.

Also, please note that onDestroy method may not be called.

DDsix
  • 1,966
  • 1
  • 18
  • 23
  • but that doesn't answer the question. when Android releases Assets automatically when the process is killed, why should I dispose it? – batman May 26 '15 at 10:03
  • Then don't dispose them. However, you may get an OutOfMemory exception which in fact will cause your process to be killed. – DDsix May 26 '15 at 10:19
  • Also, the question was "When should I load and dispose assets?", and I believe I answered to that exact question – DDsix May 26 '15 at 10:20
  • sorry about the question, I edited it slightly. Why would it give me an OOM exception? It's not like I'm continuously allocated assets in my game. Why would it ever run out of memory. I allocate assets once in `create`. Then if I fail to dispose it, Android will take care of it. When I open the game again, it should work normally as before, right? Am I missing something? I would understand OOM if I kept allocating assets in my render loop or something. – batman May 26 '15 at 14:02
  • I think the confusion here is because the question is about Libgdx, which abstracts away the Android-specific life cycle stuff. The question is specifically about when to dispose of assets in the Libgdx life cycle. – Tenfour04 May 26 '15 at 14:26
0

not if I understand your question, if you ask why? should the dispose-law, it is common that android is responsible for releasing the resources, lidGDX.

in some cases it creates objects out of reach of the garbage collector, of the JVM, these objects are created from native side of the application and GC does not have the ability to remove them, so you have to call the objects that have dispose not all objects have the need to use the dipose, here you can see a list:

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Disposable.html

It may also be part of this answer helps you:

Game crash if interrupted while Splash Screen is on - LIBGDX

I apologize for my English, and if this question does not help you tell me and delete

Community
  • 1
  • 1
Angel Angel
  • 19,670
  • 29
  • 79
  • 105
  • Thanks for the answer. But I don't quite understand your first sentence. My question is, why do I need to dispose objects at all when Android will do it after the game exits? It's not a question of continuous memory leak while the game runs. This is a one time thing. If I forget to dispose asset memory, Android will do it. Next time the game is opened, everything is good and fresh. – batman May 26 '15 at 13:58
0

Dispose of your assets in dispose() or when you are done with them and need to free up memory (such as switching to a new stage that uses different assets).

The game's dispose method is called in the Activity's onPause() method, but only if the Activity is being finished, so you are safe if you only dispose in dispose(). If the application process remains open, dispose() has been called, so you're good. And at this point, if your game reopens, create() will be called again so you don't need to worry about reloading in resume().

If for some reason Android force-quits your game, then all the memory is cleared up anyway and you have no need to worry about leaks.

And the reason why you must dispose in dispose() is for cases where the user backs out of your game Activity but the Application process is not shut down. If the user launches the game Activity again, all the non-disposed assets from the previous instance are still held in memory and have now leaked.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154