Skip to main content

Heap usage constantly growing

3 replies [Last post]
Joined: 2006-11-28


here is interesting effect I spotted:

Below is code snippet which does nothing spectacular, it waits on 0 from input to exit. There is neither object allocation nor additional thread running.

public static void main(String[] args) {
Scanner sc = new Scanner(;
int i = 10;
while (i!= 0)
i = sc.nextInt();

When I observe memory consumption of VM running this example over jconsole or jstat I see constant rise in heap memory usage. Could somebody explain me where this effect come from?

Example is running on win xp sp2 and jdk 1.5.06


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-02-15

I think it comes from the allocation of space for data to send to jconsole.

How does it behave when being monitored with visualgc?

Joined: 2004-01-07

both, jconsole and visualgc actually communicate with the jvm andask it for some values - there will be garbage created.

No need to worry about ;)

Joined: 2005-07-11

Actually, jconsole and visualgc use completely different mechanisms to access the same information. jconsole is a JMX based tool that communicates to the target JVM via network interfaces. visualgc uses a shared memory via direct ByteBuffer to access the data. When running visualgc, the JVM that runs the visualgc application will be allocating and collecting, but it has NO impact on (other than to steal cpu cycles from) the target JVM. This is one of the significant differences between jconsole and visualgc.

The call to sc.nextInt() should block when there's no input on However, if there is data on the stream, nextInt() will create a lot of objects. If there was recently data on the stream, that data will get processed before it blocks. That processing may have created reference objects that need subsequent processing.

Right now, I'm booted on Solaris and I don't see any allocation or collection occurring with no data on I can try on windows later today.

You could take a few 'jmap -histo' snapshots of this microbenchmark to see the object types that exist. This may help in figuring out what's happening.