Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  minimizing garbage collection  (Read 16059 times)
0 Members and 1 Guest are viewing this topic.
Offline princec

« JGO Spiffy Duke »


Medals: 434
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #30 - Posted 2007-07-20 10:34:16 »

Just remember one of the reasons the VM guarantees the memory is zeroed is to give you a known state for a new object - one of the classic reasons C programs go awry unexpectedly is having cruddy data in a struct. With pooling you lose that safety net and you're on your own again.

Cas Smiley

Offline keldon85

Senior Devvie


Medals: 1



« Reply #31 - Posted 2007-07-20 10:42:50 »

Just remember one of the reasons the VM guarantees the memory is zeroed is to give you a known state for a new object - one of the classic reasons C programs go awry unexpectedly is having cruddy data in a struct. With pooling you lose that safety net and you're on your own again.

Cas Smiley
But C guarantees the memory will be uncertain, and whatever you don't initialize may not be 0 Cheesy And 0 isn't a safety net if you don't wnat 0 either Smiley

Offline princec

« JGO Spiffy Duke »


Medals: 434
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #32 - Posted 2007-07-20 10:50:16 »

Hehe Smiley There is a subtle but big difference in having guaranteed default values, and that's repeatable crashes Smiley

Cas Smiley

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

Senior Devvie


Medals: 1



« Reply #33 - Posted 2007-07-20 11:11:55 »

Hehe Smiley There is a subtle but big difference in having guaranteed default values, and that's repeatable crashes Smiley

Cas Smiley
lol yes, funny but true. Like when beginners test their games in VBA and find that it doesn't work in No$ and real hardware - although that's almost the opposite.

Well the whole reason for me creating the FixedInt class was so that I could have integer precision and predictability without having to worry about the correctness of the arithmetic statements (i.e. shifting and performing operations with different point integers). Having said that I will probably change it to floating point anyway, but it did create a fine example of creating something that works over something that is fast (if you get what I mean)!

By the way -1>>1 = -1 Sad

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #34 - Posted 2007-07-21 00:12:31 »

>> duplicates the most-significant-bit
>>> inserts zeroes.

-1 >>> 1 = 2 billion

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline keldon85

Senior Devvie


Medals: 1



« Reply #35 - Posted 2007-07-21 00:32:26 »

>> duplicates the most-significant-bit
>>> inserts zeroes.

-1 >>> 1 = 2 billion
Yes, well the way to get around that (providing you want to divide) is to write "-(-b>>1)" when b is negative.

Offline emzic

Senior Devvie





« Reply #36 - Posted 2007-12-29 17:05:35 »

i just read through this whole thread again, but i still do not fully understand.

so will object-pooling increase performance when i need  thousands of new Vector3fs in each gameloop?

www.embege.com - personal website
webstart blendinspect - OpenGL BlendingModes visualization.
Offline DzzD
« Reply #37 - Posted 2007-12-29 18:25:47 »

basically because it is costless to get an existing object, and it is cpu expansive to allocate (release when GC) memory for new object (even for one byte), when creating a new Object you ask the target OS RAM manager or java memory manager to give you an heap space, this is slow in comparaison of getting an existing memory allocated place, so using a pool for object this is what you do by getting a "vector" to an already allocated memory area. this is why it it will be always faster in any language to use pooled objects.

Offline emzic

Senior Devvie





« Reply #38 - Posted 2007-12-29 21:10:35 »

thanks for the info.
so short answer is yes, in this situation pooling will be better?

www.embege.com - personal website
webstart blendinspect - OpenGL BlendingModes visualization.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #39 - Posted 2007-12-29 21:44:13 »

As always with performance, it depends on your implementation.


Just implement it, then tweak it, then compare it to creating new objects, then pick the fastest.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DzzD
« Reply #40 - Posted 2007-12-29 22:25:58 »

Quote
As always with performance, it depends on your implementation.

this a general good way, and you may try it.

but, in this special case, there will be no surprise, pooling will be faster especially for a lot of objects. Memory managment works exacly the same as hard disk managment, including fragmentation and such, so even if Java GC run it best and Java memory manager is excellent and underlying OS memory manager is excellent, it cant be faster to allocate object than reusing already existing object. reusing object is near to be cpu costless, it only imply reading an object reference wich is near to read a "pointer", maybe only 2/4 cpu cycles.

If you have time, make a simple test case (a simple loop allocating object and doing some computation on them and do the same with prealocated objects) an printout both bench result, I guess you will find a huge difference.

thinking of the general rule of cpu use 80/20, that basically explain that most of the source code 80% doesn't help a lot in being optimised as most cpu is used in 20% of your code, you dont have to care of pooling if it is outside of the 20% code that use most of your cpu.

