

The first example is truly simple – the following Java code tries to allocate an array of 2M integers.

Over time the leaked objects consume all of the available Java heap space and trigger the already familiar : Java heap space error. Every time the leaking functionality of the application is used it leaves some objects behind into the Java heap space. A particular type of programming error will lead your application to constantly consume more memory. When the number of users or the volume of data suddenly spikes and crosses that expected threshold, the operation which functioned normally before the spike ceases to operate and triggers the : Java heap space error. The application was designed to handle a certain amount of users or a certain amount of data. Other causes for this OutOfMemoryError message are more complex and are caused by a programming error: That is – the application just requires more Java heap space than available to it to operate normally. There most common reason for the : Java heap space error is simple – you try to fit an XXL application into an S-sized Java heap space. Note that there might be plenty of physical memory available, but the : Java heap space error is thrown whenever the JVM reaches the heap size limit. The : Java heap space error will be triggered when the application attempts to add more data into the heap space area, but there is not enough room for it. If you do not explicitly set the sizes, platform-specific defaults will be used. The size of those regions is set during the Java Virtual Machine (JVM) launch and can be customized by specifying JVM parameters -Xmx and -XX:MaxPermSize.

These regions are called Heap space and Permgen (for Permanent Generation): To make things more complex, Java memory is separated into two different regions. This limit is specified during application startup. Java applications are only allowed to use a limited amount of memory.
