I was seeing this with Java 1.4 on Windows XP. So Jim, you think that the Task Manager is not reporting memory usage correctly? No, that's not it. The Task Manager and the Runtime methods are reporting different things. The Task Manager is reporting the total memory taken up by
everything in the JVM, while totalMemory and freeMemory refer only to the heap - the memory used by Java objects. At least, I think that's the distinction. So what the output of my program above tells me is that the heap is not growing, but something else in the JVM is.
Rob's discussion is true in general, but I don't think it applies here, as the heap
isn't growing. The memory used rises and falls a bit, but it's well below the totalMemory(). And even if random fluctuations might occasionally force the JVM size to increase, what I see is slow but
consistent growth in the JVM, which doesn't seem to be accounted for by heap fluctuations.
As an aside, on other occasions I actually have sometimes seen JVM memory allocations
decrease in size. Evidently some JVM's are smart enough to notice when they've got more memory allocated than they need. In some situations at least - I never determined a consistent
pattern.