Class SystemUtilities


  • public class SystemUtilities
    extends java.lang.Object
    Provides utility methods for interacting with the operating system. For example querying the operating system.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  SystemUtilities.LowMemoryChecker
      Used by code that loops and needs to check regularly if Geneious is running low on memory to know whether to abort the operation.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String OUT_OF_MEMORY
      Has the value "out of memory".
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static long currentTimeMillis()
      Like System.currentTimeMillis() except on Windows platforms, this returns time with precision to the nearest millisecond instead of nearest 15 milliseconds which is the accuracy of System.currentTimeMillis().
      static long getAvailableMemory()
      Get the amount of available (free) memory in bytes.
      static long getAvailableMemoryAfterMostRecentGarbageCollection()
      Similar to getAvailableMemory() but returns the amount of available (free) memory in bytes after the Java virtual machine last performed a garbage collection.
      static long getCurrentThreadCpuTime()  
      static java.util.Locale getDefaultLocale()
      Returns the real System default locale.
      static java.lang.String getFaultyHardwareRecommendations​(java.lang.String intro, java.lang.String machine, java.lang.String details)
      Constructs an html formatted message describing likely hardware faults with instructions on how to perform system hardware checks
      static double getHighDPIScale​(java.awt.Graphics2D graphics)  
      static double getHighDPIScale​(java.awt.GraphicsDevice graphicsDevice)  
      static double getHighestDPIScale()
      Returns the DPI scale of the screen on the system with the highest DPI scale.
      static java.lang.String getJavaExecutable()
      Returns the path and name of the Java binary used by Geneious.
      static long getJavaVirtualMachineCpuTime()  
      static long getUsedMemory()
      Get the amount of used memory in bytes.
      static long getUsedMemory​(boolean adjustReturnedValueForInaccurateMemoryManagers)
      Provides an estimate of the memory used in bytes.
      static boolean is64BitOS()
      Return true if we are running under any 64-bit operating system.
      static boolean is64BitVM()
      Returns true if this Java VM is probably 64 bit (rather than 32 bit).
      static boolean isAvailableMemoryLessThan​(long memoryInMB)
      Checks whether the available memory has dropped below the specified amount.
      static boolean isAvailableMemoryTooLow()
      Checks whether the available memory has dropped below a level such that we shouldn't continue with non-essential operations.
      static boolean isJava10OrLater()
      Returns true if this VM is Java 10 or later.
      static boolean isJava11OrLater()
      Returns true if this VM is Java 11 or later.
      static boolean isJava17OrLater()
      Returns true if this VM is Java 17 or later.
      static boolean isJava6OrLater()
      Deprecated.
      As of version 6.0.0 Geneious is Java 6 or later only, this will always return true
      static boolean isJava7OrLater()
      Deprecated.
      As of version 10.0.0 Geneious is Java 8 or later only, this will always return true
      static boolean isJava8OrLater()
      Deprecated.
      As of version 10.0.0 Geneious is Java 8 or later only, this will always return true
      static boolean isJava9OrLater()
      Returns true if this VM is Java 9 or later.
      static boolean isLinux()
      Return true if we are running under any Linux operating system.
      static boolean isMac()
      Return true if we are running under any Mac operating system.
      static boolean isMacBigSurOrLater()
      Return true if we are running under a Mac operating system which is big sur or later.
      static boolean isRetinaDisplay()
      Deprecated.
      doesn't work in open JDK.
      static boolean isRetinaDisplay​(java.awt.Graphics2D graphics)  
      static boolean isSolaris()
      Deprecated.
      Geneious is no longer supported on Solaris
      static boolean isSolarisSparc()
      Deprecated.
      Geneious is no longer supported on Solaris
      static boolean isSolarisX86()
      Deprecated.
      Geneious is no longer supported on Solaris
      static java.lang.Boolean isSystemShuttingDown()  
      static boolean isUsingGarbageCollectorWhichReportsIncorrectUsedMemory()
      At the time of writing, all garbage collectors (apart from the obosolete concurrent mark and sweep) may report there are 100 MB+ of free memory even though none is available.
      static boolean isWindows()
      Return true if we are running under any Windows operating system.
      static long milliTime()
      Returns a counter that can be used for measuring elapsed time in milliseconds.
      static void openUrl​(java.net.URL url)
      Open the system's default browser and navigate to the specified url.
      static void openUrl​(java.net.URL url, java.awt.Component owner)
      Open the system's default browser and navigate to the specified url.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • OUT_OF_MEMORY

        public static final java.lang.String OUT_OF_MEMORY
        Has the value "out of memory". If an exception message contain this text, Geneious converts the message to a nice message explaining to the user that they are out of memory and what they can do about it.
        Since:
        API 4.900 (Geneious 9.0.0)
        See Also:
        Constant Field Values
    • Method Detail

      • isMac

        public static boolean isMac()
        Return true if we are running under any Mac operating system.
        Returns:
        true if we are running under the Mac operating system.
      • isMacBigSurOrLater

        public static boolean isMacBigSurOrLater()
        Return true if we are running under a Mac operating system which is big sur or later. Ie the os.version is greater than 10.15
        Returns:
        true if we are running under the big sur mac operating system or later.
        Since:
        API 4.202121 (Geneious 2021.2.1)
      • isWindows

        public static boolean isWindows()
        Return true if we are running under any Windows operating system.
        Returns:
        true if we are running under any Windows operating system.
      • isLinux

        public static boolean isLinux()
        Return true if we are running under any Linux operating system.
        Returns:
        true if we are running under any Linux operating system.
      • isSolaris

        @Deprecated
        public static boolean isSolaris()
        Deprecated.
        Geneious is no longer supported on Solaris
        Return true if we are running under any Solaris (SunOS) operating system.
        Returns:
        true if we are running under any Solaris (SunOS) operating system.
        See Also:
        isSolarisSparc(), isSolarisX86()
      • isSolarisSparc

        @Deprecated
        public static boolean isSolarisSparc()
        Deprecated.
        Geneious is no longer supported on Solaris
        Return true if we are running under any Solaris Sparc operating system.
        Returns:
        true if we are running under any Solaris Sparc operating system.
        See Also:
        isSolaris(), isSolarisX86()
      • isSolarisX86

        @Deprecated
        public static boolean isSolarisX86()
        Deprecated.
        Geneious is no longer supported on Solaris
        Return true if we are running under any Solaris X86 operating system.
        Returns:
        true if we are running under any Solaris X86 operating system.
        See Also:
        isSolaris(), isSolarisSparc()
      • is64BitOS

        public static boolean is64BitOS()
        Return true if we are running under any 64-bit operating system. This will return false when running a 32-bit OS installed on a x86_64 architecture CPU It will return true when running under a 32-bit JVM on a 64-bit operating system
        Returns:
        true if we are running under a 64-bit operating system.
        Since:
        API 4.701 (Geneious 7.0.1)
        See Also:
        is64BitVM()
      • openUrl

        public static void openUrl​(java.net.URL url)
        Open the system's default browser and navigate to the specified url.

        This method spawns a new thread which then launches the browser, so when this method returns it is not guaranteed that the browser has already started or that we already know whether starting the browser will succeed.

        If the browser cannot be opened then a message dialog is displayed to the user.

        Parameters:
        url - URL to open in a browser
      • openUrl

        public static void openUrl​(java.net.URL url,
                                   java.awt.Component owner)
        Open the system's default browser and navigate to the specified url.

        This method spawns a new thread which then launches the browser, so when this method returns it is not guaranteed that the browser has already started or that we already know whether starting the browser will succeed.

        If the browser cannot be opened then a message dialog is displayed to the user.

        Parameters:
        url - URL to open in a browser
        owner - Any component within the Window which will be the owner of any message dialogs. If null, the currently focused document frame (or JDialog with getClientProperty(ALLOW_AS_PARENT) != null) is used as the owner.
      • getAvailableMemory

        public static long getAvailableMemory()
        Get the amount of available (free) memory in bytes. Note that calling System.gc() first may increase the returned value.
        Returns:
        the amount of available (free) memory in bytes
        See Also:
        isUsingGarbageCollectorWhichReportsIncorrectUsedMemory()
      • getUsedMemory

        public static long getUsedMemory​(boolean adjustReturnedValueForInaccurateMemoryManagers)
        Provides an estimate of the memory used in bytes. Note that the value returned from this method may be very inaccurate. See isUsingGarbageCollectorWhichReportsIncorrectUsedMemory()
        Parameters:
        adjustReturnedValueForInaccurateMemoryManagers - attempts to account for the incorrect garbage collector memory usage, but uses an adjustment which may overcompensate for the error.
        Returns:
        the amount of used memory in bytes
        Since:
        API 4.202400 (Geneious 2024.0.0)
      • isUsingGarbageCollectorWhichReportsIncorrectUsedMemory

        public static boolean isUsingGarbageCollectorWhichReportsIncorrectUsedMemory()
        At the time of writing, all garbage collectors (apart from the obosolete concurrent mark and sweep) may report there are 100 MB+ of free memory even though none is available. We attempt to compensate for this by lowering the value returned getAvailableMemory().
        Returns:
        true if we are using a garbage collector which reports incorrect free memory.
        Since:
        API 4.202400 (Geneious 2024.0.0)
      • isSystemShuttingDown

        public static java.lang.Boolean isSystemShuttingDown()
        Returns:
        true if the JVM is shutting down, false if not; null if unknown
        Since:
        API 5.910 (Geneious 9.1.0)
      • getAvailableMemoryAfterMostRecentGarbageCollection

        public static long getAvailableMemoryAfterMostRecentGarbageCollection()
        Similar to getAvailableMemory() but returns the amount of available (free) memory in bytes after the Java virtual machine last performed a garbage collection. Note that this method itself does not perform or cause a garbage collection to occur A situation where you might use this rather than isAvailableMemoryLessThan(long) is if there is if the code can perform an alternative strategy to using lots of memory and the code doesn't want to incur the cost of performing a system gc, which can potentially take several minutes if there is insufficient memory.
        Returns:
        the amount of available (free) memory in bytes after the most recent garbage collection.
        Since:
        API 4.40 (Geneious 5.4.0)
      • isAvailableMemoryLessThan

        public static boolean isAvailableMemoryLessThan​(long memoryInMB)
        Checks whether the available memory has dropped below the specified amount. Note: This method may cause some SoftReferences to be garbage collected if there is currently less than memoryInMB available and by freeing those SoftReferences we could go back above memoryInMB free memory.

        Note, if a lot of memory is currently in use and there is less memory available than this method requests, this method can be very slow (several minutes). Instead, usage of getAvailableMemoryAfterMostRecentGarbageCollection() or SystemUtilities.LowMemoryChecker is suggested where possible.

        Parameters:
        memoryInMB - Minimum required multiple of a Mebibyte (1,048,576 bytes) required. We recommend using a value of at least 100, or things can get very slow on systems with many GB of memory close to their limit
        Returns:
        true if the amount of memory remaining that can be allocated for new objects is less than the specified parameter. Performs a garbage collection and frees SoftReferences if necessary before returning true
      • isAvailableMemoryTooLow

        public static boolean isAvailableMemoryTooLow()
        Checks whether the available memory has dropped below a level such that we shouldn't continue with non-essential operations. Currently this method returns true if less than 100MB of memory is available (after garbage collection if necessary). Once free memory gets below 100MB, particularly on systems with several GB of memory, things get extremely slow, to the point of hanging.

        Warning: This method should not be called frequently inside a loop. Instead, use SystemUtilities.LowMemoryChecker

        Returns:
        true if the available memory has dropped below a level such that we shouldn't continue with non-essential operations.
        Since:
        API 4.810 (Geneious 8.1.0)
      • getJavaExecutable

        public static java.lang.String getJavaExecutable()
        Returns the path and name of the Java binary used by Geneious. If the binary isn't found, the expected name of the binary (java or java.exe) is returned.
        Returns:
        The path and name of the Java binary used by Geneious
        Since:
        API 4.1100 (Geneious 11.0.0)
      • currentTimeMillis

        public static long currentTimeMillis()
        Like System.currentTimeMillis() except on Windows platforms, this returns time with precision to the nearest millisecond instead of nearest 15 milliseconds which is the accuracy of System.currentTimeMillis().

        Note that the returned precision is not actually closer to the real time, but is instead always offset by a fixed difference (for any given instance of the Java VM) between the real time and the returned time. This offset is constant for the lifetime of a Java VM and is always <= the accuracy of System.currentTimeMillis (15 milliseconds on Windows). For example for the lifetime of 1 instance of the Java VM, this method may return the true time always offset by 10 milliseconds. On a later instance of the Java VM, this method may return the true time always offset by 3 milliseconds.

        WARNING: Neither this method nor System.currentTimeMillis() should be used for measuring elapsed time. The values returned from these methods can go backwards due to time synchronization software. Use milliTime() or System.nanoTime() instead.

        Returns:
        the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
      • milliTime

        public static long milliTime()
        Returns a counter that can be used for measuring elapsed time in milliseconds. Equivalent to System.nanoTime() / 1,000,000.

        For measuring elapsed time, milliTime() should be used instead of System.currentTimeMillis() and SystemUtilities.currentTimeMillis() because the 2 latter methods return values that can jump forwards and backwards due to time synchronization software.
        Returns:
        System.nanoTime() / 1,000,000
        Since:
        API 4.710 (Geneious 7.1.0)
      • getCurrentThreadCpuTime

        public static long getCurrentThreadCpuTime()
        Returns:
        the total CPU time in milliseconds used by this thread since it started or 0 if this is unsupported.
        Since:
        API 4.14 (Geneious 5.1)
      • getJavaVirtualMachineCpuTime

        public static long getJavaVirtualMachineCpuTime()
        Returns:
        the total CPU time in milliseconds used by the Java Virtual Machine since it started or 0 if this is unsupported.
        Since:
        API 4.14 (Geneious 5.1)
      • getDefaultLocale

        public static java.util.Locale getDefaultLocale()
        Returns the real System default locale. Geneious overrides the value returned from Locale.getDefault() to Locale.UK as a sort of messy hack for importing/exporting file formats using dates.
        Returns:
        the real System default locale. Geneious overrides the value returned from Locale.getDefault() to Locale.UK.
      • isJava6OrLater

        @Deprecated
        public static boolean isJava6OrLater()
        Deprecated.
        As of version 6.0.0 Geneious is Java 6 or later only, this will always return true
        Returns true if this VM is Java 6 or later. Geneious use to have a minimum requirement of Java 5 prior to 6.0. However since Geneious now requires Java 6, this method will always return true.
        Returns:
        true if this VM is Java 6 or later
      • isJava7OrLater

        @Deprecated
        public static boolean isJava7OrLater()
        Deprecated.
        As of version 10.0.0 Geneious is Java 8 or later only, this will always return true
        Returns true if this VM is Java 7 or later.
        Returns:
        true if this VM is Java 7 or later
        Since:
        API 4.700 (Geneious 7.0.0)
      • isJava8OrLater

        @Deprecated
        public static boolean isJava8OrLater()
        Deprecated.
        As of version 10.0.0 Geneious is Java 8 or later only, this will always return true
        Returns true if this VM is Java 8 or later.
        Returns:
        true if this VM is Java 8 or later
        Since:
        API 4.900 (Geneious 9.0.0)
      • isJava9OrLater

        public static boolean isJava9OrLater()
        Returns true if this VM is Java 9 or later.
        Returns:
        true if this VM is Java 9 or later
        Since:
        API 4.1010 (Geneious 10.1.0)
      • isJava10OrLater

        public static boolean isJava10OrLater()
        Returns true if this VM is Java 10 or later.
        Returns:
        true if this VM is Java 10 or later
        Since:
        API 4.201900 (Geneious 2019.0.0)
      • isJava11OrLater

        public static boolean isJava11OrLater()
        Returns true if this VM is Java 11 or later.
        Returns:
        true if this VM is Java 11 or later
        Since:
        API 4.201900 (Geneious 2019.0.0)
      • isJava17OrLater

        public static boolean isJava17OrLater()
        Returns true if this VM is Java 17 or later.
        Returns:
        true if this VM is Java 17 or later
        Since:
        API 4.202400 (Geneious 2024.0.0)
      • is64BitVM

        public static boolean is64BitVM()
        Returns true if this Java VM is probably 64 bit (rather than 32 bit). This implementation returns true if either the system property sun.arch.data.model equals 64 or if the property java.vm.name contains 64. The Java specs are not clear if this will always work. See http://www.oracle.com/technetwork/java/hotspotfaq-138619.html#64bit_detection.
        Returns:
        true if this Java VM is probably 64 bit (rather than 32 bit).
        Since:
        API 4.50 (Geneious 5.5.0)
        See Also:
        is64BitOS()
      • isRetinaDisplay

        @Deprecated
        public static boolean isRetinaDisplay()
        Deprecated.
        doesn't work in open JDK. And a user could have multiple screens, one of which is retina, so doesn't always work correctly in other JDKs. Use isRetinaDisplay(Graphics2D) instead.
        Returns:
        true if the display is Retina (on MacOS)
        Since:
        API 4.710 (Geneious 7.1.0)
      • isRetinaDisplay

        public static boolean isRetinaDisplay​(java.awt.Graphics2D graphics)
        Parameters:
        graphics - the graphics for rendering to, or null (returns false if null).
        Returns:
        true if the display for this graphics is retina, ie. graphics will be drawn at a higher resolution and scaled down. Note that a user may have multiple screens, one of which is retina and Geneious can be moved between them while running. Always false if not Mac OS or running under Java 6 or graphics is null.
        Since:
        API 4.910 (Geneious 9.1.0)
      • getHighestDPIScale

        public static double getHighestDPIScale()
        Returns the DPI scale of the screen on the system with the highest DPI scale.

        Warning: This method should only be used in very rare situations. Systems can have multiple monitors with different resolutions, so for correct behaviour in almost all cases, you should instead use getHighDPIScale(Graphics2D) or getHighDPIScale(GraphicsDevice).

        Returns:
        the DPI scale of the screen on the system with the highest DPI scale.
        Since:
        API 4.202101 (Geneious 2021.0.1)
        See Also:
        getHighDPIScale(GraphicsDevice), getHighDPIScale(Graphics2D)
      • getHighDPIScale

        public static double getHighDPIScale​(java.awt.Graphics2D graphics)
        Parameters:
        graphics - the graphics for rendering to, or null (returns 1 if null).
        Returns:
        the scale factor (>1) if the OS draws graphics at a higher resolution than 1 physical pixel per logic pixel. (e.g. on MacOS retina, returns 2) Note that a user may have multiple screens, which have different scale factors and Geneious can be moved between them while running.
        Since:
        API 4.1010 (Geneious 10.1.0)
      • getHighDPIScale

        public static double getHighDPIScale​(java.awt.GraphicsDevice graphicsDevice)
        Parameters:
        graphicsDevice - the graphics device to test the scaling of (returns 1 if null).
        Returns:
        the scale factor (>1) if the OS draws graphics at a higher resolution than 1 physical pixel per logic pixel. (e.g. on MacOS retina, returns 2) Note that a user may have multiple screens, which have different scale factors and Geneious can be moved between them while running.
        Since:
        API 4.201900 (Geneious 2019.0.0)
      • getFaultyHardwareRecommendations

        public static java.lang.String getFaultyHardwareRecommendations​(java.lang.String intro,
                                                                        java.lang.String machine,
                                                                        java.lang.String details)
        Constructs an html formatted message describing likely hardware faults with instructions on how to perform system hardware checks
        Parameters:
        intro - a single line message describing the problem. This may be null (in 2022.0.0 and later), in which case the returned String will not be surrounded with opening and closing <html> tags.
        machine - where the hardware faulty probably is e.g. "this computer"
        details - exception message details. May be null in 2022.0.0 and later.
        Returns:
        an html formatted message describing the problem with instructions on how to perform system hardware checks
        Since:
        API 4.202120 (Geneious 2021.2.0)