Java Code Examples for com.sun.management.OperatingSystemMXBean#getFreePhysicalMemorySize()

The following examples show how to use com.sun.management.OperatingSystemMXBean#getFreePhysicalMemorySize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WindowsSystemCommander.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 获取windows 监控
 * https://docs.oracle.com/javase/7/docs/jre/api/management/extension/com/sun/management/OperatingSystemMXBean.html
 *
 * @return 返回cpu占比和内存占比
 */
@Override
public JSONObject getAllMonitor() {
    OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    JSONObject jsonObject = new JSONObject();
    double total = operatingSystemMXBean.getTotalPhysicalMemorySize();
    double free = operatingSystemMXBean.getFreePhysicalMemorySize();
    jsonObject.put("memory", String.format("%.2f", (total - free) / total * 100));
    //最近系统cpu使用量
    double systemCpuLoad = operatingSystemMXBean.getSystemCpuLoad();
    if (systemCpuLoad <= 0) {
        systemCpuLoad = 0;
    }
    jsonObject.put("cpu", String.format("%.2f", systemCpuLoad * 100));
    jsonObject.put("disk", getHardDisk());
    return jsonObject;
}
 
Example 2
Source File: ClusterMonitor.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private UsageUtil getMachineUsage() {
  OperatingSystemMXBean operatingSystemMXBean
      = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);

  // Returns the amount of free physical memory in bytes.
  long freePhysicalMemorySize = operatingSystemMXBean.getFreePhysicalMemorySize();

  // Returns the total amount of physical memory in bytes.
  long totalPhysicalMemorySize = operatingSystemMXBean.getTotalPhysicalMemorySize();

  // Returns the "recent cpu usage" for the whole system.
  double systemCpuLoad = operatingSystemMXBean.getSystemCpuLoad();

  int process = Runtime.getRuntime().availableProcessors();

  UsageUtil monitorUtil = new UsageUtil();
  monitorUtil.memoryUsed = totalPhysicalMemorySize - freePhysicalMemorySize;
  monitorUtil.memoryCapacity = totalPhysicalMemorySize;
  monitorUtil.cpuUsed = (long) (process * systemCpuLoad * 100);
  monitorUtil.cpuCapacity = process * 100;

  return monitorUtil;
}
 
Example 3
Source File: IndexAdminController.java    From pybbs with GNU Affero General Public License v3.0 5 votes vote down vote up
@RequiresUser
@GetMapping({"/", "/index"})
public String index(Model model) {
    // 查询当天新增话题
    model.addAttribute("topic_count", topicService.countToday());
    // 查询当天新增标签
    model.addAttribute("tag_count", tagService.countToday());
    // 查询当天新增评论
    model.addAttribute("comment_count", commentService.countToday());
    // 查询当天新增用户
    model.addAttribute("user_count", userService.countToday());

    // 获取操作系统的名字
    model.addAttribute("os_name", System.getProperty("os.name"));

    // 内存
    int kb = 1024;
    OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    // 总的物理内存
    long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
    //已使用的物理内存
    long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / kb;
    // 获取系统cpu负载
    double systemCpuLoad = osmxb.getSystemCpuLoad();
    // 获取jvm线程负载
    double processCpuLoad = osmxb.getProcessCpuLoad();

    model.addAttribute("totalMemorySize", totalMemorySize);
    model.addAttribute("usedMemory", usedMemory);
    model.addAttribute("systemCpuLoad", systemCpuLoad);
    model.addAttribute("processCpuLoad", processCpuLoad);

    return "admin/index";
}
 
Example 4
Source File: TestCpu.java    From Jpom with MIT License 5 votes vote down vote up
public static String getMemery() {
    OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    // 总的物理内存+虚拟内存
    long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
    // 剩余的物理内存
    long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();

    Double compare = (1 - freePhysicalMemorySize * 1.0 / totalvirtualMemory) * 100;
    return "内存已使用:" + compare.intValue() + "%";
}
 
