Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
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  
  So I use System.gc() manually, is it really that bad ?  (Read 2925 times)
0 Members and 1 Guest are viewing this topic.
Offline Cero
« Posted 2011-07-30 22:14:19 »

If I call System.gc() during map-changes. After unloading and loading is done, before the new map is on screen, I call System.gc().
Whats so bad about it, in a place when I'm expecting a short loading anyway ?

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2011-07-30 22:18:11 »

If you don't care about the slight slow down, there is nothing bad at all.

Offline kevglass

JGO Kernel


Medals: 85
Projects: 25


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #2 - Posted 2011-07-30 22:31:12 »

Thats not the point, System.gc() doesn't guarantee it will do a garbage collection:

Quote
Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

Note the "suggests" and "expend effort" - it doesn't really guarantee that GC will happen, though in practice it mostly does. The advice not to use it is based on it not being something you can rely on actually doing anything.

Kev

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

JGO Coder


Medals: 10



« Reply #3 - Posted 2011-07-30 22:32:47 »

If I call System.gc() during map-changes. After unloading and loading is done, before the new map is on screen, I call System.gc().
Whats so bad about it, in a place when I'm expecting a short loading anyway ?
There is no single reason why you should never call it, but there are three issues which are related. First some people presume/rely on it always running, but there are garbage collectors that will simply ignore System.gc, such as those on a phone. JVM garbage collectors are also moving away from the simple 'stop the world' model (although they do need to stop it at certain points), and more towards running some of the work in parallel. Essentially when you call it, you don't know what will happen!

The second issue is that it can be an indicator of bad code; such as excessive amounts of object creation. It is much better to solve the issue, in that example re-using objects or just creating less, rather then presuming System.gc will solve your problems.

The third issue is that it can reduce performance because you are asking the JVM to garbage collect more often, where it only ends up collecting a small proportion of objects. Typically leaving it up to the JVM to decide when to GC yields better performance (but not always better responsiveness).

To sum it up, generally it is seen that if you think you need to call System.gc then your program is probably flawed. However with the example you have given, I don't see any real reason why you should not to call it.

Offline Cero
« Reply #4 - Posted 2011-07-30 23:17:27 »

Yeah well I don't rely on it. It's just a good moment for a garbage collection... and if it doesn't happen, doesn't matter
I would actually call it a mark for a moment in code when garbage collection would be nice

I don't have memory issues or experience slow downs whenever the gc runs automatically or anything like that.
I just thought that this would be the perfect moment to gc, as all the objects and the map are being unload and load and stuff.

Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2011-07-31 01:40:18 »

Seeing as the JRE can collect about a gig of garbage in under half a second on most machines now it's hardly really worth doing it... just let it take care of itself I think. No harm in doing it between levels but... yeah, what's the point, really...

Cas Smiley

Offline Mads

JGO Ninja


Medals: 24
Projects: 3


One for all!


« Reply #6 - Posted 2011-07-31 09:40:29 »

While we're on the topic, what applications use the System.gc() call?  persecutioncomplex When would you ever need that, seeing as it runs itself.

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #7 - Posted 2011-07-31 13:31:03 »

I say System.gc() is mostly used when you create a lot of objects and then, like changing levels, need to get rid of them fast because you're about to create a lot of new objects. However remember that System.gc() doesn't actually run the GC but expends effort toward garbage collection.

Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #8 - Posted 2011-07-31 14:02:01 »

I wouldn't give you a point in an exam if you answered a question like that Smiley

Cas Smiley

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #9 - Posted 2011-07-31 14:03:23 »

I wouldn't give you a point in an exam if you answered a question like that Smiley

Cas Smiley
So does that mean I got 100%? Cheesy

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

JGO Coder


Medals: 10



« Reply #10 - Posted 2011-08-01 03:23:07 »

Seeing as the JRE can collect about a gig of garbage in under half a second on most machines now ...
Stopping a game for just a few frames is noticeable, so half a second is awful.

Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #11 - Posted 2011-08-01 11:55:43 »

In theory yes, it is awful. In practice, you never collect even a tenth of that in one go, and in a strange twist, provided any pauses occur in a random and irregular and not-too-frequent fashion you can absolutely get away with delays of up to maybe a tenth of a second and no-one will bat an eyelid!

In practice, this means using -Xincgc, and your problems all vanish Smiley

BTW last time I checked, Revenge of the Titans spends 0.1% of its entire execution time garbage collecting.

Cas Smiley

Offline gouessej

« In padded room »



TUER


« Reply #12 - Posted 2011-08-01 13:03:41 »

What about G1?

Offline nsigma
« Reply #13 - Posted 2011-08-01 13:24:42 »

What about G1?
My experiences with low latency audio, where pauses are far more of a concern, have so far been that -Xincgc (or -J-XX:+UseConcMarkSweepGC -J-XX:+CMSIncrementalMode, which I believe is equivalent these days) performs better than G1.  YMMV  Smiley

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #14 - Posted 2011-08-01 13:30:20 »

G1 unfortunately crashes pretty frequently in JDK6 (at least it did about 4-5 months ago), though it's fine in JDK7. I didn't notice any significant different in GC activity for Revenge - but then, at 0.1% of total execution time, I was hardly likely to in the first place Smiley

Escape analysis is finally properly turned on in JDK7 isn't it?

Cas Smiley

Offline nsigma
« Reply #15 - Posted 2011-08-01 14:28:35 »

