Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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  
  How much 'help' does the gc normally need?  (Read 1761 times)
0 Members and 1 Guest are viewing this topic.
Offline cep21

Junior Duke




Java games rock!


« Posted 2004-07-06 18:39:37 »

When I'm done with a variable, I mark it as null to signal the gc can collect it (that is the correct thing to do right?).  What if the variable is an array of objects.  Does it help to mark each object as null before i mark the array as null?  Or is that just wasted time?
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2004-07-06 20:04:35 »

Wasted time. The only time you might want to null something is if you're dealing with really big objects like images, and need to replace the only reference to the image with another one - allowing the first image to be garbage collected to make way for the new one. (I discovered this fact here on JGO Smiley)

Cas Smiley

Offline cep21

Junior Duke




Java games rock!


« Reply #2 - Posted 2004-07-07 15:18:19 »

"The only time you might want to null something is if ...."

So never mark anything null unless it's huge?  Even if I won't use it again?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline mhale

Senior Newbie




Take pity, I'm just a poor blob!


« Reply #3 - Posted 2004-07-07 17:26:56 »

You will probably find the two articles below very useful. The first one has a section about explicit nulling, and states "But in most cases, it doesn't help the garbage collector at all, and in some cases, it can actually hurt your program's performance."

Garbage collection and performance
http://www-106.ibm.com/developerworks/java/library/j-jtp01274.html

Garbage collection in the 1.4.1 JVM
http://www-106.ibm.com/developerworks/java/library/j-jtp11253/
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2004-07-07 18:14:51 »

A quick example of what I meant:

int[] a = new int[10000000];
a = new int[10000000];

At one point there are two 40MB arrays in memory - the first one couldn't be collected because the new one was constructed while 'a' still held a reference to it. This kind of thing seems to occur with images more than anything else as they tend to be large.

Cas Smiley

Offline 20thCenturyBoy

Senior Duke


Medals: 3


So much to learn, so little time.


« Reply #5 - Posted 2004-07-08 06:25:36 »

Cas, I'm probably being thick, but where do you do the nulling in your example? And how does it help the gc, when you can't guarantee when it will kick in Huh

20thCB

"I have never done unit testing and I don’t find it a very useful concept" - Jonathan Blow
Offline crystalsquid

Junior Duke




... Boing ...


« Reply #6 - Posted 2004-07-08 06:37:39 »

The example was without Null'ing.
The second array is created, but the reference count to the first array would not (in theory) be decremented until the 'a = ' is evaluated. the sequence internally would be like this:

alloc 40MB (array A)
refcount A += 1
reference a = array A.

alloc 40MB (array B) <- Eek, 80MB alloc'd at the moment
refcount A -=1; <- A is now free to be GC'd
refcount B+= 1
a = array B;

Adding the line 'a = null;' in between will decrement the refcount of the first array before the second alloc, so that when you new the second array, the GC can say 'I don't have the spare memory, lets garbage collect', and lo & behold there is 40MB sitting there that it can reclaim.

As to whether modern VMs would actually do this (though I would take Cas's word that he noticed an improvement), or even whether you would notice anything with objects smaller than a few MB is another question Smiley
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #7 - Posted 2004-07-11 00:20:34 »

Quote
The second array is created, but the reference count to the first array would not (in theory) be decremented until ...


Java doesn't use refernece counting - just thought I would point that out in case some newbies get confused with your explanation.

The same point applies though -- the first array is still reachable* when the second array is being created.

*Java decides what is "garbage" based on what is reachable, not reference counts.  That's part of what makes it "safe".  It is impossible to have a reference that points to garbage, because having such a reference is what makes the object not garbage in the first place.

Technically for local references the VM could probably optimize the array example above so that it released the first array knowing in advance that it would become unreachable after executing the next line -- however it is complicated by the fact that the allocation on the next line could still fail for some reason say an OutOfMemoryError is still thrown because the second array is a bit bigger.  The VM has to be sure that the first array wasnt collected while trying to get memory for the second allocation because it would not be able to undo the optimization since the first array might remain reachable depending on how the exception was handled.
So when dealing with very large allocations in a similar context it is probably best to do the explicite nulling.

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.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (54 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
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

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