All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.rmuhamedgaliev.service.RAM.impl.RAMInformationImpl Maven / Gradle / Ivy

package io.github.rmuhamedgaliev.service.RAM.impl;

import io.github.rmuhamedgaliev.service.RAM.RAMInformation;
import org.springframework.stereotype.Service;

import java.lang.management.ManagementFactory;

/**
 * Developer: Muhamedgaliev Rinat
 * Date: 1/18/13
 */
@Service(value = "ramInformation")
public class RAMInformationImpl implements RAMInformation
{
    private final static int KILO = 1024;

    @Override
    public final Long getTotalRAM(String measure)
    {
        Long ramSize = null;
            long memorySize = ((com.sun.management.OperatingSystemMXBean)
                    ManagementFactory.getOperatingSystemMXBean()).getTotalPhysicalMemorySize();
            if (measure == null)
            {
                ramSize = memorySize;
            }
            else if (measure.trim().length() > 0)
            {
                switch (measure){
                    case "KiB":
                        ramSize = memorySize / KILO;
                        break;
                    case "MiB":
                        ramSize = memorySize / KILO / KILO;
                        break;
                    case "GiB":
                        ramSize = memorySize / KILO / KILO / KILO;
                        break;
                    default:
                        ramSize = memorySize;
                        break;
                }
            }
        return ramSize;
    }

    @Override
    public final Long getFreeRAM(String measure)
    {
        Long freeRamSize = null;
            long freeRam = ((com.sun.management.OperatingSystemMXBean)
                    ManagementFactory.getOperatingSystemMXBean()).getFreePhysicalMemorySize();
            if (measure == null)
            {
                freeRamSize = freeRam;
            }
            else if (measure.trim().length() > 0)
            {
                switch (measure){
                    case "KiB":
                        freeRamSize = freeRam / KILO;
                        break;
                    case "MiB":
                        freeRamSize = freeRam / KILO / KILO;
                        break;
                    case "GiB":
                        freeRamSize = freeRam / KILO / KILO / KILO;
                        break;
                    default:
                        freeRamSize = freeRam;
                        break;
                }
            }
        return freeRamSize;
    }

    @Override
    public final Long getUsedRAM(String measure)
    {
        return getTotalRAM(measure) - getFreeRAM(measure);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy