1

Is there a way to get all the setting from which a java program (JVM) is running from with in the program. Yes, I want them from with in the program so I can log them as part of application log.

Here is some useful info - but either they capture this inform from outside the program or only lists specific info from within the program.

  1. Getting the parameters of a running JVM - but this is from outside the program not from within - i want these settings to be part of application log for latter reference even after the application is terminated.

  2. Getting GC settings for running JVM - this is specific to GC details. I want all parameters/setting of JVM from within program.

  3. how to get the min and max heap size settings of a JVM from within a Java program - this is also specific to Memory.

  4. How to read JVM parameters/arguments from within a running Java application - this for arguments explicitly passed to the JVM. It's does not capture the other settings not passed to JVM explicitly.

I am fine to use libraries outside of JDK too - like spring etc if they can provide me this info.

joven
  • 371
  • 1
  • 6
  • 17
  • @Brian, please undo the duplication. The Q you pointed to is specific to argument. I am asking for all settings. And did clearly mention this in the Q. You should re-read the Q. – joven Jun 21 '21 at 07:14

3 Answers3

1

You can get lots of information about the running JVM programmatically from the ManagementFactory . Apart from standard MBeans exposing JVM information your other components like appserver, frameworks, application code can expose custom data via JMX and can be accessed.

You can take a look here at an example of how memory related information is extracted from this interface.

Also in another example here you can see how Tomcat server uses this interface to log the commandline argumnets

List<String> args = ManagementFactory.getRuntimeMXBean().getInputArguments();
            for (String arg : args) {
                log.info(sm.getString("versionLoggerListener.arg", arg));
            }
Shailendra
  • 8,874
  • 2
  • 28
  • 37
1

If by JVM settings you mean VM flags, you can get them with the following code:

public static void main(String[] args) throws Exception {
    System.out.print(getVMFlags());
}

public static String getVMFlags() throws JMException {
    return (String) ManagementFactory.getPlatformMBeanServer().invoke(
            new ObjectName("com.sun.management:type=DiagnosticCommand"),
            "vmFlags",
            new Object[]{new String[]{"-all"}},
            new String[]{"[Ljava.lang.String;"});
}

Sample output:

[Global flags]
     intx ActiveProcessorCount                      = -1          {product}
    uintx AdaptiveSizeDecrementScaleFactor          = 4           {product}
    uintx AdaptiveSizeMajorGCDecayTimeScale         = 10          {product}
    uintx AdaptiveSizePausePolicy                   = 0           {product}
    uintx AdaptiveSizePolicyCollectionCostMargin    = 50          {product}
    uintx AdaptiveSizePolicyInitializingSteps       = 20          {product}
    uintx AdaptiveSizePolicyOutputInterval          = 0           {product}
    uintx AdaptiveSizePolicyWeight                  = 10          {product}
    uintx AdaptiveSizeThroughPutPolicy              = 0           {product}
    uintx AdaptiveTimeWeight                        = 25          {product}
     bool AdjustConcurrency                         = false       {product}
     bool AggressiveHeap                            = false       {product}
     bool AggressiveOpts                            = false       {product}
     intx AliasLevel                                = 3           {C2 product}
     bool AlignVector                               = false       {C2 product}
...

Note, however, this API is not standard. It works only in OpenJDK / HotSpot JVM, and may be changed or even removed in any future release.

apangin
  • 92,924
  • 10
  • 193
  • 247
0

Have you check spring boot actuator? This provide lots of OOB metrics for JVM

  • Various memory and buffer pool details
  • Statistics related to garbage collection
  • Threads utilization
  • The Number of classes loaded/unloaded

Ref: https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.metrics.supported