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
9 package org.opendaylight.yangtools.yang.data.impl;
12 * Provides memory consumption and elapsed time between 2 points
15 public class MemoryConsumption {
17 private long memBegin;
21 * record memory and timestamp
23 public void startObserving() {
24 Runtime runtime = Runtime.getRuntime();
25 // Run the garbage collector
27 memBegin = getActualMemoryConsumption();
28 tsBegin = System.currentTimeMillis();
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));
43 * @return actual memory usage
45 public static long getActualMemoryConsumption() {
46 Runtime runtime = Runtime.getRuntime();
47 // Calculate the used memory
48 long memory = runtime.totalMemory() - runtime.freeMemory();