001/**
002 * Copyright 2014-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.jute.runtime;
017
018import static org.kuali.common.jute.base.Precondition.checkMin;
019
020import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
021
022@JsonDeserialize(builder = Memory.Builder.class)
023public final class Memory {
024
025    private final long used;
026    private final long free;
027    private final long allocated;
028    private final long max;
029
030    private Memory(Builder builder) {
031        this.used = builder.used;
032        this.free = builder.free;
033        this.allocated = builder.allocated;
034        this.max = builder.max;
035    }
036
037    public static Memory build() {
038        // Total amount of memory the JVM is allowed to use
039        long max = Runtime.getRuntime().maxMemory();
040
041        // Total amount of memory currently allocated
042        long allocated = Runtime.getRuntime().totalMemory();
043
044        // The JDK method "freeMemory()" reports what is free in the currently allocated heap
045        // The amount of memory currently being used by the JVM is the difference between what has been allocated and what is still free
046        long used = allocated - Runtime.getRuntime().freeMemory();
047
048        // The true amount of free memory is the difference between max and what is currently being used
049        long free = max - used;
050
051        return builder().withAllocated(allocated).withFree(free).withMax(max).withUsed(used).build();
052    }
053
054    public static Builder builder() {
055        return new Builder();
056    }
057
058    public static class Builder implements org.apache.commons.lang3.builder.Builder<Memory> {
059
060        // Total amount of memory the JVM is allowed to use
061        private long max;
062
063        // Total amount of memory currently allocated
064        private long allocated;
065
066        // The amount of memory currently being used by the JVM
067        private long used;
068
069        // The total amount of memory still available to the JVM
070        private long free;
071
072        public Builder withUsed(long used) {
073            this.used = used;
074            return this;
075        }
076
077        public Builder withFree(long free) {
078            this.free = free;
079            return this;
080        }
081
082        public Builder withAllocated(long allocated) {
083            this.allocated = allocated;
084            return this;
085        }
086
087        public Builder withMax(long max) {
088            this.max = max;
089            return this;
090        }
091
092        @Override
093        public Memory build() {
094            return validate(new Memory(this));
095        }
096
097        private static Memory validate(Memory instance) {
098            checkMin(instance.used, 0, "used");
099            checkMin(instance.free, 0, "free");
100            checkMin(instance.allocated, 0, "allocated");
101            checkMin(instance.max, 0, "max");
102            return instance;
103        }
104
105    }
106
107    public long getUsed() {
108        return used;
109    }
110
111    public long getFree() {
112        return free;
113    }
114
115    public long getAllocated() {
116        return allocated;
117    }
118
119    public long getMax() {
120        return max;
121    }
122
123}