public static long getSizeOfPhysicalMemory() {
        // first try if the JVM can directly tell us what the system memory is
        // this works only on Oracle JVMs
        try {
            Class<?> clazz = Class.forName("com.sun.management.OperatingSystemMXBean");
            Method method = clazz.getMethod("getTotalPhysicalMemorySize");
            OperatingSystemMXBean operatingSystemMXBean =
                    ManagementFactory.getOperatingSystemMXBean();

            // someone may install different beans, so we need to check whether the bean
            // is in fact the sun management bean
            if (clazz.isInstance(operatingSystemMXBean)) {
                return (Long) method.invoke(operatingSystemMXBean);
            }
        } catch (ClassNotFoundException e) {
            // this happens on non-Oracle JVMs, do nothing and use the alternative code paths
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            LOG.warn(
                    "Access to physical memory size: "
                            + "com.sun.management.OperatingSystemMXBean incompatibly changed.",
                    e);
        }

        // we now try the OS specific access paths
        switch (OperatingSystem.getCurrentOperatingSystem()) {
            case LINUX:
                return getSizeOfPhysicalMemoryForLinux();

            case WINDOWS:
                return getSizeOfPhysicalMemoryForWindows();

            case MAC_OS:
                return getSizeOfPhysicalMemoryForMac();

            case FREE_BSD:
                return getSizeOfPhysicalMemoryForFreeBSD();

            case UNKNOWN:
                LOG.error("Cannot determine size of physical memory for unknown operating system");
                return -1;

            default:
                LOG.error("Unrecognized OS: " + OperatingSystem.getCurrentOperatingSystem());
                return -1;
        }
    }
@Test
    void testPhysicalMemory() {
        try {
            long physMem = Hardware.getSizeOfPhysicalMemory();
            assertThat(physMem).isGreaterThanOrEqualTo(-1);
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
    }