Example 5
Source File: SystemUtils.java    From neural with MIT License 5 votes vote down vote up
/**
 * The get memory info
 *
 * @return {@link MemoryInfo}
 */
public static MemoryInfo getMemory() {
    int mb = 1024 * 1024;
    Runtime runtime = Runtime.getRuntime();
    MemoryInfo memoryInfo = new MemoryInfo();

    // JVM
    double freeMemory = (double) runtime.freeMemory() / mb;
    double maxMemory = (double) runtime.maxMemory() / mb;
    double totalMemory = (double) runtime.totalMemory() / mb;
    double usedMemory = totalMemory - freeMemory;
    double percentUsed = (usedMemory / maxMemory) * 100.0;
    usedMemory = new BigDecimal(usedMemory).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    maxMemory = new BigDecimal(maxMemory).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    percentUsed = new BigDecimal(percentUsed).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    memoryInfo.setUsedMemory(usedMemory);
    memoryInfo.setMaxMemory(maxMemory);
    memoryInfo.setPercentUsed(percentUsed);

    // System
    OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    double totalPhysicalMemory = (double) osBean.getTotalPhysicalMemorySize() / mb;
    double freePhysicalMemory = (double) osBean.getFreePhysicalMemorySize() / mb;
    double committedVirtualMemory = (double) osBean.getCommittedVirtualMemorySize() / mb;
    totalPhysicalMemory = new BigDecimal(totalPhysicalMemory).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    freePhysicalMemory = new BigDecimal(freePhysicalMemory).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    committedVirtualMemory = new BigDecimal(committedVirtualMemory).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    memoryInfo.setTotalPhysicalMemory(totalPhysicalMemory);
    memoryInfo.setFreePhysicalMemory(freePhysicalMemory);
    memoryInfo.setCommittedVirtualMemory(committedVirtualMemory);

    return memoryInfo;
}
 
Example 6
Source File: Mem.java    From zfile with MIT License 5 votes vote down vote up
public Mem() {
    OperatingSystemMXBean osb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    // 总的物理内存+虚拟内存
    long totalVirtualMemory = osb.getTotalSwapSpaceSize();
    // 剩余的物理内存
    long freePhysicalMemorySize = osb.getFreePhysicalMemorySize();
    this.total = totalVirtualMemory;
    this.free = freePhysicalMemorySize;
    this.used = totalVirtualMemory - freePhysicalMemorySize;
}
 
Example 7
Source File: MonitorSerImpl.java    From jcalaBlog with MIT License 5 votes vote down vote up
@Override
public int getFreeMemory(){
        OperatingSystemMXBean osmxb = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
        long totalVirtualMemory = osmxb.getTotalPhysicalMemorySize();
        long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
        Double compare = (freePhysicalMemorySize * 1.0 / totalVirtualMemory) * 100;
        return compare.intValue();
}
 
Example 8
Source File: ServerLoadStatus.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public void calculateSystemInfo() {
    this.osName = System.getProperty("os.name");
    int kb = 1024;
    OperatingSystemMXBean osmxb = (OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
    this.systemLoadAverage = osmxb.getSystemLoadAverage();
    this.availableProcessors = osmxb.getAvailableProcessors();
    this.freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
    this.totalPhysicalMemorySize = osmxb.getTotalPhysicalMemorySize();
    this.usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / kb;

}
 
Example 9
Source File: MonitorSer.java    From luckyBlog with Apache License 2.0 5 votes vote down vote up
public int getFreeMemory() {
    OperatingSystemMXBean osmxb = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
    long totalVirtualMemory = osmxb.getTotalPhysicalMemorySize();
    long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
    Double compare = (freePhysicalMemorySize * 1.0 / totalVirtualMemory) * 100;
    return compare.intValue();
}
 
Example 10
Source File: GetSystemInfo.java    From FATE-Serving with Apache License 2.0 5 votes vote down vote up
public static long getUsedMemory() {
    int kb = 1024;
    OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
            .getOperatingSystemMXBean();
    long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / kb;
    return usedMemory;
}
 
Example 11
Source File: GetSystemInfo.java    From csustRepo with MIT License 5 votes vote down vote up
/**
 * 获取内存使用率  
 * @return
 */
public static String getMemeryRate()  
{  
    OperatingSystemMXBean osmxb = (OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();  
    // 总的物理内存+虚拟内存  
    long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();  
    // 剩余的物理内存  
    long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();  
    Double compare = (Double)(1 - freePhysicalMemorySize * 1.0 / totalvirtualMemory) * 100;  
    String str = compare.intValue() + "%";  
    return str;  
}
 
Example 12
Source File: ScheduleTask.java    From Jantent with MIT License 5 votes vote down vote up
public static String getMemery() {

        OperatingSystemMXBean osmxb = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
        long totalvirtualMemory = osmxb.getTotalSwapSpaceSize(); // 剩余的物理内存
        long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
        Double compare = (Double) (1 - freePhysicalMemorySize * 1.0 / totalvirtualMemory) * 100;
        String str = compare.intValue() + "%";
        return str;

    }
 
Example 13
Source File: IndexAdminController.java    From Roothub with GNU Affero General Public License v3.0 5 votes vote down vote up
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(Model model) {
	// 查询当天新增话题
    model.addAttribute("topic_count", topicService.countToday());
    // 查询当天新增评论
    model.addAttribute("comment_count", replyService.count());
    // 查询当天新增用户
    model.addAttribute("user_count", userService.countToday());
    //查询当天新增节点
    model.addAttribute("node_count", nodeService.countToday());
    // 获取操作系统的名字
    model.addAttribute("os_name", System.getProperty("os.name"));
    // 内存
    int kb = 1024;
    OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
        .getOperatingSystemMXBean();
    // 总的物理内存(G)
    float totalMemorySize = (float) osmxb.getTotalPhysicalMemorySize() / kb / kb / kb;
    
    //已使用的物理内存(G)
    float usedMemory = (float) (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / kb / kb /kb;
    // 获取系统cpu负载
    double systemCpuLoad = osmxb.getSystemCpuLoad();
    // 获取jvm线程负载
    double processCpuLoad = osmxb.getProcessCpuLoad();
    
    DecimalFormat df = new DecimalFormat("0.0");
    model.addAttribute("totalMemorySize", df.format(totalMemorySize));
    model.addAttribute("usedMemory", df.format(usedMemory));
    model.addAttribute("systemCpuLoad", df.format(systemCpuLoad));
    model.addAttribute("processCpuLoad", df.format(processCpuLoad));

	return "/admin/index";
}
 
Example 14
Source File: VMSummary.java    From vi with Apache License 2.0 4 votes vote down vote up
public VMSummary(){
    MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
    MemoryUsage u = bean.getHeapMemoryUsage();
    heapCommitedMemory= (u.getCommitted());
    heapUsedMemory=(u.getUsed());
    heapMaxMemory=(u.getMax());

    u = bean.getNonHeapMemoryUsage();
    nonHeapCommitedMemory=(u.getCommitted());
    nonHeapUsedMemory=(u.getUsed());
    nonHeapMaxMemory=(u.getMax());

    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    currentThreadCount=threadBean.getThreadCount();
    daemonThreadCount= threadBean.getDaemonThreadCount();
    totalStartedThreadCount= threadBean.getTotalStartedThreadCount();
    peakThreadCount = threadBean.getPeakThreadCount();

    ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
    loadedClassCount=classLoadingBean.getLoadedClassCount();
    totalLoadedClassCount=classLoadingBean.getTotalLoadedClassCount();
    unloadedClassCount=classLoadingBean.getUnloadedClassCount();
    getGCStatus();

    RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
    classPath = runtimeBean.getClassPath();
    libraryPath = runtimeBean.getLibraryPath();
    vmOptions = TextUtils.join(" ",runtimeBean.getInputArguments());
    bootClassPath = runtimeBean.getBootClassPath();
    upTime = runtimeBean.getUptime();

    vmName=runtimeBean.getVmName();
    vmVendor= runtimeBean.getVmVendor();


    OperatingSystemMXBean osBean= (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

    processCpuTime = osBean.getProcessCpuTime();

    jdkVersion=System.getProperty("java.version");
    jitCompiler=System.getProperty("java.vm.name");

    os = osBean.getName() + " "+osBean.getVersion();
    osArch = osBean.getArch();
    availableProcessors = osBean.getAvailableProcessors();

     commitedVirtualMemory = osBean.getCommittedVirtualMemorySize();
    freePhysicalMemorySize =(osBean.getFreePhysicalMemorySize());
    totalPhysicalMemorySize =(osBean.getTotalPhysicalMemorySize());

    freeSwapSpaceSize =(osBean.getFreeSwapSpaceSize());
    totalSwapSpaceSize =(osBean.getTotalSwapSpaceSize());

    List<GarbageCollectorMXBean> beans = ManagementFactory.getGarbageCollectorMXBeans();
    gcInfos = new ArrayList<>(beans.size());
    for (GarbageCollectorMXBean b : beans) {
        GCBean gcBean = new GCBean();
        gcBean.name =b.getName();
        gcBean.gcCount = b.getCollectionCount();
        gcBean.gcTime = b.getCollectionTime();
        gcInfos.add(gcBean);
    }

}
 
Example 15
Source File: PerformanceStatus.java    From vi with Apache License 2.0 4 votes vote down vote up
public PerformanceStatus() {
    OperatingSystemMXBean bean= (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();

    MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
    heapMemoryUsage= memBean.getHeapMemoryUsage();
    nonHeapMemoryUsage = memBean.getNonHeapMemoryUsage();

    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    currentThreadCount=threadBean.getThreadCount();
    daemonThreadCount= threadBean.getDaemonThreadCount();
    beanCreatedThreadCount= threadBean.getTotalStartedThreadCount();
    peakThreadCount = threadBean.getPeakThreadCount();

    ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
    loadedClassCount=classLoadingBean.getLoadedClassCount();
    totalLoadedClassCount=classLoadingBean.getTotalLoadedClassCount();
    unloadedClassCount=classLoadingBean.getUnloadedClassCount();
    committedVirtualMemorySize = (bean.getCommittedVirtualMemorySize());
    freePhysicalMemorySize =(bean.getFreePhysicalMemorySize());
    totalPhysicalMemorySize =(bean.getTotalPhysicalMemorySize());

    freeSwapSpaceSize =(bean.getFreeSwapSpaceSize());
    totalSwapSpaceSize =(bean.getTotalSwapSpaceSize());
    processCpuTime =(bean.getProcessCpuTime());
    availableProcessors =bean.getAvailableProcessors();
    processCpuLoad =bean.getProcessCpuLoad();

    systemCpuLoad =bean.getSystemCpuLoad();
    systemLoadAverage =bean.getSystemLoadAverage();
    appStartUpTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(runtimeBean.getStartTime()));
    runtime = (new Date().getTime() - runtimeBean.getStartTime())/1000;
    os = bean.getName()+" "+bean.getVersion();

    if(HostInfo.isLinux()){
        try {
            availableMemory = (LinuxInfoUtil.getAvailableMemKB()*1024l);
        } catch (Throwable ignored) {
        }
    }

    File[] roots = File.listRoots();
    for(File file:roots){
        rootFiles.add(new RootFile(file.getAbsolutePath(),file.getTotalSpace(),file.getFreeSpace()));
    }
    getGCStatus();
}
 
Example 16
Source File: GetSystemInfo.java    From csustRepo with MIT License 4 votes vote down vote up
/**
 * 获取剩余内存
 * @return
 */
public static Long getLeftMemory(){
 OperatingSystemMXBean osmxb=(OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
 // 剩余的物理内存  
 return osmxb.getFreePhysicalMemorySize();  
}
 
Example 17
Source File: DataxMachineUtil.java    From DataLink with Apache License 2.0 4 votes vote down vote up
public static long getMachineFreeMemory() {
    OperatingSystemMXBean osmb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    return osmb.getFreePhysicalMemorySize();
}
 
Example 18
Source File: ServerArgument.java    From incubator-iotdb with Apache License 2.0 4 votes vote down vote up
private long freePhysicalMemory() {
  OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
  long freeMemorySize = osmxb.getFreePhysicalMemorySize() / 1024 / 1024;
  return freeMemorySize;
}
 
Example 19
Source File: MemoryInformation.java    From FlyingAgent with Apache License 2.0 4 votes vote down vote up
MemoryInformation() {
    OperatingSystemMXBean os = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    physicalMemorySize = (os.getTotalPhysicalMemorySize());
    freePhysicalMemory = (os.getFreePhysicalMemorySize());
    inUseMemorySize = (physicalMemorySize - freePhysicalMemory);
}
 
Example 20
Source File: HostTask.java    From bistoury with GNU General Public License v3.0 4 votes vote down vote up
private static HostInfo getHostInfo(MxBean mxBean) {
    OperatingSystemMXBean osBean = mxBean.getOsBean();
    ThreadMXBean threadBean = mxBean.getThreadBean();

    HostInfo hostInfo = new HostInfo();
    String osName = System.getProperty("os.name");
    int availableProcessors = Runtime.getRuntime().availableProcessors();
    String cpuLoadAverages = null;
    if (osName != null && osName.toLowerCase().contains("linux")) {
        try {
            File file = new File(LOADAVG_FILENAME);
            cpuLoadAverages = FileUtil.readFile(file);
        } catch (IOException e) {
            logger.error("get CPU Load Averages error", e);
        }
    }

    double systemLoadAverage = osBean.getSystemLoadAverage();
    // 可使用内存
    long totalMemory = Runtime.getRuntime().totalMemory() / KB;
    // 剩余内存
    long freeMemory = Runtime.getRuntime().freeMemory() / KB;
    // 最大可使用内存
    long maxMemory = Runtime.getRuntime().maxMemory() / KB;

    //总交换空间
    long totalSwapSpaceSize = osBean.getTotalSwapSpaceSize() / KB;
    //空闲交换空间
    long freeSwapSpaceSize = osBean.getFreeSwapSpaceSize() / KB;
    //总物理内存
    long totalPhysicalMemorySize = osBean.getTotalPhysicalMemorySize() / KB;
    //空闲物理内存
    long freePhysicalMemorySize = osBean.getFreePhysicalMemorySize() / KB;
    //系统CPU利用率
    double systemCpuLoad = osBean.getSystemCpuLoad();

    long totalThread = threadBean.getTotalStartedThreadCount();

    //获取磁盘信息
    getDiskInfo(hostInfo);

    hostInfo.setAvailableProcessors(availableProcessors);
    hostInfo.setSystemLoadAverage(systemLoadAverage);
    hostInfo.setCpuLoadAverages(Strings.nullToEmpty(cpuLoadAverages));
    hostInfo.setOsName(osName);
    hostInfo.setTotalMemory(totalMemory);
    hostInfo.setFreeMemory(freeMemory);
    hostInfo.setMaxMemory(maxMemory);
    hostInfo.setTotalSwapSpaceSize(totalSwapSpaceSize);
    hostInfo.setFreeSwapSpaceSize(freeSwapSpaceSize);
    hostInfo.setTotalPhysicalMemorySize(totalPhysicalMemorySize);
    hostInfo.setFreePhysicalMemorySize(freePhysicalMemorySize);
    hostInfo.setUsedMemory(totalPhysicalMemorySize - freePhysicalMemorySize);
    hostInfo.setTotalThread(totalThread);
    hostInfo.setCpuRatio(systemCpuLoad);
    return hostInfo;
}