001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.management.diagnostics;
020
021import java.io.Serializable;
022import java.lang.management.MemoryUsage;
023import java.text.NumberFormat;
024import java.util.Locale;
025
026/**
027 * A <code>Serializable</code> implementation of {@link MemoryUsage}.
028 * @author Laurent Cohen
029 */
030public class MemoryUsageInformation implements Serializable
031{
032  /**
033   * Explicit serialVersionUID.
034   */
035  private static final long serialVersionUID = 1L;
036  /**
037   * Initial memory size.
038   */
039  private final long init;
040  /**
041   * Current memory size.
042   */
043  private final long committed;
044  /**
045   * Current used memory.
046   */
047  private final long used;
048  /**
049   * Maximum memory size.
050   */
051  private final long max;
052
053  /**
054   * Initialize this object from a {@link MemoryUsage} instance.
055   * @param memUsage contains the memroy usage information to use.
056   */
057  public MemoryUsageInformation(final MemoryUsage memUsage)
058  {
059    this.init = memUsage.getInit();
060    this.committed = memUsage.getCommitted();
061    this.used = memUsage.getUsed();
062    this.max = memUsage.getMax();
063  }
064
065  /**
066   * Get the initial memory size.
067   * @return the initial memory size.
068   */
069  public long getInit()
070  {
071    return init;
072  }
073
074  /**
075   * Get the current memory size.
076   * @return the current memory size.
077   */
078  public long getCommitted()
079  {
080    return committed;
081  }
082
083  /**
084   * Get the used memory size.
085   * @return the used memory size.
086   */
087  public long getUsed()
088  {
089    return used;
090  }
091
092  /**
093   * Get the maximum memory size.
094   * @return the maximum memory size.
095   */
096  public long getMax()
097  {
098    return max;
099  }
100
101  /**
102   * Return the ratio of used memory over maximum available memory.
103   * @return the ratio as a double value in the range [0, 1], or -1 if the maximum memory information is not available. 
104   */
105  public double getUsedRatio()
106  {
107    if (max <= 0) return -1d;
108    return (double) used / (double) max;
109  }
110
111  @Override
112  public String toString()
113  {
114    NumberFormat nf = NumberFormat.getIntegerInstance(Locale.US);
115    nf.setGroupingUsed(true);
116    nf.setMinimumIntegerDigits(9);
117    StringBuilder sb = new StringBuilder();
118    sb.append('[');
119    sb.append("init=").append(nf.format(init));
120    sb.append(", committed=").append(nf.format(committed));
121    sb.append(", used=").append(nf.format(used));
122    sb.append(", max=").append(nf.format(max));
123    sb.append(']');
124    return sb.toString();
125  }
126}