Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (529)
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  
  Making a new list with new objects every single frame  (Read 940 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Posted 2013-08-31 22:46:15 »

Hey guys.  Smiley

I'm working on a game for android that requires some processing of the data model, before it can be rendered correctly.
It's not much, but it requires me to assemble a new List every frame, with some list elements in it (between 4 and 500).
The element objects are very small convinience-things like the Vector3f, employing only 2 integers, and 2 floats.
The list assembly is essentially copying a similar list' (the raw model) important values, and doing some minor tweaking with some of the values. The minor tweaking is virtually nothing, in terms of CPU usage.
 
I'm afraid of the list assembly though. I have not been able to test my game on android yet, as I'm waiting for a new workstation to arrive, but the game runs smoothly on PC.
 
I have the following questions about performance in this situation.
Is it better to not reinitialize my list every frame, and just clear it?
Would I be far better off using an object pool for these small short-life objects?
Is there a chance the garbage collector in android is actually capable of cleaning this up, without showing lag?
Should I not worry about it, and get on with my game?

I'm quite new to the platform, so I don't know how well I have to manage object creation and garbage collection. I know that I can slack off quite a lot in this respect on the PC.
In the past, I have drawn the model directly so I never really had this problem. It could very well be that this exercise of making a few new objects to use for rendering is common practice, and I'm just stressing over something that hasn't been a problem in Java for a long time. Reading about memory fragmentation, and garbage collector lag makes me nervous, but it could be that those issues only are with huge datamodels, and not applicable in this case. Anyway, please clue me in!

Thanks for reading, and perhaps responding. Cheers!

Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #1 - Posted 2013-08-31 22:54:32 »

If its more likely to be 4-30 I'd say it doesn't matter.  If you expect 400-500 quite often, then I'd personally recommend an object pool.


However with that said.
"Should I not worry about it, and get on with my game?" <-- this, until its an issue

I wouldn't bother pre-optimizing it at all.  Why not just write it and if it works fine, move on.  Only come back to this when its an issue?


"Experience is what you get when you did not get what you wanted"
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #2 - Posted 2013-08-31 23:02:47 »

If its more likely to be 4-30 I'd say it doesn't matter.  If you expect 400-500 quite often, then I'd personally recommend an object pool.

It's for the length snake. 500 is possible, but really not something that would happen often. The assembly takes place only once every frame.

I wouldn't bother pre-optimizing it at all.  Why not just write it and if it works fine, move on.  Only come back to this when its an issue?

Alrighty then. I guess I'll throw in an FPS counter and take it from there. Assuming a pool is reasonably easy to implement at a later stage. Thanks!

EDIT: Even though this question is closed now, I'd still love to hear from you if using a Pool for these kind of things is something everybody does, and how bad exactly it is to rely on the garbage collector. Many different views and practices on this would be awesome. Has this ever affected any of your games a lot? Is it bad on android specifically?

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

Senior Newbie


Projects: 1



« Reply #3 - Posted 2013-11-03 13:52:21 »

I know this is a dead thread, but anyway.
Avoid doing new on android. Always use an object pool. Code is not complicated. Here is mine, that works well for mainly all cases :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
class Bucket<T>
{
   private final ArrayListEx<T>   elements   = new ArrayListEx<T> ();
   private int                  usedCount;
   private final Class<T>         classOfT;

   Bucket(Class<T> classOfT)
   {
      this.classOfT = classOfT;
   }

   Bucket(Class<T> classOfT, int initialCapacity)
   {
      this ( classOfT );
      reserve ( initialCapacity );
   }

   final void reserve(int capacity)
   {
      for (int i = 0; i < capacity; i++)
         elements.add ( newT () );
   }

   final int capacity()
   {
      return elements.size ();
   }

   final int size()
   {
      return usedCount;
   }

   final T get(int index)
   {
      return elements.get ( index );
   }

   final T add()
   {
      if (usedCount == elements.size ())
         elements.add ( newT () );

      final T obj = elements.get ( usedCount );
      usedCount++;

      return obj;
   }

   final int remove(int index)
   {
      elements.swap ( usedCount - 1, index );
      usedCount--;
      return index - 1;
   }

   final int remove(T obj)
   {
      final int index = elements.indexOf ( obj );
      return remove ( index );
   }

   final void remove(ArrayList<T> list)
   {
      final int count = list.size ();
      for (int i = 0; i < count; i++)
         remove ( list.get ( i ) );
   }

   final void clear()
   {
      usedCount = 0;
   }

   private final T newT()
   {
      try
      {
         return classOfT.newInstance ();
      }
      catch (Exception e)
      {
         return null;
      }
   }
}


Example of use :

1  
2  
3  
4  
5  
6  
7  
Bucket<Firework> sprites = new Bucket<Firework>(Firework.class);
...
Firework f = sprites.add();
f.setPosition(...); // reset all you need cause we are using recycled instances
f.setColor(...);
f.setVelocity(...);
...


I'm using here a ArrayListEx instead of a ArrayList, it is just the same class with some additionnal methods that you will rewrite (I can only see the swap method here).

Using a pool is really performance friendly for a very little cost and almost no drawback (except maybe the quick 'swap with last' method I'm using for removal, that changes order, which sometime is important).

Cheers.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

ctomni231 (30 views)
2014-07-18 06:55:21

Zero Volt (26 views)
2014-07-17 23:47:54

danieldean (22 views)
2014-07-17 23:41:23

MustardPeter (23 views)
2014-07-16 23:30:00

Cero (38 views)
2014-07-16 00:42:17

Riven (40 views)
2014-07-14 18:02:53

OpenGLShaders (27 views)
2014-07-14 16:23:47

Riven (27 views)
2014-07-14 11:51:35

quew8 (24 views)
2014-07-13 13:57:52

SHC (60 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!