aspose file tools*
The moose likes Java in General and the fly likes to find the size occupied by arraylist in kilobytes - i want to find the size of an arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "to find the size occupied by arraylist in kilobytes - i want to find the size of an arraylist" Watch "to find the size occupied by arraylist in kilobytes - i want to find the size of an arraylist" New topic
Author

to find the size occupied by arraylist in kilobytes - i want to find the size of an arraylist

Sundar Subbu
Greenhorn

Joined: Sep 16, 2011
Posts: 3
I want to find the size of an arraylist whether it exceeds 4MB or not. I tried using some methods but nothing is showing the size. I need this for my project.

Source code is attached with this. Arraylist is populated values through sql queries. My requirement is how to check the size of this arraylist. Please help me.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

What is the exact spec?

You are running your application in an environment where you only have a little more than 4 Mb to give to the VM?
You need to send the items in the array list in a serialized of buffered way and that can not be larger than 4 Mb?


If you REALLY want to know the byte size of an object, the "trick" is to convert it to an byte[] and look at the size of it. That is done like this:



NOTE: This is serialized object size, not in memory object size as object is in java. For that, you need to use instrumentation, since it depends on the JVM and memory block size.

Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

And, btw... Welcome to the Ranch Sundar.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Ove Lindström wrote:If you REALLY want to know the byte size of an object, the "trick" is to convert it to an byte[] and look at the size of it.
...
NOTE: This is serialized object size, not in memory object size as object is in java. For that, you need to use instrumentation, since it depends on the JVM and memory block size.

Exactly... the serialization "trick" tells you very little about how much memory the ArrayList and all the elements that it refers to take up, so it's not very useful...

Java does not have an easy way to find out how much memory an object takes up in memory. There are some ways to do it, look for example at this project which does it by using the Java Agent API.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
 
subject: to find the size occupied by arraylist in kilobytes - i want to find the size of an arraylist