G1 unfortunately crashes pretty frequently in JDK6 (at least it did about 4-5 months ago), though it's fine in JDK7. I didn't notice any significant different in GC activity for Revenge - but then, at 0.1% of total execution time, I was hardly likely to in the first place Smiley
Well, I didn't manage to crash it.  However, when you're doing audio with 256 or even 128 sample buffer you're looking at over 170 or 350 fps.  And no margin for missing it or it sounds like a train wreck.  G1 just couldn't cope with this when I tried, whereas Xincgc is pretty stable.  Must try with 7 sometime.

Escape analysis is finally properly turned on in JDK7 isn't it?
Isn't this now the same in 6 too?  Seen a few posts that suggest this has been default since 6u23, but nothing I'd consider authoritative.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #16 - Posted 2011-08-01 14:40:20 »

I didn't think EA in 6 actually did that thing where it replaces heap allocs with stack allocs, though I might be wrong. Certainly cuts down on loads of short-lived garbage.

I'm curious as to why GC has any impact at all on audio...? Are you abusing IO design practices?

Cas Smiley

Offline nsigma
« Reply #17 - Posted 2011-08-01 15:11:15 »

I didn't think EA in 6 actually did that thing where it replaces heap allocs with stack allocs, though I might be wrong. Certainly cuts down on loads of short-lived garbage.
I think HotSpot in 6 is now the same as 7(?), just with different default parameters.

Finally found something that looks authoritative - http://download.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html  See comment under escape analysis about 6u23. See also that it seems to suggest neither 6 or 7 replace heap allocs with stack allocs, though do get rid of some object allocations altogether.

I'm curious as to why GC has any impact at all on audio...? Are you abusing IO design practices?
Same reason it has any impact on anything else - it stops the world!  Not knowing quite what you mean by IO design practice, the audio libs in Praxis (which I'm finally in the process of getting out separately), in fact Praxis as a whole, is written around lock-free processing.  This is the only way of doing decent low latency audio, but it doesn't mean the GC can't get in the way.

Drifting a bit OT here - sure the OP won't mind ...  persecutioncomplex

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Cero
« Reply #18 - Posted 2011-08-01 15:58:18 »

Drifting a bit OT here - sure the OP won't mind ...  persecutioncomplex

well its all relevant to what GC can cause and how to handle it
can't say that I knew -Xincgc before, or that I'm really sure what it does differently

Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #19 - Posted 2011-08-01 16:20:36 »

Same reason it has any impact on anything else - it stops the world!  Not knowing quite what you mean by IO design practice, the audio libs in Praxis (which I'm finally in the process of getting out separately), in fact Praxis as a whole, is written around lock-free processing.  This is the only way of doing decent low latency audio, but it doesn't mean the GC can't get in the way.
Hm does stop-the-world actually stop all threads in the process, or just all Java threads, or just block allocs in Java threads? We use OpenAL to do our actual audio mixing, which carries on regardless of GC occurring or not (it has its own "native" mixing thread).

Cas Smiley

Offline nsigma
« Reply #20 - Posted 2011-08-01 17:11:46 »

Same reason it has any impact on anything else - it stops the world!  Not knowing quite what you mean by IO design practice, the audio libs in Praxis (which I'm finally in the process of getting out separately), in fact Praxis as a whole, is written around lock-free processing.  This is the only way of doing decent low latency audio, but it doesn't mean the GC can't get in the way.
Hm does stop-the-world actually stop all threads in the process, or just all Java threads, or just block allocs in Java threads? We use OpenAL to do our actual audio mixing, which carries on regardless of GC occurring or not (it has its own "native" mixing thread).

Cas Smiley

I don't know a huge amount about OpenAL, but I believe all the mixing and DSP (is there any DSP?) is done in native code?  I'm talking about doing DSP and mixing in Java.  I've got both a low-latency JavaSound server (possible if you ignore the way most people use JS!) and my own binding to Jack (JNA based so some potential for further optimisation).  Jack uses a callback into the JVM, so even though the Jack thread is allocated outside the JVM, and has real-time priority, once it calls into the JVM it can still get into conflict with the garbage collector.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline lhkbob

JGO Knight


Medals: 32



« Reply #21 - Posted 2011-08-01 17:35:56 »

I remember reading somewhere that System.gc() does a stop-the-world collection if the JVM decides to respond to the GC request, even if you have a more parallel collector configured.  Or it might be that if you're using the default generational, mark-and-sweep collector, calling System.gc() is equivalent to doing a full collection which is stop-the-world.

I wouldn't recommend using it since the JVM has a property you can set to disable the method call:"-XX:-DisableExplicitGC".  They wouldn't have that without a reason.

Offline nsigma
« Reply #22 - Posted 2011-08-01 19:14:25 »

I remember reading somewhere that System.gc() does a stop-the-world collection if the JVM decides to respond to the GC request, even if you have a more parallel collector configured.  Or it might be that if you're using the default generational, mark-and-sweep collector, calling System.gc() is equivalent to doing a full collection which is stop-the-world.

I wouldn't recommend using it since the JVM has a property you can set to disable the method call:"-XX:-DisableExplicitGC".  They wouldn't have that without a reason.

As far as I'm aware, all the GC's have their stop-the-world moments (ie. they suspend all non-GC threads in the VM), so at some point the mark-and-sweep collector is still going to stop things - just hopefully for a much shorter time!

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
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.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (10 views)
2014-04-15 03:46:01

UprightPath (23 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (107 views)
2014-04-08 23:06:04

tom_mai78101 (207 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!