Reputation: 993
How do I set java program from consumption large amount of memory? Does constantly System.out.println increase in memory consumption?
I have profiling using VisualVM, I don't quite understanding on how to fix some part of the code.
Upvotes: 2
Views: 2142
Reputation: 23980
The way to consume massive amounts of memory is by retaining objects. That is, keep them alive after you're done with them. A cache with infinite size for instance could do that.
But even then the JVM is limited to the memory assigned to it (on for instance the command line), it can never go beyond PermGenSize + HeapSize. If you hit that threshold java will stop with an OutOfMemoryError.
Using a large amount of memory according to the profiler does not mean that your program is actually needing that amount of memory. It can also be that the garbage collector has not yet run, or decided it doesn't need to run all that aggressively because there is still enough memory left.
Upvotes: 0
Reputation: 138497
Use the -Xmxn
flag, e.g. java -Xmx100m foo
to limit foo
to 100MB.
-Xmxn
Specify the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is chosen at runtime based on system configuration. For more information, see HotSpot Ergonomics
Examples:
-Xmx83886080
-Xmx81920k
-Xmx80m
Worth a read: http://javahowto.blogspot.com/2006/06/6-common-errors-in-setting-java-heap.html
Constantly using System.out.println()
should not increase memory usage. Each call is independent of the next and they won't build up a stash of used memory.
Upvotes: 5