you can use -xprof option to identify the code that use the most of your cpu and se if you really need optimising this part by pooling,  low level optimisation should be used carefully and must  be done as later as possible in your project.

Offline emzic

Senior Devvie





« Reply #41 - Posted 2008-01-02 16:54:00 »

ok, thanks a lot everybody for your information.
you have convinced me to give it a try, which leads me to the next question: how to implement object pooling?

especially how do i find out, if an object isnt needed anymore? (referencecount == 0)

www.embege.com - personal website
webstart blendinspect - OpenGL BlendingModes visualization.
Offline bienator

Senior Devvie




OutOfCoffeeException


« Reply #42 - Posted 2008-01-02 18:05:52 »

especially how do i find out, if an object isnt needed anymore? (referencecount == 0)
Don't reimplement the GC! If you want fast pooling keep it simple as possible. Put objects into the pool if you are sure that there are not in use any more. Anything else will run slower than regular new/GC cycles.

I use pools for the storage of Triangle objects for terrain triangulation. Without the pool I get regular full (stop the world) GCs every 6 seconds. With the pool enabled the GC is triggered concurrently without stops and enables smooth rendering (60+ fps). And don't use pools for a small amount of objects (my initial pool size is around 1 million triangles).

Offline emzic

Senior Devvie





« Reply #43 - Posted 2008-01-02 19:15:18 »

Put objects into the pool if you are sure that there are not in use any more.
yeah, but how do i know that?

and what is a good datastructure for object pools? java.util.HashSet ?

thanks!

www.embege.com - personal website
webstart blendinspect - OpenGL BlendingModes visualization.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #44 - Posted 2008-01-02 19:18:08 »

Not tested, not compiled:


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  
public interface Supply<T>
{
   public T create();
}


public class Pool<T>
{
   private final Supply supply;
   private final int max;
   private final List<T> cache;

   public Pool(Supply supply, int max)
   {
      this.supply = supply;
      this.max = max;
      this.cache = new ArrayList<T>();
   }

   public final T grab()
   {
      if(cache.isEmpty())
         return supply.create();
      return cache.remove(cache.size() - 1);
   }

