Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Will I get the exact amount of memory usage?  (Read 956 times)
0 Members and 1 Guest are viewing this topic.
Offline Quarry
« Posted 2013-05-13 16:15:45 »

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 (128*128*128*2/1024/1024==4) memory use or more? If it is more I'd love an explanation why that is the issue
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 803
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2013-05-13 16:23:12 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Quarry
« Reply #2 - Posted 2013-05-13 16:32:50 »

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #3 - Posted 2013-05-13 17:16:45 »

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.

1  
"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!
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2013-05-13 17:18:03 »

That'll gve you all the RAM used...he's just asking for how much the 3d array will use.
Offline Quarry
« Reply #5 - Posted 2013-05-13 18:21:16 »

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
Offline xsvenson
« Reply #6 - Posted 2013-05-13 19:27:16 »

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...

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Offline Z-Man
« Reply #7 - Posted 2013-05-13 19:43:36 »

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[][][].
Offline Danny02
« Reply #8 - Posted 2013-05-13 19:53:01 »

edit: should be right now, everytime I write code without looking at it twice

1D index to 3D index and vice versa
1  
2  
3  
4  
5  
6  
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))
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 803
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2013-05-13 20:26:52 »

That sounds terrible
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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Quarry
« Reply #10 - Posted 2013-05-13 20:40:45 »

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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 803
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2013-05-13 20:45:14 »

Overhead wasn't my actual concern, it is the triple nested loop's performance.
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.

Also I see he just mixed y and z, so it's fine
That's not the bug, Z values leak into his Y value.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Quarry
« Reply #12 - Posted 2013-05-13 20:48:53 »

THANKS
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (7 views)
2014-10-02 14:36:20

Pippogeek (41 views)
2014-09-24 16:13:29

Pippogeek (32 views)
2014-09-24 16:12:22

Pippogeek (22 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (30 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!