heap and non-heap memory in JVM

The JVM memory consists of the following segments:

  • Heap Memory, which is the storage for Java objects
  • Non-Heap Memory, which is used by Java to store loaded classes and other meta-data
  • other, JVM code itself, JVM internal structures, loaded profiler agent code and data, etc.

Heap

The JVM has a heap that is the runtime data area from which memory for all class instances and arrays are allocated. It is created at the JVM start-up.

The heap size may be configured with the following VM options:

    -Xmx<size> - to set the maximum Java heap size
    -Xms<size> - to set the initial Java heap size

By default, the maximum heap size is 64 Mb.

Heap memory for objects is reclaimed by an automatic memory management system which is known as a garbage collector. The heap may be of a fixed size or may be expanded and shrunk, depending on the garbage collector’s strategy.

Non-Heap

Also, the JVM has memory other than the heap, referred to as non-heap memory. It is created at the JVM startup and stores per-class structures such as runtime constant pool, field and method data, and the code for methods and constructors, as well as interned Strings.

Unfortunately, the only information JVM provides on non-heap memory is its overall size. No detailed information on non-heap memory content is available.

The abnormal growth of non-heap memory size may indicate a potential problem, in this case you may check up the following:

  • If there are class loading issues such as leaked loaders.
  • If there are strings being massively interned. For detection of such problem, Object allocation recording may be used.

If the application indeed needs that much of non-heap memory and the default maximum size of 64 Mb is not enough, you may enlarge the maximum size with the help of -XX:MaxPermSize VM option. For example, -XX:MaxPermSize=128m sets the size of 128 Mb.

Relative Time(Yesterday, Tomorrow) using bash

When wiring in shell script, it’s very common to get a relative time, like yesterday, tomorrow, one week ago, 1 hour ago, next 30 minutes.

The date command in GNU shell already supports this feature. By running man date, you can see the option:

$ man date
NAME
       date - print or set the system date and time
SYNOPSIS
       date [OPTION]... [+FORMAT]
       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION
......
       -d, --date=STRING
              display time described by STRING, not 'now'

......
DATE STRING
       The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday".  A date string
       may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers.  An empty string indicates  the  beginning  of  the
       day.  The date string format is more complex than is easily documented here but is fully described in the info documentation.

Now, let’s do some test.

# date in the future
$ date --date=tomorrow
Thu Jan 12 12:06:35 GMT$7 2017
$ date '--date=1 day'
Thu Jan 12 12:06:35 GMT$7 2017
$ date '--date=10 day'
Sat Jan 21 12:06:35 GMT$7 2017
$ date '--date=1 week'
Wed Jan 18 12:06:35 GMT$7 2017
$ date '--date=1 month'
Sat Feb 11 12:06:35 GMT$7 2017
$ date '--date=1 year'
Thu Jan 11 12:06:35 GMT$7 2018

# date in the past
$ date --date=yesterday
Tue Jan 10 12:06:35 GMT$7 2017
$ date '--date=1 day ago'
Tue Jan 10 12:06:35 GMT$7 2017
$ date '--date=10 day ago'
Sun Jan  1 12:06:35 GMT$7 2017
$ date '--date=10 week ago'
Wed Nov  2 12:06:35 GMT$7 2016
$ date '--date=10 month ago'
Fri Mar 11 12:06:35 GMT$7 2016
$ date '--date=10 year ago'
Thu Jan 11 12:06:35 GMT$7 2007

# move date with more precise unit
# use fortnight for 14 day.
# week for 7 days.
# hour for 60 minutes
# minute for 60 seconds
# second for one second
# use this / now / today keywords to stress the meaning
$ date --date=fortnight
Wed Jan 25 12:06:35 GMT$7 2017
$ date '--date=5 fortnight'
Wed Mar 22 12:06:35 GMT$7 2017
$ date '--date=fortnight ago'
Wed Dec 28 12:06:35 GMT$7 2016
$ date '--date=5 fortnight ago'
Wed Nov  2 12:06:35 GMT$7 2016
$ date '--date=2 hour'
Wed Jan 11 14:06:35 GMT$7 2017
$ date '--date=2 hour ago'
Wed Jan 11 10:06:35 GMT$7 2017
$ date '--date=20 minute'
Wed Jan 11 12:26:35 GMT$7 2017
$ date '--date=20 minute ago'
Wed Jan 11 11:46:35 GMT$7 2017
$ date '--date=6 months 15 day'
Wed Jul 26 12:06:35 GMT$7 2017

# date using the day of week
$ date '--date=this Friday'
Fri Jan 13 00:00:00 GMT$7 2017
$ date '--date=next Friday'
Fri Jan 13 00:00:00 GMT$7 2017
$ date '--date=this Fri'
Fri Jan 13 00:00:00 GMT$7 2017
$ date '--date=next Fri.'
Fri Jan 13 00:00:00 GMT$7 2017