2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.impl;
11 * Provides memory consumption and elapsed time between 2 points.
15 public class MemoryConsumption {
16 private long memBegin;
20 * Record memory and timestamp.
22 public void startObserving() {
23 Runtime runtime = Runtime.getRuntime();
24 // Run the garbage collector
26 memBegin = getActualMemoryConsumption();
27 tsBegin = System.currentTimeMillis();
31 * Return memory usage and elapsed time message.
33 * @return memory usage and time elapsed message
35 public String finishObserving() {
36 long memEnd = getActualMemoryConsumption();
37 long tsEnd = System.currentTimeMillis();
38 return String.format("Used memory: %10d B; Elapsed time: %5d ms", memEnd - memBegin, tsEnd - tsBegin);
44 * @return actual memory usage
46 public static long getActualMemoryConsumption() {
47 Runtime runtime = Runtime.getRuntime();
48 // Calculate the used memory
49 long memory = runtime.totalMemory() - runtime.freeMemory();