I want to understand what is going on with Java and memory - I thought I did,
but evidently I'm misunderstanding something.
I am running JDK 1.4.2 on a Solaris system, and what I see is that when the java
process is started(i.e. when I browse to my application), a certain amount of
memory is evidently allocated to the JVM heap - this value is reflected in a
call to System.totalMemory() - call it "total". The amount of free memory, free
SYSTEM memory, I will call "free" and is gotten by a call to
System.freeMemory(). This is memory that can be used by Java, as Java needs it.
A call to System.maxMemory() will yield what I'll call "max", or the maximum
amount of memory that the JVM will attempt to use - I set this by the -Xmx
option when I start up. Now, in my application, as users come and go, total
will start off small in comparison to max, and then grow as time passes, up to
max, and once total=max AND (I assume) java tries to allocate more memory (due
to GC, free jumps around a lot as memory is used and freed, but I assume once
there's not enough free memory for an allocation request, trouble happens), I
get an OutOfMemory error.
So - WHY does total never go back down? Why does it only grow? Or is there
something I'm not seeing here, like that once total increases to meet a demand
and then the demand goes away, total doesn't change for a very long time, and
free is greater? But if that were the case, then shouldn't I see something where
max and free and total are almost equal?
Any answers appreciated!
Thanks!
Lynn.
Boudewijn Dijkstra - 21 May 2004 22:45 GMT
> I want to understand what is going on with Java and memory - I thought I
did, > but evidently I'm misunderstanding something.
> [...] I get an OutOfMemory error.
>
> So - WHY does total never go back down? Why does it only grow? Or is there
> something I'm not seeing here, [...]
The GC can only free memory if some of the objects on the heap have no
references to them. If you keep user information in a data structure, which
adds information about new users, but never removes information about old
users, the heap memory will eventually fill up.