2

I compile this using jdk1.7.0_25 on Windows 7:

public class Test {             
    public static void main(String[] args) throws Exception
    {
        while (true){
            System.out.print(".");
        }                 
    }        
}

But when I run this and view java.exe in the task manager, the memory steadily creeps up.

Does anyone have an explanation for this? This is a cut down version of a program that I'm running which I suspect contains a memory leak.

(Whilst writing this, java.exe started with about 25Mb of memory: I'm now at 100Mb).

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • Why are you using old JDK? – MikeCAT Jun 09 '16 at 16:00
  • My boss is a luddite. Do you think this would be fixed in a later version? –  Jun 09 '16 at 16:04
  • Why do you believe that it is a memory leak? do you get OOME? – Nicolas Filotto Jun 09 '16 at 16:05
  • I can't explain why the task manager process memory goes up so high. Am I missing something obvious? –  Jun 09 '16 at 16:05
  • read my answer here http://stackoverflow.com/questions/37684928/jvm-heap-not-released/37685111#37685111 it may help – Nicolas Filotto Jun 09 '16 at 16:06
  • The print statement probably uses some memory internally, e.g. for accessing the stream etc. and that might mean creating temporary objects. Unless the JVM actually thinks it runs out of memory it might not run any garbage collection and thus just acquire more and more memory. – Thomas Jun 09 '16 at 16:07
  • 1
    Just because it's up to 100 MB doesn't necessarily mean that it is leaking memory. The memory usage may go down a large amount once garbage collection occurs. To see if this is the case, force garbage collection: http://stackoverflow.com/questions/1481178/how-to-force-garbage-collection-in-java – Tony Hinkle Jun 09 '16 at 16:07
  • But what could possibly be scheduled for garbage collection? –  Jun 09 '16 at 16:08
  • 1
    Suggestion: acquire a heap dump and check what actually takes up the memory. – Thomas Jun 09 '16 at 16:08
  • I agree, this would continue until the program ends. – Weasemunk Jun 09 '16 at 16:09

1 Answers1

0

The System cache has to store a gradually increasing string of periods because it is not being released. Your memory will increase by the amount of text stored in System.out. This is expected behavior.

A hacky solution would be to print a backspace character ("\b") after every period.

Weasemunk
  • 455
  • 4
  • 16
  • If you mean the OS's cache (e.g. the console's cache) then you might be right. The JVM itself should not store that string of periods since it just writes one character to stdout and then forgets about it. – Thomas Jun 09 '16 at 16:21
  • That may be true; I was thinking System was native to the JVM. @Chump, could you try the solution to see if it does the same thing? – Weasemunk Jun 09 '16 at 16:23