memory alocation

I have a little question about sizo of objects in the memory.

How big would be a difference between this

class a {
b some = new b[1000];
class b{
int a;
int ouch;
}
}

and this
class a {
b some = new b[1000];
}
class b{
int a;
int ouch;
}

and how much memory would eat this:

byte[][] by = new byte[1000000][1];

I tried it on JVM 1.5 beta3 and it looked horrible.
basically 4 MB + 15 MB

In first case, you get 1 extra word for outer class reference. You cannot really compare the memory, as meaning is different. If you put static class b in first case, then it will be the same as second.

byte[][] by = new byte[1000000][1]; is about the worst thing you can do. Array will take at least 16 bytes of memory, maybe even 24 (class pointer, gc/hashcode word, array size, contents, padding to rounded size). so byte[1][1000000] will take around 1MB, and byte[1000000][1] around 16-24MB.

Aw nasty. Isn’t it unnecessary to have additional reference for inner class? It did a nasty things with PPM compressor, 200000 nodes is a little amount.

Yes I forgot on the array size, however, it’s more like 24 - 30 bytes. I rewrote it into a “pointer arithmetic”, and it’s working now much better.

[quote]Aw nasty. Isn’t it unnecessary to have additional reference for inner class?
[/quote]
How will the inner class access the outer object without the reference?

You are aware that in the first case ‘b’ objects have access to all the instance members of the outer ‘a’ object right?

http://www.javaranch.com/campfire/StoryInner.jsp

[quote]I rewrote it into a “pointer arithmetic”, and it’s working now much better.
[/quote]
Why not just swap where you put your index variables? Instead of array[x][y] use array[y][x], then the array can be the more optimal [1][1000000] case.

Just wanted to add to swpalmer’s great explanation. Just remember that arrays are fully fledged objects in Java. Something declared as int[] can be passed into an argument declared as Object. That means you get a lot of class overhead for every dimension of the array above the first.

x = 1 - 1000000
y = 2 - 1000
y passed into another method as a small arrays.