awesome assets from gffs code
[feisty_meow.git] / kona / src / org / gffs / application / MemoryFootprint.java
1 package org.gffs.application;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
6 import net.sourceforge.sizeof.SizeOf;
7
8 /**
9  * can retrieve the size of java objects to assist tuning of caches and memory usage. this requires the SizeOf jar and -javaagent setting to
10  * point at that jar. this is the project page: https://sourceforge.net/projects/sizeof/?source=typ_redirect
11  */
12 public class MemoryFootprint
13 {
14         static private Log _logger = LogFactory.getLog(MemoryFootprint.class);
15
16         // static SizeOf _sizeEstimater = new SizeOf();
17         static {
18                 // don't count statics in the memory size.
19                 SizeOf.skipStaticField(true);
20                 // only complain about large objects if they're bigger than the limit below.
21                 SizeOf.setMinSizeToLog(5 * 1024 * 1024);
22         }
23
24         /**
25          * can report the size of the object 'o' if instrumentation has been set up. if instrumentation is absent, all object sizes will be
26          * reported as zero.
27          */
28         public static long getFootprint(Object o)
29         {
30                 if (!_logger.isDebugEnabled()) {
31                         _logger.error("abusive memory footprint called when not in debug mode.  a logging statement is wrong.");
32                         return 0;
33                 }
34                 try {
35                         return SizeOf.sizeOf(o);
36                 } catch (Exception e) {
37                         _logger.debug("error retrieving SizeOf object; is SizeOf.jar in javaagent?");
38                         return 0;
39                 }
40         }
41
42         /**
43          * reports the size of the object 'o' plus the size of all other objects reachable from it.
44          */
45         public static long getDeepFootprint(Object o)
46         {
47                 if (!_logger.isDebugEnabled()) {
48                         _logger.error("abusive memory footprint called when not in debug mode.  a logging statement is wrong.");
49                         return 0;
50                 }
51
52                 try {
53                         return SizeOf.deepSizeOf(o);
54                 } catch (Exception e) {
55                         _logger.debug("error retrieving SizeOf object; is SizeOf.jar in javaagent?");
56                         return 0;
57                 }
58         }
59 }