Easily measure the size of Java Objects

code

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class ObjectUtils {
	public static int getObjectSize(Object o) {
		ObjectOutput out = null;
		try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
			out = new ObjectOutputStream(bos);
			out.writeObject(o);
			out.flush();
			byte[] b = bos.toByteArray();
			return b.length;
		} catch (IOException e) {
			return -1;
		}
	}
}

How to use

	public static void main(String[] args) {
		{
			String o = "ABC";
			System.err.println(ObjectUtils.getObjectSize(o));
		}
		{
			Integer o = 100;
			System.err.println(ObjectUtils.getObjectSize(o));
		}
		{
			String[] o = { "ABC", "ABC", "ABC" };
			System.err.println(ObjectUtils.getObjectSize(o));
		}
	}

Execution result example

10
81
60

Recommended Posts

Easily measure the size of Java Objects
Measure the size of a folder in Java
[Java] Delete the elements of List
[Java version] The story of serialization
Sort a List of Java objects
The origin of Java lambda expressions
Output the difference between each field of two objects in Java
[Read Effective Java] Chapter 2 Item 5 "Avoid the creation of unnecessary objects"
Get the result of POST in Java
Check the contents of the Java certificate store
Examine the memory usage of Java elements
[Java] Get the day of the specific day of the week
Memo: [Java] Check the contents of the directory
Compare the elements of an array (Java)
[day: 5] I summarized the basics of Java
What are the updated features of java 13
Looking back on the basics of Java
Output of the book "Introduction to Java"
The story of writing Java in Emacs
[Java] Check the number of occurrences of characters
[Java] [Spring] Test the behavior of the logger
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
JAVA: jar, aar, view the contents of the file
The story of making ordinary Othello in Java
[Android] [Java] Manage the state of CheckBox of ListView
About the description order of Java system properties
About the idea of anonymous classes in Java
[Java] Access the signed URL of s3 (signed version 2)
The story of learning Java in the first programming
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
I compared the characteristics of Java and .NET
[Java] Be careful of the key type of Map
Summarize the life cycle of Java objects to be aware of in Android development
[Java] How to easily get the longest character string of ArrayList using stream
Calculate the similarity score of strings with JAVA
Try the free version of Progate [Java II]
[Java / Kotlin] Resize considering the orientation of the image
I touched on the new features of Java 15
The date time of java8 has been updated
Measure the distance of the maze with breadth-first search
[Java] How to get the authority of the folder
Import files of the same hierarchy in Java
[Java] Overview of Java
First touch of the Files class (or Java 8)
Java Welcome to the Swamp of 2D Arrays
Try the free version of Progate [Java I]
Java General-purpose method that retrieves only the differences between the properties of objects of the same class
Dreaming of easily creating a Web API for the DB of an existing Java system
[Java] How to get the URL of the transition source
Get the URL of the HTTP redirect destination in Java
CI the architecture of Java / Kotlin applications with ArchUnit
How to write Scala from the perspective of Java
Please note the division (division) of java kotlin Int and Int
The comparison of enums is ==, and equals is good [Java]
Do not easily round the result of decimal calculation
[Java] Check the JDK version of the built war file
[For beginners] Quickly understand the basics of Java 8 Lambda
Use Java lambda expressions outside of the Stream API