   public void dump(T t)
   {
      if(cache.size() < max)
         cache.add(t);
   }
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #45 - Posted 2008-01-02 19:26:40 »

yeah, but how do i know that?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
Vec3 a = new Vec3(1,2,3);
Vec3 b = new Vec3(3,2,1);
Vec3 tmp = pool.grab();

Vec3.cross(a, b, tmp);
float val = a.dot(tmp);

// we can be sure here that 'tmp' will not be used anymore
pool.dump(tmp);

// it will only break seriously when either cross() or dot() store the reference
// of 'tmp' somewhere, but we can reasonably assume that's not happening

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline emzic

Senior Devvie





« Reply #46 - Posted 2008-01-02 19:39:50 »

thank you riven!

so that means, i will need to figure out myself when an object isnt needed anymore. (ok that was obvious).

actually that is quite a shock, since i am doing vector math all over the place in my engine and i will need to insert the pool.dump(tmp) everywhere...

hmmm.... is it still worth the effort?

www.embege.com - personal website
webstart blendinspect - OpenGL BlendingModes visualization.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #47 - Posted 2008-01-02 19:40:54 »

only pool objects where it matters, so that doesn't mean you should do it everywhere.

just pool the bottlenecks.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline emzic

Senior Devvie





« Reply #48 - Posted 2008-01-02 19:49:08 »

ok thanks again for all your help. i'm now off to implementing it, then testing it, then i will report here with the results.

www.embege.com - personal website
webstart blendinspect - OpenGL BlendingModes visualization.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #49 - Posted 2008-01-02 20:15:36 »

Keep in mind that the provided code is NOT threadsafe.

Do NOT access the same Pool from more than 1 thread. Never.


Synchronizing the methods will pretty much destroy your preformance-gains.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline emzic

Senior Devvie





« Reply #50 - Posted 2008-01-05 16:41:41 »

ok, according to my profiler i got garbage collection down a lot! the minor GCs happen now only every 3 seconds as opposed to 3 times a second before. if that has an impact on the overall performance i do not know. probably on slower machines yes.

www.embege.com - personal website
webstart blendinspect - OpenGL BlendingModes visualization.
Offline Abuse

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #51 - Posted 2008-01-15 20:19:52 »

Tbh, I wouldn't introduce object pooling at the source level at all.
You are compromising the design integrity of your source-code to accomodate for a performance limitation of the current breed of VMs.
What do you do when the next VM comes along, and your object pooling turns out to now be the performance bottleneck?

A bytecode engineering solution to compliament the capabilities of the VM's compiler would be a much cleaner, reusable & more scalable solution.

While it isn't a trivial problem to solve, it isn't beyond the realms of imagination (No doubt it would be borrowing many aspects from the miriad of optimising compilers that already exist)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #52 - Posted 2008-01-15 22:04:21 »

I totally agree with you, but...

I want performance now... it makes my code 2-3x faster, in a few minutes for refactorying my 'ideal' sourcecode.

I don't have the time to build that bytecode-transformer. Keep in mind that such a transformer would be almost impossible to get right, as the developer knows when the Object is ready to reuse, yet the transformer _cannot_ analyse that. Or you'd be building yet another GC...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline abies

Senior Devvie





« Reply #53 - Posted 2008-01-27 19:56:28 »

There is object pooling and object pooling. Not every case is just about saving gc, sometimes it is about saving memory. 'Pooling' immutable objects has a nice side effect that you won't end up with millions of instances of the same (same like in 'equals returning true') object in jvm. After all, java.lang.Integer.valueOf(int) implements a small pool itself, so it cannot be THAT bad, can it ? Wink

As far as claiming that gc will solve all your problems - it is not exactly true. If you generate a LOT of immediate garbage, you will invoke gc pauses more often. In every gc, some part of life objects will be copied here and there (at least before they mature enough to hit old generation) - which is costly operation. So, don't sacrifice your app logic for gc, but also don't allocate things just because they are 'free'.

I'm doing a lot of performance sensitive code these days and when you hit 8+GB heaps and cannot afford more than 50ms pauses and cannot use NewParallelGC (because it crashes 100% with our app within 4 hours), one because bit more careful about garbage allocation.

Artur Biesiadowski
Offline bienator

Senior Devvie




OutOfCoffeeException


« Reply #54 - Posted 2008-01-27 21:06:31 »

There is object pooling and object pooling. Not every case is just about saving gc, sometimes it is about saving memory. 'Pooling' immutable objects has a nice side effect that you won't end up with millions of instances of the same (same like in 'equals returning true') object in jvm. After all, java.lang.Integer.valueOf(int) implements a small pool itself, so it cannot be THAT bad, can it ? Wink

As far as claiming that gc will solve all your problems - it is not exactly true. If you generate a LOT of immediate garbage, you will invoke gc pauses more often. In every gc, some part of life objects will be copied here and there (at least before they mature enough to hit old generation) - which is costly operation. So, don't sacrifice your app logic for gc, but also don't allocate things just because they are 'free'.
100% agreed. [offtopic] Immutable objects have even another cool side effect you need 0 synchronisation if you work with multiple threads. Scala uses even immutable HashMaps...[/offtopic]
I'm doing a lot of performance sensitive code these days and when you hit 8+GB heaps and cannot afford more than 50ms pauses and cannot use NewParallelGC (because it crashes 100% with our app within 4 hours), one because bit more careful about garbage allocation.
8GB Heap and 50ms GC pauses? Thats awesome! I never thought that the GC would scale so good. My engine uses currently around 1 gig of RAMfull of small objects and is at a point where even parallel young GCs take >100ms (and full GCs would take > 2 seconds without pooling). Now I decided to move from dynamic resizing pools to static pre-allocated pools that fixed that problem (0 allocation or deallocations, yeha!).

 But good to know that there is still room left  Grin

(Am I the only one who noticed that every VM/GC performance white paper tries to advice against pooling?)

Offline abies

Senior Devvie





« Reply #55 - Posted 2008-01-27 21:35:39 »

8GB Heap and 50ms GC pauses? Thats awesome! I never thought that the GC would scale so good.

Of course we use CMS, with only 48MB new generation, rest being done periodically in background by CMS.

Artur Biesiadowski
Offline emzic

Senior Devvie





« Reply #56 - Posted 2008-02-17 10:13:47 »

Of course we use CMS, with only 48MB new generation, rest being done periodically in background by CMS.
may i ask, what is CMS? Smiley

www.embege.com - personal website
webstart blendinspect - OpenGL BlendingModes visualization.
Offline bienator

Senior Devvie




OutOfCoffeeException


« Reply #57 - Posted 2008-02-17 15:29:24 »

may i ask, what is CMS? Smiley

this is the new Concurrent Mark and Sweep Collector introduced with Java SE 6. It uses all available cores to clean the young generation and tries to do most of the work in the tenured generation concurrently (-> while your app is runnig).

Its primary aim is to prevent the evil full stops (also known as full GCs).

great overview over all Garbage Collectors:
http://blogs.sun.com/jonthecollector/entry/our_collectors

there is also a new concurrent GC planed for Java 7 called Garbage First I have aggregated a interesting discussion on my blog:
http://www.michael-bien.com/roller/mbien/entry/garbage_first_the_new_concurrent

Pages: 1 [2]
  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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (105 views)
2014-11-26 15:20:36

toopeicgaming1999 (31 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!