Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Max Heap , real free memory and GC invoke

Status
Not open for further replies.

antonas

Programmer
Oct 22, 2002
6
DE
Hi,

My question is about Garbage Collector invoking. Ash far as I know Garbage collector implicitly is invoked when it reaches maximum heap size assigned to the JVM. Lets say if I have
java -Xmx25m Blabla

So when JVM memory usage will reach 25M it will not be able to create more objects and will run GC tu free not strongly reffered objects. My question what if on my system is only 20 Mb available memory (lets say 10Mb RAM and 10MB virtual). Will GC be executed when it reaches phycical memory limit or it will throw OutOfMemoryError ?

Thank You very much

Tony
 
The GC does run as you describe, but it also runs at other times, usually when the cpu is idle (no processing running) so it does garbage collecting at other times, even if the memory isnt maxed out.

In answer to your question I believe it depends very much on your programming. If you have 25Mb of strongly referenced objects then your program with collapse in a heap because there is nothing the GC can remove, however, if you program smartly and null object references and other things to minimalise the number of strongly referanced types then the program will cope. ----------------------------------------
There are no onions, only magic
----------------------------------------
 
Yes but my question is if GC will be invoked after physical free memory is no available. Situation is that I have created plenty of temporary objects in the loop. I explicitly null in the loop so they can be collected by GC. But The problem is (or may be) that I set Max Heap size of JVM to 25 Mb and before I start JVM I see that only 20MB of free memory is available. So again when my JVM reaches 20 MB does it starts GC or it still tries to achieve 25 MB and then runs out of memory ?

Thanks in Advance
 
You should realize that the answer will depend on your platform, since GC is implemented by the JVM and differs from one to the other.

For instance, on the Windows platform that extra 5M of heap you defined will result in your JVM using the swap file on disk. You won't get an Out-of-memory error unless the swap file can't expand to cover it. Setting it up this way will result in a very slow application.

The general rule is that you're not supposed to define the heap larger than the RAM available.
 
So, yes, you will get a Out_Of_Memory error if you have consumed all of your heap space AND there is no more swap file space in windows (highily unlikely unless you have set your swap file to less then 5 megs in the first place). As for Linux etc I am not sure, but I expect the case to be similar.

You can try to call
Code:
System.gc()
to free up some space before the max is reached, although the JVM does not garantee that the gc will run when you call it (however in my experience it tends do, but don't bet on it).

You mentioned that you create a lot of temporary objects, can those objects not be reused instead of creating new ones? I don't know your code or what you are trying to achieve, but I thought I would bring it up just to get you thinking about it (but of course you probably have already)


Does that kinda answer your initial question? Or am I rambling to much? ----------------------------------------
There are no onions, only magic
----------------------------------------
 
Thanks for your response.

So if got it clear JVM does not starts gc when physical memory limit is reached. Thats actualy my question is. Its little hard to use System.gc because I can hardly say when I need to start it while I am sending objects via network in some encoded format. Object size can vary from very small to pretty huge. So I can not count on number of objects. Checking Runtime.freeMemory() after every object created is too expensive.

Of cource the best solution would be reuse an objects but I send objects via network in encoded files, and encoding API is written by third part. Tha API documentation defines that new object shoud be given (maybe its based on serialization)

Thank You
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top