From Java code to Java heap

Source: Internet
Author: User
Tags command line integer memory usage

Optimizing the memory usage of application code is not a new topic, but people usually don't understand it well. This article will briefly describe the memory usage of the Java process, and then delve into the memory usage of the Java code that you write. Finally, this article will demonstrate ways to improve the efficiency of code memory, with particular emphasis on the use of Java collections such as HashMap and ArrayList.

Background information: Memory usage for Java processes

By executing Java on the command line or by starting a Java-based middleware, the Java runtime creates an operating system process, as you would if you were running a C-based program. In fact, most JVMs are written in C or C + + language. As an operating system process, the Java runtime faces exactly the same memory limitations as other processes: the addressing capabilities provided by the architecture and the user space provided by the operating system.

The memory addressing capabilities provided by the schema depend on the number of bits of the processor, for example, 32-bit or 64-bit, and 31 for the mainframe. The number of bits the process can handle determines the range of memory the processor can address: 32-bit provides a 2^32 addressable range, which is 4,294,967,296 bits, or 4GB. The addressable range of the 64-bit processor increases significantly: 2^64, or 18,446,744,073,709,551,616, or exabyte (Bai byte).

The partial addressable range provided by the processor architecture is occupied by the OS itself, provided to the operating system kernel and the C runtime (for JVMs written in C or C + +). The amount of memory consumed by the OS and C runs depends on the OS used, but usually in large numbers: Windows defaults to 2GB of memory. The remaining addressable space, expressed in terms of user space, is the memory that is available for the actual process to run.

For Java applications, user space is the memory consumed by the Java process, which actually contains two pools: the Java heap and the native (non-Java) heap. The size of the Java heap is controlled by the Java heap settings of the JVM:-xms and-xmx set the minimum and maximum Java heaps respectively. After the Java heap has been assigned to the maximum size setting, the remaining user space is the native heap. Figure 1 shows the memory layout for a 32-bit Java process:

Figure 1. Example of a memory layout for a 32-bit Java process

In Figure 1, the addressable range has a total of 4gb,os and C runtimes that occupy approximately 2GB of the 1gb,java heap, and the native heap occupies other parts. Note that the JVM itself consumes memory, just as the OS kernel and C run, while the JVM occupies a subset of the native heap.

Java Object Detailed

When your Java code uses the new operator to create an instance of a Java object, it actually allocates more data than you think. For example, the ratio of an int to an Integer object (the smallest object that can contain int) is 1:4, which can be a surprise to you. The extra overhead stems from the JVM's use of metadata to describe Java objects, in this case Integer.

Depending on the version and supply of the JVM, the number of object metadata varies, but typically includes:

Class: A pointer to the class information that describes the object type. For example, for a Java.lang.Integer object, this is a pointer to the Java.lang.Integer class.

Lock: The synchronization information of an object, that is, whether the object is currently synchronizing.

Object metadata immediately follows the object data itself, including the fields stored in the object instance. For a Java.lang.Integer object, this is an int.

If you are running a 32-bit JVM, the layout of the object may be as shown in Figure 2 when you create an Java.lang.Integer object instance:

Figure 2. Example of layout of a 32-bit Java process Java.lang.Integer Object

As shown in Figure 2, 128 bits of data are used to store an int value of 32 bits, while the object metadata occupies the remaining 96 bits.

Java Array Object Detailed

An array object, such as an array of int values, has a similar shape and structure to a standard Java object. The main difference is that the array object contains additional metadata that describes the size of the array. Therefore, the metadata for the data object includes:

Class: A pointer to the class information that describes the object type. For example, for an int field array, this is a pointer to the int[] class.

Tags: a set of tags that describe the state of an object, including the object's hash code (if any), and the shape of the object (that is, whether the object is an array).

Lock: The synchronization information of an object, that is, whether the object is currently synchronizing.

Size: The size of the array.

Figure 3 shows an example of a layout for an int array object:

Figure 3. Example of layout of an int array object for a 32-bit Java process

As shown in Figure 3, 160 bits of data are used to store 32 bits of data in the int value, while the array metadata occupies the remaining 160 digits. For primitives such as Byte, int, and long, a single array is more expensive than the corresponding wrapper object (byte, Integer, or long) for a single field, considering the aspect of memory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.