3

I'm running my Java app through command line without passing in any JVM settings (i.e. java -cp some.jar).

VisualVM shows the Java app allocated ~740M of Heap space.

Since I didn't pass in any JVM settings, how can I view what JVM settings my app is using?

Glide
  • 20,235
  • 26
  • 86
  • 135
  • Is [this](http://stackoverflow.com/a/5317243/2815219) answer helpful? – Raman Sahasi Oct 12 '16 at 05:26
  • @rD. Thanks but no. `jps -lvm` only shows explicit parameters being passed in. It's not showing what `Xmx` the JVM has for the app. – Glide Oct 12 '16 at 05:49
  • The maximum heap is typically 1/4 of main memory unless you have the 32-bit windows JVM. I guess you have around 3 GB if 740MB is your maximum. – Peter Lawrey Oct 12 '16 at 08:21
  • @PeterLawrey My machine has 16GB of ram. Do you mean 1/4 of all memory or available memory? If I don’t specify `Xmx` when launching my java app, is there a limit to how much heap the JVM will allocate? – Glide Oct 12 '16 at 12:48
  • 1
    @Glide the heap will be 1/4 of how much the memory the OS says you have (which can be slightly less) this is the heap limit but the JVM can use more memory for other purposes. – Peter Lawrey Oct 12 '16 at 14:16

2 Answers2

3

You can use java.lang.management.MemoryUsage:

MemoryUsage mu = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();

and get max memory from it

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Is it possible not to modify the code to find the max memory? I can currently find the max memory with VisualVM. I'm just curious what JVM settings my app is using when no VM parameters are passed in when starting the app. – Glide Oct 12 '16 at 06:01
  • @Glide: well, you could use JVisualVM for it. Wait—you do already…so…what’s your question? – Holger Oct 13 '16 at 13:36
  • @Holger You're right, I must've been blind because I didn't see the Max listed along with Size and Used under the Heap tab. – Glide Oct 13 '16 at 14:34
1

I would use a Profiling Tool and analyze the memory. Also if you know how much memory is used, the problem is not solved. Next step is to know why so much memory is used.

Try to profil your program with Java Mission Control

The tool is part of any newer JDK like: "C:\Program Files (x86)\Java\jdk1.8.0_20\bin\jmc.exe"

Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66