Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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 2902 times)
0 Members and 1 Guest are viewing this topic.
Offline cep21

Junior Devvie

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?
Online princec

« JGO Spiffy Duke »

Medals: 976
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 Devvie

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

Garbage collection in the 1.4.1 JVM
Online princec

« JGO Spiffy Duke »

Medals: 976
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 Devvie

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


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

Junior Devvie

... 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 »

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  

xxMrPHDxx (21 views)
2017-11-21 16:21:00

xxMrPHDxx (14 views)
2017-11-21 16:14:31

xxMrPHDxx (16 views)
2017-11-21 16:10:57

Ecumene (114 views)
2017-09-30 02:57:34

theagentd (150 views)
2017-09-26 18:23:31

cybrmynd (260 views)
2017-08-02 12:28:51

cybrmynd (250 views)
2017-08-02 12:19:43

cybrmynd (247 views)
2017-08-02 12:18:09

Sralse (260 views)
2017-07-25 17:13:48

Archive (881 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51 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‑
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!