Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


android fluid frames and gc


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 fir   Members   -  Reputation: -460

Like
0Likes
Like

Posted 28 May 2014 - 11:18 AM

 
 
 
I am new to android Im watching some example source of simple game and there is stated
 
 
 This code is not as beauty as could be. It is optimized to avoid garbage collection
 
which would make the display updates stutter.
 
 
Does it mean that when you make game for android you have to do such things regularry? Is there some way of programming in java that let you not engage garbage collection ;/
 
 
 
sorry for banal question maybe but i am quite green here Im curious and it would be hard probably to get this answer with my zero experience 


Sponsor:

#2 SimonForsman   Crossbones+   -  Reputation: 6665

Like
2Likes
Like

Posted 28 May 2014 - 12:30 PM

Since you didn't post any actual code it is hard to give a proper answer but.... yes, when you make games using Java(and other languages with a hard to control GC) you want to write your code in a way that keeps the GC from kicking in during gameplay. Hold on to your references, re-use allready allocated objects instead of reallocating them, avoid modifying immutable objects(such as strings) and try to do your cleanup (drop references and force the GC to run) during your loading screens,


Edited by SimonForsman, 28 May 2014 - 12:31 PM.

I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#3 fir   Members   -  Reputation: -460

Like
0Likes
Like

Posted 28 May 2014 - 01:09 PM

Since you didn't post any actual code it is hard to give a proper answer but.... yes, when you make games using Java(and other languages with a hard to control GC) you want to write your code in a way that keeps the GC from kicking in during gameplay. Hold on to your references, re-use allready allocated objects instead of reallocating them, avoid modifying immutable objects(such as strings) and try to do your cleanup (drop references and force the GC to run) during your loading screens,

 

Isnt this something you do against the language? Im a bit schocked. Is it really possible to not involve garbage collection in java? I mean it would be something like c style writing in java ;/

 

I understand that procedural c-like way of writing in java on android is impossible because of heavy objective api, or this is also possoble?

 

(sad I got no experience here, come from couple of years in pure c/winapi world, maybe I will learn something about this very crazy world of android this year ;\)

 

PS as to source i just wathing some very simple random pice of android game code 

 

https://github.com/tinkerlog/PongTime/blob/master/src/com/tinkerlog/android/pongtime/PongTimeView.java

 

and trying to get know what is going on here; 

If someone would like volountinery explain me a bit what is goin on here it would be welcome too...


Edited by fir, 28 May 2014 - 01:21 PM.


#4 frob   Moderators   -  Reputation: 28197

Like
1Likes
Like

Posted 28 May 2014 - 01:23 PM

Java follows a different model than C.

As for the link, the source file seems self-explanatory. Precisely WHAT about it do you not understand?

If you are having trouble with the language broadly, a good old fashioned book on the subject is probably in order, followed with writing your own toy programs and learning by trial and error.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#5 fir   Members   -  Reputation: -460

Like
0Likes
Like

Posted 28 May 2014 - 01:49 PM

Java follows a different model than C.

As for the link, the source file seems self-explanatory. Precisely WHAT about it do you not understand?

If you are having trouble with the language broadly, a good old fashioned book on the subject is probably in order, followed with writing your own toy programs and learning by trial and error.

 

 

Answer coud be quicker ;/ In general I know (probably) most 

syntax of java and thiings but do not unrerstand at all the 

objective 'rush' and android api elements ;/

 

precise question? could be like "could you mention most important elements in this program (both program defined or androids api) and say to me how they interract  ?" I am lost in this :C I understand the View idea and some overrides, but not quite a idea of extending thread, (yet less other elements) and Im generally lost in code flow here


Edited by fir, 28 May 2014 - 01:52 PM.


#6 frob   Moderators   -  Reputation: 28197

Like
2Likes
Like

Posted 28 May 2014 - 03:13 PM

That code is not particularly complex. If you do not understand it, I suggest more study, more reading, more experimentation, and less posting on the forums.

You tend to ask questions on the forums before applying enough thought. Think first. Then experiment and think again.

The code you linked to is not particularly difficult to modify, and is very easy to modify and experiment with. I recommend you do more experimentation.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#7 SimonForsman   Crossbones+   -  Reputation: 6665

Like
1Likes
Like

Posted 29 May 2014 - 03:08 PM

 

Since you didn't post any actual code it is hard to give a proper answer but.... yes, when you make games using Java(and other languages with a hard to control GC) you want to write your code in a way that keeps the GC from kicking in during gameplay. Hold on to your references, re-use allready allocated objects instead of reallocating them, avoid modifying immutable objects(such as strings) and try to do your cleanup (drop references and force the GC to run) during your loading screens,

 

Isnt this something you do against the language? Im a bit schocked. Is it really possible to not involve garbage collection in java? I mean it would be something like c style writing in java ;/

 

