001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 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   * Explicit serialVersionUID.
033   */
034  private static final long serialVersionUID = 1L;
035  /**
036   * Initial memory size.
037   */
038  private final long init;
039  /**
040   * Current memory size.
041   */
042  private final long committed;
043  /**
044   * Current used memory.
045   */
046  private final long used;
047  /**
048   * Maximum memory size.
049   */
050  private final long max;
051
052  /**
053   * Initialize this object from a {@link MemoryUsage} instance.
054   * @param memUsage contains the memroy usage information to use.
055   */
056  public MemoryUsageInformation(final MemoryUsage memUsage) {
057    this.init = memUsage.getInit();
058    this.committed = memUsage.getCommitted();
059    this.used = memUsage.getUsed();
060    this.max = memUsage.getMax();
061  }
062
063  /**
064   * Get the initial memory size.
065   * @return the initial memory size.
066   */
067  public long getInit() {
068    return init;
069  }
070
071  /**
072   * Get the current memory size.
073   * @return the current memory size.
074   */
075  public long getCommitted() {
076    return committed;
077  }
078
079  /**
080   * Get the used memory size.
081   * @return the used memory size.
082   */
083  public long getUsed() {
084    return used;
085  }
086
087  /**
088   * Get the maximum memory size.
089   * @return the maximum memory size.
090   */
091  public long getMax() {
092    return max;
093  }
094
095  /**
096   * Return the ratio of used memory over maximum available memory.
097   * @return the ratio as a double value in the range [0, 1], or -1 if the maximum memory information is not available.
098   */
099  public double getUsedRatio() {
100    if (max <= 0) return -1d;
101    return (double) used / (double) max;
102  }
103
104  @Override
105  public String toString() {
106    final NumberFormat nf = NumberFormat.getIntegerInstance(Locale.US);
107    nf.setGroupingUsed(true);
108    nf.setMinimumIntegerDigits(9);
109    final StringBuilder sb = new StringBuilder();
110    sb.append('[');
111    sb.append("init=").append(nf.format(init));
112    sb.append(", committed=").append(nf.format(committed));
113    sb.append(", used=").append(nf.format(used));
114    sb.append(", max=").append(nf.format(max));
115    sb.append(']');
116    return sb.toString();
117  }
118}