Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Size of an arraylist in bytes

Reply
Thread Tools

Size of an arraylist in bytes

 
 
sara
Guest
Posts: n/a
 
      11-20-2011
Hi All,

I create an Arraylist<Integer> tmp and add some integers to it.
Afterward, I measure the size of tmp in bytes (by converting tmp to
bytes array). Assume the result is byte[] C. However, when I update an
element of tmp, and measure size of tmp in bytes again, the result is
different than C!
Why this is the case?

Best
Sara
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      11-20-2011
On 11/20/2011 1:01 PM, sara wrote:
> Hi All,
>
> I create an Arraylist<Integer> tmp and add some integers to it.
> Afterward, I measure the size of tmp in bytes (by converting tmp to
> bytes array). Assume the result is byte[] C. However, when I update an
> element of tmp, and measure size of tmp in bytes again, the result is
> different than C!
> Why this is the case?



We'd have to see some code to give you a good answer, but basically you
can't measure the memory size of Java objects. They change over time,
in ways that C or C++ can't or doesn't, and there's not much to do that
can rectify that.


 
Reply With Quote
 
 
 
 
sara
Guest
Posts: n/a
 
      11-20-2011
Here is the code:

ArrayList<Integer> tmp=new ArrayList<Integer>();
tmp.add(-1);
tmp.add(-1);
System.out.println(DiGraph.GetBytes(tmp).length);
tmp.set(0, 10);
System.out.println(DiGraph.GetBytes(tmp).length);


public static byte[] GetBytes(Object v) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(bos);
oos.writeObject(v);
oos.flush();
oos.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = bos.toByteArray();
return data;
}

The problem is I need to write multiple arraylists on disk and later
on I update the elements of them. I store the starting location of
arraylists and their size such that later I can refer to them. If the
size of objects change then it messes up! Could you please help?
On Nov 20, 1:05*pm, markspace <-@.> wrote:
> On 11/20/2011 1:01 PM, sara wrote:
>
> > Hi All,

>
> > I create an Arraylist<Integer> *tmp and add some integers to it.
> > Afterward, I measure the size of tmp in bytes (by converting tmp to
> > bytes array). Assume the result is byte[] C. However, when I update an
> > element of tmp, and measure size of tmp in bytes again, the result is
> > different than C!
> > Why this is the case?

>
> We'd have to see some code to give you a good answer, but basically you
> can't measure the memory size of Java objects. *They change over time,
> in ways that C or C++ can't or doesn't, and there's not much to do that
> can rectify that.


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-20-2011
On 11/20/2011 4:01 PM, sara wrote:
> Hi All,
>
> I create an Arraylist<Integer> tmp and add some integers to it.
> Afterward, I measure the size of tmp in bytes (by converting tmp to
> bytes array). Assume the result is byte[] C. However, when I update an
> element of tmp, and measure size of tmp in bytes again, the result is
> different than C!
> Why this is the case?


See markspace's response. Another possible point of confusion:
The ArrayList does not actually contain objects, but references to
those objects -- that's why the same object instance can be in three
ArrayLists, two Sets, and a Map simultaneously. In fact, the same
Integer object could appear forty-two times in a single ArrayList:

List<Integer> list = new ArrayList<Integer>();
Integer number = Integer.valueOf(42);
for (int i = 0; i < 42; ++i)
list.add(number);

If you're coming from a C background, a rough analogy is that
the ArrayList holds "pointers" to the objects it holds, not copies
of those objects.

--
Eric Sosman
d
 
Reply With Quote
 
sara
Guest
Posts: n/a
 
      11-20-2011
On Nov 20, 1:30*pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> On 11/20/2011 4:01 PM, sara wrote:
>
> > Hi All,

>
> > I create an Arraylist<Integer> *tmp and add some integers to it.
> > Afterward, I measure the size of tmp in bytes (by converting tmp to
> > bytes array). Assume the result is byte[] C. However, when I update an
> > element of tmp, and measure size of tmp in bytes again, the result is
> > different than C!
> > Why this is the case?

>
> * * *See markspace's response. *Another possible point of confusion:
> The ArrayList does not actually contain objects, but references to
> those objects -- that's why the same object instance can be in three
> ArrayLists, two Sets, and a Map simultaneously. *In fact, the same
> Integer object could appear forty-two times in a single ArrayList:
>
> * * * * List<Integer> list = new ArrayList<Integer>();
> * * * * Integer number = Integer.valueOf(42);
> * * * * for (int i = 0; i < 42; ++i)
> * * * * * * list.add(number);
>
> * * *If you're coming from a C background, a rough analogy is that
> the ArrayList holds "pointers" to the objects it holds, not copies
> of those objects.
>
> --
> Eric Sosman
> esos...@ieee-dot-org.invalid


