The maximum size of the code cache is set via the -XX:ReservedCodeCacheSize= N flag
(where N is the default just mentioned for the particular compiler). The code cache is man-
aged like most memory in the JVM: there is an initial size (specified by -
XX:InitialCodeCacheSize= N ). Allocation of the code cache size starts at the initial size
and increases as the cache fills up. The initial size of the code cache varies based on the chip
architecture and compiler in use (on Intel machines, the client compiler starts with a 160 KB
cache and the server compiler starts with a 2,496 KB cache). Resizing the cache happens in
the background and doesn't really affect performance, so setting the ReservedCodeCacheS-
ize size (i.e., setting the maximum code cache size) is all that is generally needed.
Is there a disadvantage to specifying a really large value for the maximum code cache size so
that it never runs out of space? It depends on the resources available on the target machine. If
a 1 GB code cache size is specified, then the JVM will reserve 1 GB of native memory
space. That memory isn't allocated until needed, but it is still reserved, which means that
there must be sufficient virtual memory available on your machine to satisfy the reservation.
RESERVED VERSUS ALLOCATED MEMORY
It is important to understand the distinction between how the JVM reserves memory and how it
allocates memory. This difference applies to the code cache, the Java heap, and various other nat-
ive memory structures of the JVM.
In addition, if the JVM is 32-bit, then the total process size of the process cannot exceed 4
GB. That includes the Java heap, space for all the code of the JVM itself (including its native
libraries and thread stacks), any native memory the application allocates (either directly of
via the NIO libraries), and of course the code cache.
Those are the reasons the code cache is not unbounded and sometimes requires tuning for
large applications (or even medium-sized applications when tiered compilation is used). Par-
ticularly on 64-bit machines, though, setting the value too high is unlikely to have a practical
effect on the application: the application won't run out of process space memory, and the ex-
tra memory reservation will generally be accepted by the operating system.
The size of the code cache can be monitored using jconsole by selecting the Memory Pool
Code Cache chart on the Memory panel.