<p>I have an application that reads a CSV file with piles of data rows.  I give the user a summary 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 and cause <code>OutOfMemoryError</code>s.  Each row translates into an object.  Is there an easy way to find out the size of that object programmatically?  Is there a reference that defines how large primitive types and object references are for a <code>VM</code>?</p>

<p>Right now, I have code that says read up to <strong>32,000 rows</strong>, but I'd also like to have code that says read as many rows as possible until I've used <strong>32MB</strong> of memory.  Maybe that is a different question, but I'd still like to know. </p>

I doubt you want to do it programmatically unless you just want to do it once and store it for future use.  It's a costly thing to do.  There's no sizeof() operator in Java, and even if there was, it would only count the cost of the references to other objects and the size of the primitives.
One way you could do it is to serialize the thing to a File and look at the size of the file, like this:
```
```
Serializable myObject;
ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream ("obj.ser"));
oos.write (myObject);
oos.close ();
```
```
Of course, this assumes that each object is distinct and doesn't contain non-transient references to anything else.
Another strategy would be to take each object and examine its members by reflection and add up the sizes (boolean & byte = 1 byte, short & char = 2 bytes, etc.), working your way down the membership hierarchy.  But that's tedious and expensive and ends up doing the same thing the serialization strategy would do.