But do you have any answer to my second question?
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      11-20-2011
Eric Sosman <> writes:
> If you're coming from a C background, a rough analogy is that
>the ArrayList holds "pointers" to the objects it holds, not copies
>of those objects.


An ArrayList /does/ hold pointers (in the sense of Java),
this is not just »a rough analogy«:

»(...) reference values (...) are pointers«

JLS3, 4.3.1.

http://java.sun.com/docs/books/jls/t...ues.html#4.3.1

 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      11-20-2011
sara <> wrote:
> Here is the code:
> ArrayList<Integer> tmp=new ArrayList<Integer>();
> tmp.add(-1);
> tmp.add(-1);
> System.out.println(DiGraph.GetBytes(tmp).length);
> tmp.set(0, 10);
> System.out.println(DiGraph.GetBytes(tmp).length);
>
> public static byte[] GetBytes(Object v) {
> ByteArrayOutputStream bos = new ByteArrayOutputStream();
> ObjectOutputStream oos;
> try {
> oos = new ObjectOutputStream(bos);
> oos.writeObject(v);


The serialization output size of an ArrayList<Integer> depends on
more than just the number of Integer elements in the array. There
is the capacity, which may be larger than the size, but what really
spoils it for you is the Integer-objects, which get serialized along
with the array. If both are same, only one Integer-object gets saved,
but if you change the value for one, then you get two different
Integer-objects serialized along with the actual array, and thus
you get more bytes.

If you need fixed-size records for your arrays (assuming a fixed
size() ), you might be more lucky with arrays of primitives:

If you had:
int[] = new int[2]; tmp[0]=-1; tmp[1]=-1;
and dump that array onto oos, then change tmp[0]=0;
it's very likely, you'll see the same number of bytes
dumped, afterwards.

> oos.flush();
> oos.close();
> bos.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> byte[] data = bos.toByteArray();
> return data;
> }
>
> The problem is I need to write multiple arraylists on disk and later
> on I update the elements of them. I store the starting location of
> arraylists and their size such that later I can refer to them. If the
> size of objects change then it messes up! Could you please help?

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-20-2011
On 11/20/2011 4:44 PM, Stefan Ram wrote:
> Eric Sosman<> writes:
>> If you're coming from a C background, a rough analogy is that
>> the ArrayList holds "pointers" to the objects it holds, not copies
>> of those objects.

>
> An ArrayList /does/ hold pointers (in the sense of Java),
> this is not just »a rough analogy«:
>
> »(...) reference values (...) are pointers«


They're "pointers" in Java's terms, but Java is considerably
more restrictive about what you can do with a "pointer" than C is.
You cannot, for example, print the value of a Java reference; you
can do so in C. You cannot convert a Java reference to or from an
integer; C allows it (with traps for the unwary). Java references
obey a type hierarchy; C's types (and hence the pointers to them)
are unrelated. And so on, and so on: Little niggly differences.
Since Java's references support (and prohibit) a different set of
operations than C's pointers do, I maintain they're as similar as
dogs and wolves, and as different.

Put it this way: If I had told sara "An ArrayList contains
C-style pointers to the objects it holds," would I have been
telling the truth?

--
Eric Sosman
d
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      11-20-2011
On 11/20/2011 1:11 PM, sara wrote:

> The problem is I need to write multiple arraylists on disk and later
> on I update the elements of them. I store the starting location of
> arraylists and their size such that later I can refer to them. If the
> size of objects change then it messes up! Could you please help?



Yes, this is the problem. You have to use something different from an
ArrayList, because the ArrayList does change size.

Look into plain arrays, IntBuffer, DataInputStream and DataOutputStream.

It would also help now if we knew why you want to store multiple
ArraysLists on disk. What is it you are trying to do?

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      11-20-2011
On 11/20/2011 5:04 PM, Patricia Shanahan wrote:
> On 11/20/2011 1:58 PM, Eric Sosman wrote:
> ...
>> Put it this way: If I had told sara "An ArrayList contains
>> C-style pointers to the objects it holds," would I have been
>> telling the truth?
>>

>
> No, but if you had said "An ArrayList contains pointers to the objects
> it holds." you would have been telling the exact truth.


Yes.

> The baggage that C added to pointers was an unfortunate aberration, not
> something that should ever be considered to be the default definition of
> "pointer".


C/C++ pointers has certainly caused a lot of problems over the
years.

But the languages would not have been the same without them. And
I even doubt that they would have been as popular.

C and C++ was not chosen because alternatives without
"do anything you want pointers" did not exist.

Arne

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Size of an arraylist in bytes Stefan Ram C Programming 77 03-08-2012 06:54 PM
Could a struct with size 44 bytes point always points to a char array with size 2024 bytes? eagle_jyjh@citiz.net C++ 8 04-10-2006 03:05 PM
Could a struct with size 44 bytes point always points to a char array with size 2048 bytes? eagle_jyjh@citiz.net C Programming 5 04-09-2006 02:49 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57