I can’t test this at the moment but can anyone tell me whether if;
char[][][] arr = new char[128][128][128];
results in a 4 megabyte (128128128*2/1024/1024==4) memory use or more? If it is more I’d love an explanation why that is the issue
I can’t test this at the moment but can anyone tell me whether if;
char[][][] arr = new char[128][128][128];
results in a 4 megabyte (128128128*2/1024/1024==4) memory use or more? If it is more I’d love an explanation why that is the issue
Every object has memory overhead:
You have 128*128 char[] objects
You have 128 char[][] objects
You have 1 char[][][] object
That makes 16,513 objects - arrays even, each roughly adding 12 or 16 bytes, which means it uses ~230K more memory than you expected.
That sounds terrible, can I counter that with a 1D array? But then how can I convert x-y-z to a single number or a single number to x-y-z? I know that it’s x:i%width and y:floor(i/width) for 2D but I can’t figure out 3D
I’m not sure if what you’re asking and what I’m thinking are the same but this is how I get the amount of memory being used.
"Total RAM Used: "+((runtime.totalMemory() - runtime.freeMemory()) / (1024*1024))+"mb"
This should, as far as I know, give you the total amount of RAM being used without counting the heap. Sorry if this isn’t what you’re asking for!
That’ll gve you all the RAM used…he’s just asking for how much the 3d array will use.
I can do the difference with and without the array, hurr
also the question I need the answer to is
[quote]Can I counter that with a 1D array? But then how can I convert x-y-z to a single number or a single number to x-y-z? I know that it’s x:i%width and y:floor(i/width) for 2D but I can’t figure out 3D
[/quote]
Does it matter ?
Since this question is posted under the “Newbie & Debugging” section I would say tackel the problem when it arrives.
Do You really have a memory problem bad enough to be worried about 230k ?
Edit: Unless You just want to be informed about the memory structure and inner workings of java…
If I understand Riven’s explanation correctly each char[] adds between 12 and 16 bytes of overhead, so in my mind that means using a single char[] would eliminate a majority of the overhead added by a char[][][].
edit: should be right now, everytime I write code without looking at it twice
1D index to 3D index and vice versa
val width=..., height=...;
//assert that x < width and y < height
def to1DIndex(x:Int, y:Int, z:Int) = x + y * width + z * width * height
def to3DIndex(i:Int) = (i % width, (i / width) % height, i / (width * height))
230K of memory overhead is perfectly fine. Imagine loading a 1024*1024 image, it will take 4MB of memory. We have gigabytes of RAM… is 230K really terrible??
The bonus you have when using a char[][][] is that it does bounds checking for you. If you’d just use Danny02’s code, and you’d feed in x=-1, y=-2, z=5, there wouldn’t be an error thrown, it would just result into unexpected behavior. So apart from the 1D<->3D convertion, you have to do your own bounds checking for x,y,z values.
Bounds checking and index calculations are all trivial, we say, but for a newbie, it’s highly error prone, given that Danny02 got it wrong too (returned Y axis value is wrongly calculated), and I’d say one is better off with char[][][], and swallow that 230K overhead.
Overhead wasn’t my actual concern, it is the triple nested loop’s performance. Also I see he just mixed y and z, so it’s fine
You asked what the overhead was, then I told you what it was and you said that it was terrible, and now you say it’s not about memory overhead…
Anyway, triple nested loops are not somehow much slower than a single loop. Benchmark your code if you notice it’s slow, don’t make too many assumptions, especially if you’re new to it all.
That’s not the bug, Z values leak into his Y value.
THANKS