1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     4858522 6191542
  27  * @summary Basic unit test of OperatingSystemMXBean.getCommittedVirtualMemorySize()
  28  * @author  Steve Bohne
  29  */
  30 
  31 /*
  32  * This test is just a sanity check and does not check for the correct
  33  * value.  The correct value should be checked manually:
  34  * Solaris:
  35  *   1. In a shell, enter the command: "ps -efly"
  36  *   2. Find your process, and look in the "SZ" column.  Reported in Kbytes.
  37  * Linux:
  38  *   1. In a shell, enter the command: "ps -efly"
  39  *   2. Find your process, and look in the "SZ" column.  Reported in Kbytes.
  40  * Windows NT/XP/2000:
  41  *   1. Hit Ctrl-Alt-Delete, select Task Manager, go to Processes tab.
  42  *   2. Find your process and look in the "Mem Usage" column.  Reported in
  43  *      Kbytes.
  44  * Windows 98/ME:
  45  *   Not supported.
  46  */
  47 
  48 import com.sun.management.OperatingSystemMXBean;
  49 import java.lang.management.*;
  50 
  51 public class GetCommittedVirtualMemorySize {
  52 
  53     private static OperatingSystemMXBean mbean =
  54         (com.sun.management.OperatingSystemMXBean)
  55         ManagementFactory.getOperatingSystemMXBean();
  56 
  57     // Careful with these values.
  58     private static final long MIN_SIZE_FOR_PASS = 1;
  59     // Max size for pass dynamically determined below
  60     private static long       max_size_for_pass = Long.MAX_VALUE;
  61 
  62     private static boolean trace = false;
  63 
  64     public static void main(String args[]) throws Exception {
  65         if (args.length > 0 && args[0].equals("trace")) {
  66             trace = true;
  67         }
  68 
  69         // 4934082: On Linux, VM size *can* be larger than total swap
  70         // size.  Linux might not reserve swap memory immediately when
  71         // a page is mmaped.  This means that the reported committed
  72         // memory size can grow beyond the swap limit.
  73         long max_size = mbean.getTotalSwapSpaceSize() +
  74                         mbean.getTotalPhysicalMemorySize();
  75 
  76         if (max_size > 0) {
  77             max_size_for_pass = max_size;
  78         }
  79         long size = mbean.getCommittedVirtualMemorySize();
  80         if (size == -1) {
  81             System.out.println("getCommittedVirtualMemorySize() is not supported");
  82             return;
  83         }
  84 
  85         if (trace) {
  86             System.out.println("Committed virtual memory size in bytes: " +
  87                                size);
  88         }
  89 
  90         if (size < MIN_SIZE_FOR_PASS || size > max_size_for_pass) {
  91             throw new RuntimeException("Committed virtual memory size " +
  92                                        "illegal value: " + size + " bytes " +
  93                                        "(MIN = " + MIN_SIZE_FOR_PASS + "; " +
  94                                        "MAX = " + max_size_for_pass + ")");
  95         }
  96 
  97         System.out.println("Test passed.");
  98     }
  99 }