Setting objects to null?

Started by
8 comments, last by PurpleAmethyst 19 years, 5 months ago
When creating J2me games.If there is an object which is used only once in the entire game (Intro splash screen) etc etc.Do you explicitly set it to null after the object is used so that the gc can do it's work?
Advertisement
Yup that's exactly correct. If you still have a reference to it, then the VM will think you still want it.

When the project starts getting more complicated, and you start having multiple references to the thing, then you have to take extra care to make sure that *all* references to the thing are set to null. It's an easy mistake to have one leftover reference somewhere that you forgot about, and it only takes one to prevent the garbage collector from collecting it.
Also if you want the gc to get rid of it right away call System.gc(). But be warned doing so will give you a performance hit, whilst clearing up the memory.

If System.gc() isnt called the program will have to wait until the next garbage collection is made before that memeory is actually freed.

Setting something to null doesnt automatically free the memory it uses!
Do not remove a fly from your friend's forehead with a hatchet.Chinese Proverb
Someone should mention memory fragmentation:

It is my understanding that because the KVM garbage collector is non-compacting that this dosn't always free the memory unless you have an object which is smaller or the same size as the one you de-referenced which can replace it. If the new object is bigger next available chunk of memory is used, which sometimes means you run out of heap memory even when you shouldn't. This has been a pain for me so I feel I should warn you about it.
But usually Java objects are small and high in number, so this shouldn't be a problem.
Quote:Original post by kooktroop
Also if you want the gc to get rid of it right away call System.gc(). But be warned doing so will give you a performance hit, whilst clearing up the memory.

If System.gc() isnt called the program will have to wait until the next garbage collection is made before that memeory is actually freed.

Setting something to null doesnt automatically free the memory it uses!


If i am right.Calling System.gc() does not automatically call the garbage collector,some KVM calls it automatically,some doesn't.It's KVM version dependent.Can anyone confirm this?
Quote:
public void gc()

Runs the garbage collector. Calling this 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 virtual machine has made its best effort to recycle all discarded objects.

The name gc stands for "garbage collector". The virtual machine performs this recycling process automatically as needed, in a separate thread, even if the gc method is not invoked explicitly.

The method System.gc() is the conventional and convenient means of invoking this method.

from the Java API.

So it may but it doesnt need to do a garbage collection.
Another warning, using system.gc() on a nokia 6600 will practically kill the application, avoid it like the proverbial plague.
Quote:Original post by pkelly83
Someone should mention memory fragmentation:

It is my understanding that because the KVM garbage collector is non-compacting that this dosn't always free the memory unless you have an object which is smaller or the same size as the one you de-referenced which can replace it. If the new object is bigger next available chunk of memory is used, which sometimes means you run out of heap memory even when you shouldn't. This has been a pain for me so I feel I should warn you about it.


How to overcome this problem?
The only way is to be careful. I don't know any specific way as it all depends on what you are loading. I guess if you loaded your splash screen before anything else, and immeadiately nulled it after use, then did System.gc() you would have all the phone's memory available.

A method I use to overcome this problem is when loading the level, which usually takes up most memory apart from graphics, is to use a static array. Just load new values into this array when you need to change the level. This static array should be initialised right at the beggining of your game class, that way you can gaurantee that reloading the level won't eat more heap than it needs.

This topic is closed to new replies.

Advertisement