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.*;
023
024
025/**
026 * Instances of this class hold memory usage information.
027 * @author Laurent Cohen
028 */
029public class MemoryInformation implements Serializable
030{
031  /**
032   * Explicit serialVersionUID.
033   */
034  private static final long serialVersionUID = 1L;
035  /**
036   * The heap memory usage.
037   */
038  private MemoryUsageInformation heapMemoryUsage = null;
039  /**
040   * The non-heap memory usage.
041   */
042  private MemoryUsageInformation nonHeapMemoryUsage = null;
043
044  /**
045   * Create this object and initialize its data.
046   */
047  public MemoryInformation()
048  {
049    collectMemoryUsage();
050  }
051
052  /**
053   * Collect the memory usage information.
054   */
055  private void collectMemoryUsage()
056  {
057    MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
058    this.heapMemoryUsage = new MemoryUsageInformation(bean.getHeapMemoryUsage());
059    this.nonHeapMemoryUsage = new MemoryUsageInformation(bean.getNonHeapMemoryUsage());
060  }
061
062  @Override
063  public String toString()
064  {
065    StringBuilder sb = new StringBuilder();
066    sb.append("heap    : ").append(heapMemoryUsage).append('\n');
067    sb.append("non-heap: ").append(nonHeapMemoryUsage).append('\n');
068    return sb.toString();
069  }
070
071  /**
072   * Get the heap memory usage.
073   * @return a <code>JPPFMemoryUsage</code> instance.
074   */
075  public MemoryUsageInformation getHeapMemoryUsage()
076  {
077    return heapMemoryUsage;
078  }
079
080  /**
081   * Get the non-heap memory usage.
082   * @return a <code>JPPFMemoryUsage</code> instance.
083   */
084  public MemoryUsageInformation getNonHeapMemoryUsage()
085  {
086    return nonHeapMemoryUsage;
087  }
088}