Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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  
  Does System.gc() need a pause ?  (Read 1921 times)
0 Members and 1 Guest are viewing this topic.
Offline Serethos

Junior Member




Java games rock!


« Posted 2004-12-23 07:31:06 »

i often see some code snippets for manually cleaning up memory like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
System.gc();
try
{
    Thread.sleep(500);
}
catch(InterruptedException e){ ... }

// or

System.gc()
Thread.yield();


its used for giving the garbage collection request some amount of "free time" so that it is really invoked.
but does this really have any affect ? or is the gc request so independent that it follows it own rules ?
Offline princec

JGO Kernel


Medals: 362
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2004-12-23 11:53:11 »

It's pretty pointless really.

Cas Smiley

Offline Serethos

Junior Member




Java games rock!


« Reply #2 - Posted 2004-12-23 12:08:57 »

good to know. would have been a little wired though.
but i am really sure to have read this in a java game book. i only have to look up if it was the one from brackeen or petchel ...   Cool
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #3 - Posted 2004-12-23 12:30:26 »

That's actually a left-over trick from the 1.1 days. Back then the Garbage Collector would happily interrupt your game at the worst time possible. As a result, programmers got used to manually invoking it when they knew their program had a little extra time to burn.

These days, that trick actually slows down the JVM. The Garbage Collector is a far more complex device than the mark and sweep collector of Java 1.1, and is quite good at keeping itself out of the way.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline shmoove

Junior Member




Doh!


« Reply #4 - Posted 2004-12-23 13:22:31 »

The trick is still useful with MIDP many times (though device-dependantly).

shmoove
Offline Serethos

Junior Member




Java games rock!


« Reply #5 - Posted 2004-12-23 13:56:40 »

ohh good to know shmoove. its exactly for j2me purposes, though i thought its a general purpose, i didnt post it there.
some overview, where to use it and where to avoid ?
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #6 - Posted 2004-12-23 19:53:10 »

Quote
That's actually a left-over trick from the 1.1 days. Back then the Garbage Collector would happily interrupt your game at the worst time possible. As a result, programmers got used to manually invoking it when they knew their program had a little extra time to burn.

These days, that trick actually slows down the JVM. The Garbage Collector is a far more complex device than the mark and sweep collector of Java 1.1, and is quite good at keeping itself out of the way.


But then still, why the extra sleep?
It seems pointless, even for 1.1

Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #7 - Posted 2004-12-24 01:46:48 »

Quote
But then still, why the extra sleep?
It seems pointless, even for 1.1


The sleep was usually a calculated time period, not a fixed one. However, fixed time periods were not uncommon. Since the mark and sweep collector would make the same pass each time, you could get fairly consistent timing for each loop. Just tune your sleep time correctly, and your framerate should naturally fall about where you want it.

Truth be told, though, I think that slowness was an advantage for 1.1. Since your code couldn't produce more than 15-30 FPS anyway, you could run flat out and never even care about timing. :-)

Java Game Console Project
Last Journal Entry: 12/17/04
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #8 - Posted 2004-12-24 07:43:05 »

It seems to me the sleep has a different purpose and has nothing to do with GC then...  :-/

Offline shmoove

Junior Member




Doh!


« Reply #9 - Posted 2004-12-26 05:29:35 »

Quote
ohh good to know shmoove. its exactly for j2me purposes, though i thought its a general purpose, i didnt post it there.
some overview, where to use it and where to avoid ?


Well, you have to take it in a case by case basis. Off the top of my head, I can tell you about some SKVM devices (A Korean flavour of J2ME) that need to have code like that  (gc then sleep) after every time a network connection attepmt failed, or else you would never be able to make another successful connection.
Nothing can be generalized because this trick is usually used to overcome various bugs in the implementations.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Raghar

Junior Member




Ue ni taete 'ru hitomi ni kono mi wa dou utsuru


« Reply #10 - Posted 2004-12-26 11:57:58 »

Quote
It's pretty pointless really.
Cas Smiley

You sure about it?
My fastest arithmetic compressor used 400 MB RAM of 500 MB, then threw it away, then alocated another 400 MB. It swaped pretty heavily without System.gc().
Of course a little swaping isn't bad, but 400 MB swaping * 2 is nasty even on lastest (non flash) HD.

I did a pretty nice example of GC work, but it get lost when I pressed post button, so GC internal isues are left to imagination of programmer.
Offline princec

JGO Kernel


Medals: 362
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #11 - Posted 2004-12-26 12:47:24 »

Should have set your memory usage parameters before running it then Wink

Cas Smiley

Offline Raghar

Junior Member




Ue ni taete 'ru hitomi ni kono mi wa dou utsuru


« Reply #12 - Posted 2004-12-26 13:07:28 »

I did that. I set "don't use more than xxxx nodes". It used them, threw them away, and used another xxxx nodes. Just GC didn't reacted quickly enough.

Yes these are the problems when you have fastest arithmetic compressor in the world, GC isn't fast enought. ~_^
Offline princec

JGO Kernel


Medals: 362
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #13 - Posted 2004-12-27 11:11:26 »

The GC reacts "on-demand". You make a request, there's not enough space, it clears some garbage away. You gain incredible speed in allocation versus an occasional pause on a new().

Cas Smiley

Offline Raghar

Junior Member




Ue ni taete 'ru hitomi ni kono mi wa dou utsuru


« Reply #14 - Posted 2004-12-27 15:20:35 »

Yes that's the theory.
The problem is when you'd have container = new container(hold rest of the 30000 nodes);
Then somewhere else container = new container(hold rest of the another 30000 nodes);

So you are in situation when first 30000 nodes and container isn't garbage collected, but other container is quickly filled, for example by 30 nodes per byte of compressed data. Result? Old container isn't valid, all refferences for old nodes in the container aren't valid either. GC would have two options. 1. alocate another memory that would be taken from the swap. 2. look throught all refferences for one null one, then look again for rest of the null ones.
From my experience it would take the first aproach, throught best generic aproach would be a combination of the two above options.

Offline princec

JGO Kernel


Medals: 362
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #15 - Posted 2004-12-28 11:37:46 »

null the reference to the first container?

Cas Smiley

Offline Raghar

Junior Member




Ue ni taete 'ru hitomi ni kono mi wa dou utsuru


« Reply #16 - Posted 2004-12-30 16:41:02 »

I expect when you have code like this
1  
2  
3  
 for( int c1 = 0; c1< files.length; c1++){
   container  someContainer = new container(150000);
}

writing someContainer = null is no help.
It might be interesting to send from various parts of the code hints to GC like
GC.hint(GC.TIME_AVAILABLE, 3000);
GC.hint(GC.CLEAN, GC.ALL);
GC.hint(GC.CLEAN, GC.LAST_FRAME, 0); //translated data allocated between two calls GC.hint with identifier 0 .


Offline princec

JGO Kernel


Medals: 362
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #17 - Posted 2004-12-31 10:57:00 »

Programmatic GC hinting is a really important feature, and it's very surprising we've still only got System.gc() after all these years.

Cas Smiley

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.

CopyableCougar4 (18 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (63 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (38 views)
2014-08-06 19:49:38
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!