I understand that procedural c-like way of writing in java on android is impossible because of heavy objective api, or this is also possoble?

 

(sad I got no experience here, come from couple of years in pure c/winapi world, maybe I will learn something about this very crazy world of android this year ;\)

 

PS as to source i just wathing some very simple random pice of android game code 

 

https://github.com/tinkerlog/PongTime/blob/master/src/com/tinkerlog/android/pongtime/PongTimeView.java

 

and trying to get know what is going on here; 

If someone would like volountinery explain me a bit what is goin on here it would be welcome too...

 

 

No, its not possible to "not involve" the garbage collector, but it is possible to keep it from running when you don't want it to run. (The GC will not run(or will atleast run really really quickly) if there is no garbage to clean up) and you can coax the GC to run when you want it to run (By asking it nicely and sleeping repeatedly until it runs).

 

That code being ugly has nothing to do with the GC though (for a simple game like pong the GC is irrelevant as you can create all your objects at startup and hold on to them until the program closes)


I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#8 fir   Members   -  Reputation: -460

Like
0Likes
Like

Posted 30 May 2014 - 02:19 AM

Specifically I do not understand this here, maybe someone culd explain it a bit 

 

 

 public PongTimeView(Context context, AttributeSet attrs) {
        super(context, attrs);
 
        // register our interest in hearing about changes to our surface
 
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);
 
        // create thread only; it's started in surfaceCreated()
        thread = new PongThread(holder);
        setFocusable(true); // make sure we get key events
    }
 
I understand a surfaceHolder is set of "in and out" methods you use to "react and act" on surface
 
 
 
but here it is passed to two objects to pongtimeView and also to pongThread - why is that so shouldnt just one object reach it and use it (isnt it a mess when botk object react or act on the same surfaceHolder methods and callbacks?) or for convenience this is done and pongtimeviev uses some methods and pongthread other methods?
 
yet some mysterious things - is there a need to create such sperate thread like here? cannot be this done in more classic way (more classic i mean for example program everything just in one "view" setting onlu a timer to some update method - like In javascript canvas games i tiny know )
 
also this is a bit mysterious
 
  SurfaceHolder holder = getHolder();
 
isnt it some violation of OOP rules (i know almost nothing about) to get some "holder" to some surface just from nothing, some global space? I do not understand this - how man shopuld understand it

Edited by fir, 30 May 2014 - 02:24 AM.


#9 Apptelope   Members   -  Reputation: 116

Like
1Likes
Like

Posted 09 June 2014 - 05:22 AM


PS as to source i just wathing some very simple random pice of android game code



https://github.com/tinkerlog/PongTime/blob/master/src/com/tinkerlog/android/pongtime/PongTimeView.java
 

 

There are integer divisions with no comments in that code. Always makes you wonder if it's on purpose.

 

The GC will cause problems if you allocate objects at each update. You can also get strange GC problems if you allocate a lot of memory.

 

However, my experience with Java and Android says that it's not the GC that is the big problem. The main problem is almost always with the resolution and correctness of the timer. I have one phone where the resolution of the timer is no more than 10ms. Also, the sum of small time intervalls can be different from the complete time interval.

 

What you also need to now is that the frame rate is fixed on most Android phones. You will probably get about 60Hz. In one of my new projects I've started to use that as a timer.

 

About that code example. There is no need to understand everything. Get the code to run first and then start to change it. Add features and so on.


Edited by Apptelope, 09 June 2014 - 12:22 PM.


#10 Olof Hedman   Crossbones+   -  Reputation: 3561

Like
1Likes
Like

Posted 09 June 2014 - 08:32 AM


Isnt this something you do against the language? Im a bit schocked. Is it really possible to not involve garbage collection in java? 

 

It's not that much stranger then avoiding to do unnecessary memory allocations and copies in your inner loops in C to increase performance.



#11 frob   Moderators   -  Reputation: 28197

Like
1Likes
Like

Posted 09 June 2014 - 10:08 AM


also this is a bit mysterious
 
  SurfaceHolder holder = getHolder();

It is exactly the same as it works in C, since you are already familiar with C.  It calls a function and stores the result. However, as Java works slightly different from C, it would be a SurfaceHolder pointer rather than a SurfaceHolder structure, but the concept is the same. Call a function, store the result.

 


isnt it some violation of OOP rules (i know almost nothing about) to get some "holder" to some surface just from nothing, some global space? I do not understand this - how man shopuld understand it

It calls a function that is within the current scope. It might be inside the class, it might be inside a parent class, it might be somewhere else.  All you need to know is that the compiler can find the function somewhere.

 

 

As for the rest, why doesn't the code follow some other design, those are bigger design questions. Even though it adds a tiny bit of complexity by requiring additional knowledge, it radically simplifies the design because components can be reused.

 

Even though it might be simpler in some ways to weld and epoxy everything together, people use bolts and screws and other standard parts because they can be easily reused.


Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS