This project has retired. For details please refer to its Attic page.
MemoryUtils xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.utils;
20  
21  import org.apache.giraph.metrics.GiraphMetrics;
22  import org.apache.giraph.metrics.GiraphMetricsRegistry;
23  import org.apache.giraph.metrics.MetricNames;
24  
25  import com.yammer.metrics.util.PercentGauge;
26  
27  /**
28   * Helper static methods for tracking memory usage.
29   */
30  public class MemoryUtils {
31    /** Do not instantiate. */
32    private MemoryUtils() { }
33  
34    /**
35     * Helper to compute megabytes
36     * @param bytes integer number of bytes
37     * @return megabytes
38     */
39    private static double megaBytes(long bytes) {
40      return bytes / 1024.0 / 1024.0;
41    }
42  
43    /**
44     * Get total memory in megabytes
45     * @return total memory in megabytes
46     */
47    public static double totalMemoryMB() {
48      return megaBytes(Runtime.getRuntime().totalMemory());
49    }
50  
51    /**
52     * Get maximum memory in megabytes
53     * @return maximum memory in megabytes
54     */
55    public static double maxMemoryMB() {
56      return megaBytes(Runtime.getRuntime().maxMemory());
57    }
58  
59    /**
60     * Get free memory in megabytes
61     * @return free memory in megabytes
62     */
63    public static double freeMemoryMB() {
64      return megaBytes(Runtime.getRuntime().freeMemory());
65    }
66  
67    /**
68     * Get free plus unallocated memory in megabytes
69     * @return free plus unallocated memory in megabytes
70     */
71    public static double freePlusUnallocatedMemoryMB() {
72      return freeMemoryMB() + maxMemoryMB() - totalMemoryMB();
73    }
74  
75    /**
76     * Get fraction of memory that's free
77     * @return Fraction of memory that's free
78     */
79    public static double freeMemoryFraction() {
80      return freePlusUnallocatedMemoryMB() / maxMemoryMB();
81    }
82  
83    /**
84     * Initialize metrics tracked by this helper.
85     */
86    public static void initMetrics() {
87      GiraphMetricsRegistry metrics = GiraphMetrics.get().perJobOptional();
88      metrics.getGauge(MetricNames.MEMORY_FREE_PERCENT, new PercentGauge() {
89          @Override
90          protected double getNumerator() {
91            return freeMemoryMB();
92          }
93  
94          @Override
95          protected double getDenominator() {
96            return totalMemoryMB();
97          }
98        }
99      );
100   }
101 
102   /**
103    * Get stringified runtime memory stats
104    *
105    * @return String of all Runtime stats.
106    */
107   public static String getRuntimeMemoryStats() {
108     return String.format("Memory (free/total/max) = %.2fM / %.2fM / %.2fM",
109             freeMemoryMB(), totalMemoryMB(), maxMemoryMB());
110   }
111 }