de.caff.util.startup

## Class Restarter

- java.lang.Object
- - de.caff.util.startup.Restarter

-     public final class Restarter
      extends java.lang.Object

  Restart the JVM with better settings. This class circumvents the restrictions of the original JVM started by eg double-clicking on a jar file, in which case the JVM starts with default memory settings which are often restricted too much for complex programs.

  You can influence the behavior by setting Java properties;

  - `restart.max.32bit`: maximum size used for 32bit JVMs. When I first implemented the restarter, `1700000000` (1.7GB) was a good value. But modern JVMs seem to use more memory for themselves, so nowadays the default used is only `1400000000` (1.4GB). Switch to a 64bit JVM if possible.
  - `debug.restart`: if set to `true`, additional debugging information is print to console.
  - `no.restart`: if set to `true`, restarting is not done. This can be useful when you debug your application, because debuggers connect to the original app, not the restarted one.
  - `restart.mem.part`: this defines the default part of the memory used for the application. This default is used when no method explicitly defining this part (parameter `memPart`) is used. Values are between `0.0` and `1.0`.
  Author:
    Rammi

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static double` | `DEFAULT_MEMORY_PART` Default part of memory to reserve for the application.
    `static long` | `MAX_32BIT_SIZE` Max size to use for 32bit JVMs (`restart.max.32bit`.
    `static java.lang.String` | `PROPERTY_DONT_RESTART` Property which can be set to `"true"` in order to don't restart under any circumstances.
    `(package private) static java.lang.String` | `PROPERTY_FORMAT` Pattern for formatting properties.
    `static java.lang.String` | `PROPERTY_JAVA_CLASSPATH` The Java class path property.
    `static java.lang.String` | `PROPERTY_JAVA_HOME` The Java home property.
    `static java.lang.String` | `PROPERTY_JAVA_VM_NAME` The Java virtual machine name property.
    `static java.lang.String` | `PROPERTY_RESTARTED` Property set by the restarter when starting a child process.

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static void` | `addSystemProperties(java.util.Collection<java.lang.String> commandLine)` Add the system properties to the command line arguments.
    `static java.lang.String` | `getJavaCommand()` Get the java command with which the JVM was started.
    `static long` | `getPhysicalMemorySize()` Get the amount of physical memory on this machine.
    `static boolean` | `is64BitJVM()` Check whether the JVM running this class is 64bit.
    `static void` | `main(java.lang.String[] args)` Test code.
    `static boolean` | `possiblyRestart(java.lang.Class<?> mainClass, double memoryPart, java.lang.String... args)` Possibly restart the program using improved memory settings.
    `static boolean` | `possiblyRestart(java.lang.Class<?> mainClass, java.lang.String... args)` Possibly restart the program using some 75% of available memory.
    `static boolean` | `possiblyRestart(java.lang.String mainClassName, double memoryPart, java.lang.String... args)` Possibly restart the program using improved memory settings.
    `static boolean` | `possiblyRestart(java.lang.String mainClassName, java.lang.String... args)` Possibly restart the program using some 75% of available memory..

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### PROPERTY_RESTARTED

          public static final java.lang.String PROPERTY_RESTARTED

      Property set by the restarter when starting a child process.
      See Also:
        Constant Field Values

    - #### PROPERTY_DONT_RESTART

          public static final java.lang.String PROPERTY_DONT_RESTART

      Property which can be set to `"true"` in order to don't restart under any circumstances.
      See Also:
        Constant Field Values

    - #### PROPERTY_JAVA_HOME

          public static final java.lang.String PROPERTY_JAVA_HOME

      The Java home property.
      See Also:
        Constant Field Values

    - #### PROPERTY_JAVA_CLASSPATH

          public static final java.lang.String PROPERTY_JAVA_CLASSPATH

      The Java class path property.
      See Also:
        Constant Field Values

    - #### PROPERTY_JAVA_VM_NAME

          public static final java.lang.String PROPERTY_JAVA_VM_NAME

      The Java virtual machine name property.
      See Also:
        Constant Field Values

    - #### MAX_32BIT_SIZE

          public static final long MAX_32BIT_SIZE

      Max size to use for 32bit JVMs (`restart.max.32bit`.

    - #### DEFAULT_MEMORY_PART

          public static final double DEFAULT_MEMORY_PART

      Default part of memory to reserve for the application. Can be set via Java property `restart.mem.part` to something between `0.0` (no memory) and `1.0` (all memory). If property is not defined or has an insane value `0.75` is used.

    - #### PROPERTY_FORMAT

          static final java.lang.String PROPERTY_FORMAT

      Pattern for formatting properties.
      See Also:
        Constant Field Values

  - ### Method Detail

    - #### possiblyRestart

          public static boolean possiblyRestart(@NotNull
                                                java.lang.Class<?> mainClass,
                                                java.lang.String... args)

      Possibly restart the program using some 75% of available memory.

      This method only returns if it did not restart the program, either because the program was already restarted, or because there were errors which kept the program from being restarted.

      Parameters:
        `mainClass` - main class to start
        `args` - program arguments
      Returns:
        `true` if the program was already restarted, or there are no better memory settings available
        `false` if there are problems keeping the program from being restarted

    - #### possiblyRestart

          public static boolean possiblyRestart(@NotNull
                                                java.lang.Class<?> mainClass,
                                                double memoryPart,
                                                java.lang.String... args)

      Possibly restart the program using improved memory settings.

      This method only returns if it did not restart the program, either because the program was already restarted, or because there were errors which kept the program from being restarted.

      Parameters:
        `mainClass` - main class to start
        `memoryPart` - part of physical memory to use
        `args` - program arguments
      Returns:
        `true` if the program was already restarted, or there are no better memory settings available
        `false` if there are problems keeping the program from being restarted

    - #### possiblyRestart

          public static boolean possiblyRestart(@NotNull
                                                java.lang.String mainClassName,
                                                java.lang.String... args)

      Possibly restart the program using some 75% of available memory..

      This method only returns if it did not restart the program, either because the program was already restarted, or because there were errors which kept the program from being restarted.

      Parameters:
        `mainClassName` - full name of main class to start
        `args` - program arguments
      Returns:
        `true` if the program was already restarted, or there are no better memory settings available
        `false` if there are problems keeping the program from being restarted

    - #### possiblyRestart

          public static boolean possiblyRestart(@NotNull
                                                java.lang.String mainClassName,
                                                double memoryPart,
                                                java.lang.String... args)

      Possibly restart the program using improved memory settings.

      This method only returns if it did not restart the program, either because the program was already restarted, or because there were errors which kept the program from being restarted.

      Parameters:
        `mainClassName` - full name of main class to start
        `memoryPart` - part of physical memory to use
        `args` - program arguments
      Returns:
        `true` if the program was already restarted, or there are no better memory settings available
        `false` if there are problems keeping the program from being restarted

    - #### addSystemProperties

          public static void addSystemProperties(@NotNull
                                                 java.util.Collection<java.lang.String> commandLine)

      Add the system properties to the command line arguments.
      Parameters:
        `commandLine` - command line arguments

    - #### getPhysicalMemorySize

          public static long getPhysicalMemorySize()

      Get the amount of physical memory on this machine.
      Returns:
        the amount of physical memory in bytes, or `0L` if the amount is unknown

    - #### getJavaCommand

          @Nullable
          public static java.lang.String getJavaCommand()

      Get the java command with which the JVM was started.
      Returns:
        java command or `null` if the command couldn't be determined

    - #### is64BitJVM

          public static boolean is64BitJVM()

      Check whether the JVM running this class is 64bit. This is using a heuristic check, so it is not completely reliable. Basically it should err that it assumes that an actual 64bit machine is just 32bit.
      Returns:
        `true` if this is a 64bit machine,
        `false` if this is a 32bit machine

    - #### main

          public static void main(java.lang.String[] args)

      Test code.
      Parameters:
        `args` - arguments (unused, but should be passed through)