0
\$\begingroup\$

I new to both Java and game development and working on my first LibGDX game targeting mobile systems.

How problematic is the garbage collector in a mobile environment? Should I try to reuse as many class instances as possible (e.g. with the object pool pattern), or only do it for the classes that I use most (like projectiles)?

How what instantiation rate warrants this optimisation? Or is the compiler “sufficiently smart” to effectively nullify this problem?

\$\endgroup\$
7
  • \$\begingroup\$ By spawning classes do you possibly mean spawing objects ? 50 doesn't really say much. It depends on how many variables there are in your class. In the end, you can always just test and see if its efficient or not. \$\endgroup\$
    – dimitris93
    Jun 20, 2015 at 16:22
  • \$\begingroup\$ Assuming the classes aren't huge internally then 50 in a minute is nothing, 500 in a second would also likely be nothing but would start to be worth thinking about (although the GC on most JVMs is very good and usually isn't the performance bottleneck) \$\endgroup\$ Jun 20, 2015 at 17:37
  • \$\begingroup\$ Yup i meant Objects, sorry :P And yes, they wont have many properties and methos. Even though the projectiles aren't going to be that many, since they all use the same class with different properties I will probably recycle them. Thank you \$\endgroup\$
    – Fiochkij
    Jun 20, 2015 at 18:19
  • \$\begingroup\$ I don't think you completely understand what classes are. Lets say we have a class of students with properties double Weight and int Age and 250 methods. If we instantiate 2000 students, the number of bytes in RAM we allocate is 2000*(8 + 4) bytes. I don't understand what you mean by "recycling classes". There is no such thing. Each time you use the new keyword, a new object is made. \$\endgroup\$
    – dimitris93
    Jun 20, 2015 at 19:13
  • 1
    \$\begingroup\$ You might want to take a look into the object pool design pattern. \$\endgroup\$
    – Vaillancourt
    Jun 20, 2015 at 19:39

1 Answer 1

2
\$\begingroup\$

A class is a chunk of code that can be reused as an object. An object is a single instance of something that can have certain properties (e.g. an integer representing a characters intelligence or money amount). Now that we have that settled...

Going light on object creation and heavy on object reuse is generally better. It uses significantly less RAM to stuff your 50 objects into an array and spit them out and recycle them when needed than it does to create a new instance of said object. Every single object uses RAM (albeit, usually small chunks, but Android devices dont have much to begin with) and the more seperate instances you have the more lag appears.

OpenGL based game engines (libGdx, anyone?) are generally pretty good on memory management, but abuse of object creation will still trigger the garbage collector. If you are using the Android Canvas class with bitmaps to make your game, you have to be extremely careful on how you use available memory.

Your typical song can be 20 mb. Some of the lower end phones have 50 mb of memory available to each app. Creating 50 instances of a song will cause an out of memory exception(OOE). Creating 50 instances of a object with a 1 mb image file will also cause this exception.

I suggest that you check out the "For Dummies" series of books. This is not an insult, as some would take it if I left it at that, but these books are generally very descriptive and you will have the basics of Java down in no time.

1.) Java for Dummies

2.)Java Game Programming for Dummies

3.) Android Application Development for Dummies

4.) Android Game Programming for Dummies

5.) Beginning Android Games

After you have read these books (in the order given if you have any hope of understanding more advanced concepts) you are ready to either make your own game from nothing or to use a game engine. After reading these I believe you will be better suited for learning libGdx game development since the last book essentially shows you how it operates under the hood. After that read these tutorials and your good to go!

\$\endgroup\$
3
  • \$\begingroup\$ Good explanation. Also very impressive profile if you are actually 15 years old. \$\endgroup\$
    – dimitris93
    Jun 20, 2015 at 22:03
  • \$\begingroup\$ Thanks for tour help, by no means i tale the "for dummies" as an insult since i learnt a lot from them ^^. I can code in a few languages but java is new to me. My project is a porting from UE4 cause it's too heavy for what i need. I used it to prototype the game and get my hands dirty on it ^^~ \$\endgroup\$
    – Fiochkij
    Jun 20, 2015 at 22:52
  • \$\begingroup\$ @AndreaFiocchi I if you want to know how libGdx works under the hood (which improves understanding and ability) you should follow the Beginning Android Games link. If I am correct UE4 uses C++. Shouldnt be to hard of a transfer. Java is less strict on the order of things (static variables and regular variables do not have to be in any specific order). But is harder to manage memory. I wish you luck :) \$\endgroup\$
    – Jax
    Jun 22, 2015 at 12:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .