Measuring instance memory usage in Java

Survey of memory usage for each instance

In Previous article, I described how to check the memory usage status in Java application. This time, after understanding the process that uses memory, I would like to measure how much memory each instance actually uses.

Introduction of java-sizeof library

There are several libraries that measure Java size, but this time we will use the ** java-sizeof ** library. A library developed by CARROT SEARCH LAB

--Published in Maven Repository. --Supports Java6,7,8.

There are advantages such as. Download the jar file from Maven epository. If you use Maven, put the following in the pom.xml file. For version, specify the version you want to install. This article uses "0.0.5".

<dependency>
    <groupId>com.carrotsearch</groupId>
    <artifactId>java-sizeof</artifactId>
    <version>{version}</version>
</dependency>

How to use

Now, I would like to measure the size of memory using the java-sizeof library. Just call the * RamUsageEstimator.sizeOf * function for the instance you want to measure as follows.

Sample code

import com.carrotsearch.sizeof.RamUsageEstimator;
import java.util.ArrayList;
import java.util.List;

public class JavaSizeofTest
{
    public static void main( String[] args )
    {
    	int[] intArray = new int[10];
    	List<Integer> intList = new ArrayList<Integer>(10);

    	for(int i = 0 ; i < 10 ; i++){
    		intArray[i] = i;
    		intList.add(i);
    	}

    	System.out.println("intArray size(byte) -> " + RamUsageEstimator.sizeOf(intArray));
        System.out.println("intList size(byte) -> " + RamUsageEstimator.sizeOf(intList));
    }
}

output

intArray size(byte) -> 56
intList size(byte) -> 240

I got the size of the instance.

Memory measurement of instance of own class

You can also measure the memory of your own class instance using the above method. You can recursively know the size of the sum of the sizes of all member variables.

Sample code

package so_netmedia.java_sizeof_test;

import com.carrotsearch.sizeof.RamUsageEstimator;
import java.util.ArrayList;
import java.util.List;

public class JavaSizeofTest2
{
    static class MyClass{
        int[] intArray = new int[10];
        List<Integer> intList = new ArrayList<Integer>(10);

        public MyClass(){
            for(int i = 0 ; i < 10 ; i++){
                intArray[i] = i;
                intList.add(i);
            }
        }
    }

    public static void main( String[] args )
    {
    	MyClass myInstance = new MyClass();
    	System.out.println("myInstance size(byte) -> " + RamUsageEstimator.sizeOf(myInstance));
    }
}

output

myInstance size(byte) -> 320

It's the sum of the instance sizes, plus the class overhead.

Sizing in application

By performing the above memory measurement on an instance that seems to be large in size and outputting a log, it is possible to investigate how much memory is used in which instance. Find out which instances are consuming memory and reduce memory. One last thing to note is that it takes some time to measure the memory of ** large instances. ** Do not forget to control, such as measuring in the Staging environment and skipping in production.

Recommended Posts

Measuring instance memory usage in Java
Examine the memory usage of Java elements
Partization in Java
[Java development] Java memory
Changes in Java 11
[Java] Exception instance
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Organized memo in the head (Java --instance edition)
Java No.3 for, stream memory usage useful for business
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Regarding Java variable usage
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
[AWS] Link memory usage of Ubuntu EC2 instance to CloudWatch
Try using RocksDB in Java
About an instance of java
Read binary files in Java 1
[Java] Mirage-Basic usage of SQL
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
I made roulette in Java.
Create hyperlinks in Java PowerPoint