---
title: "In Java, what is the best way to determine the size of an object?"  
description: "In Java, what is the best way to determine the size of an object?"  
author: "Anonymous User"  
published: 2015-08-11  
updated: 2015-08-11  
canonical: https://www.mindstick.com/forum/33409/in-java-what-is-the-best-way-to-determine-the-size-of-an-object  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# In Java, what is the best way to determine the size of an object?

For example, let's say I have an [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) that can read in a [CSV file](https://www.mindstick.com/forum/393/how-to-import-csv-file-in-php) with piles of data rows. I give the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) a [summary](https://www.mindstick.com/forum/33860/how-to-calculate-column-summary-in-sql-server) of the number of rows based on types of data, but I want to make sure that I don't read in too many [rows of data](https://www.mindstick.com/forum/157166/what-is-the-best-and-fast-way-to-insert-2-million-rows-of-data-into-sql-server) and cause OutOfMemoryErrors. Each row translates into an object. Is there an easy way to find out the size of that object programmatically? Is there a [reference](https://www.mindstick.com/forum/774/reference-what-does-this-error-mean-in-php) that defines how [large](https://www.mindstick.com/interview/34471/what-is-a-large-language-model-llm) primitive types and object references are for a VM?\
[Right now](https://answers.mindstick.com/qa/36863/what-would-happen-if-gravity-became-5-percent-stronger-right-now), I have code that says read up to 32,000 rows, but I'd also like to have code that says read as many rows as possible until I've used 32MB of memory. Maybe that is a different [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time), but I'd still like to know.

## Replies

### Reply by Anonymous User

You can use the java.lang.instrument package\
Compile and put this class in a JAR:\

```
import java.lang.instrument.Instrumentation;public class ObjectSizeFetcher {    private static Instrumentation instrumentation;    public static void premain(String args, Instrumentation inst) {        instrumentation = inst;    }    public static long getObjectSize(Object o) {        return instrumentation.getObjectSize(o);    }}
```

Add the following to your MANIFEST.MF:\
Premain-Class: ObjectSizeFetcherUse getObjectSize:\

```
public class C {    private int x;    private int y;    public static void main(String [] args) {        System.out.println(ObjectSizeFetcher.getObjectSize(new C()));    }}
```

Invoke with:\
java -javaagent:ObjectSizeFetcherAgent.jar C


---

Original Source: https://www.mindstick.com/forum/33409/in-java-what-is-the-best-way-to-determine-the-size-of-an-object

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
