• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sundar Subbu
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
Android Firefox Browser Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
Android Firefox Browser Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, btw... Welcome to the Ranch Sundar.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic