How do I get the total memory used by the app?

338 views
Skip to first unread message

larry google groups

unread,
Sep 12, 2012, 1:12:18 PM9/12/12
to clo...@googlegroups.com
I need to know how much memory my app is using (it is a long running service). I see that some people have asked about how to measue memory use. Robert McIntyre asked, "get the total memory used by a data structure?" Some of the answers imply that it is easy to get the total memory use of the app (much easier than getting the memory used by a particular data structure?). I am ignorant of the JVM. How can I find the total memory used at any given moment by my app?

Goal: I wrote a small app that uses Ring and Moustache and Enlive. It lives on my server and runs perpetually. I worry about it crashing, or becoming overloaded. I am setting up some ping services (not sure which yet, Nagios, or Puppet or something) to ask the app "Are you still alive?" I've established a special Moustache route just for the ping. It occurred to me that the ping could get some useful info, like memory, and save that to a file. That would give me the a good time series about the real world memory use, maybe every 5 minutes. 

I established the app with this JVM setting:

 :jvm-opts ["-Xmx4000m"]

Within that limit, I'd like to know what is going on. 

Sean Corfield

unread,
Sep 12, 2012, 1:21:09 PM9/12/12
to clo...@googlegroups.com
Here's some code we use to get memory and CPU usage in a displayable form:

(defn- as-megabytes
"Given a sequence of byte amounts, return megabyte amounts
as string, with an M suffix."
[memory]
(map #(str (int (/ % 1024 1024)) "M") memory))

(defn- as-percentage
"Given a pair of values, return the percentage as a string."
[[a b]]
(str (int (* 100 (/ a b))) "%"))

(defn- memory-bean
"Return the MemoryMXBean."
[]
(java.lang.management.ManagementFactory/getMemoryMXBean))

(defn- heap-usage
"Given a MemoryMXBean, return the heap memory usage."
[^java.lang.management.MemoryMXBean bean]
(.getHeapMemoryUsage bean))

(defn- heap-used-max
"Given heap memory usage, return a pair of used/max values."
[^java.lang.management.MemoryUsage usage]
[(.getUsed usage) (.getMax usage)])

(defn memory-usage
"Return percentage, used, max heap as strings."
[]
(let [used-max (-> (memory-bean) (heap-usage) (heap-used-max))]
(cons (as-percentage used-max)
(as-megabytes used-max))))

(defn- operating-system-bean
"Return the OperatingSystemMXBean."
[]
(java.lang.management.ManagementFactory/getOperatingSystemMXBean))

(defn- cpus
"Given an OSMXBean, return the number of processors."
[^java.lang.management.OperatingSystemMXBean bean]
(.getAvailableProcessors bean))

(defn- load-average
"Given an OSMXBean, return the load average for the last minute."
[^java.lang.management.OperatingSystemMXBean bean]
(.getSystemLoadAverage bean))

(defn- cpu-percentage
"Given the number of CPUs and the load-average, return the
percentage utilization as a string."
[[cpus load-average]]
(str (int (* 100 (/ load-average cpus))) "%"))

(defn cpu-usage
"Return utilization (as a string) and number of CPUs and load average."
[]
(let [bean (operating-system-bean)
data ((juxt cpus load-average) bean)]
(cons (cpu-percentage data)
data)))

Dave Ray

unread,
Sep 12, 2012, 1:21:15 PM9/12/12
to clo...@googlegroups.com
You can connect jconsole or visualvm to your running app to monitor memory usage, GC, threads, etc, etc. On my machine, jconsole lives in $JAVA_HOME/bin/jconsole.

Cheers,

Dave

On Wed, Sep 12, 2012 at 10:12 AM, larry google groups <lawrenc...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

larry google groups

unread,
Sep 12, 2012, 1:45:54 PM9/12/12
to clo...@googlegroups.com
Thank you much!

Shantanu Kumar

unread,
Sep 12, 2012, 1:50:20 PM9/12/12
to Clojure
Also look at java.lang.Runtime class methods maxMemory, freeMemory and
totalMemory:

http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html

Shantanu

On Sep 12, 10:45 pm, larry google groups <lawrencecloj...@gmail.com>
wrote:

larry google groups

unread,
Sep 12, 2012, 10:12:39 PM9/12/12
to clo...@googlegroups.com
Sean, 

Thank you much for this. I am curious about this:

(defn cpu-usage 
  "Return utilization (as a string) and number of CPUs and load average." 
  [] 
  (let [bean (operating-system-bean) 
        data ((juxt cpus load-average) bean)] 
    (cons (cpu-percentage data) 
          data))) 


I notice that on my server, if at the command line I simply run "top" I see CPU load averages .5 or .6, but this function gives me numbers around 40. Could you point me to some information about what the JVM is really reporting with this number? 



On Wednesday, September 12, 2012 1:21:20 PM UTC-4, Sean Corfield wrote:

larry google groups

unread,
Sep 12, 2012, 10:13:09 PM9/12/12
to clo...@googlegroups.com
Shantanu,

Thank you. That is great information.

Sean Corfield

unread,
Sep 12, 2012, 10:30:29 PM9/12/12
to clo...@googlegroups.com
On Wed, Sep 12, 2012 at 7:12 PM, larry google groups
<lawrenc...@gmail.com> wrote:
> I notice that on my server, if at the command line I simply run "top" I see
> CPU load averages .5 or .6, but this function gives me numbers around 40.
> Could you point me to some information about what the JVM is really
> reporting with this number?

How many cores do you have?
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)
Reply all
Reply to author
Forward
0 new messages