Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  Variable size calculation  (Read 667 times)
0 Members and 1 Guest are viewing this topic.
Offline pixelprime

Junior Member


Medals: 3



« Posted 2013-04-04 11:22:45 »

Just a quickie: when trying to calculate the maximum memory footprint of variables in my class, is this the correct assumption?

1  
2  
3  
4  
5  
6  
7  
8  
// one byte
byte value = 10;    

// 10 bytes maximum? Would this be zero-bytes until an element is initialised with a value?
byte[] values = new byte[10];    

// 10,000 bytes maximum? (9.7kb). Again, do the rules with the above array apply re: initialisation?
byte[][] valuesMany = new byte[100][100];    


Thanks for any advice offered here!
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2013-04-04 12:17:31 »

Afaik memory gets allocated as soon as the compiler comes to a 'new' statement (roughly).
What your are talking about is something called 'lazyness'. It's a feature of other languages, which try to only compute / allocate stuff, when the stuff is needed. Some languages are completely lazy, like Haskell, every value is lazy there, others allow specifying if you want your stuff to be lazy, for example scala, which has the 'lazy' keyword.

Anyways. One more thing I'd like to say:
Arrays are pointers to memory locations where the array saves it's values.
So if you create an array like in your example the 'values', then the local variable 'values' gets an integer assigned, which says where to look for the data inside the memory.

This means if you allocate 'values' you actually allocate 10 bytes + 4 bytes for the integer pointer (on 32 bit JVM's, 8 byte on 64 bit JVM's).
(this is not true for your
byte value = 10;
example, but true for
Byte byteWrapper = 10;
, but that's another story)

If you have a so-called two-dimensional array, then you really have arrays inside an array. This means your 'valuesMany' looks like this actually, where each [ ] pair is an array (and with that also a pointer).


[
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
]


What does that mean?
Each "[" is another pointer...:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
byte[][] valuesMany = new byte[][] {
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
}

(^^^ pseudocode ^^^)
See these lots of 'new' keywords? Wink

In the end this means you have 10 * 10 bytes (of data) + 11 pointer in case of a 64 bit JVM this would be 10 * 10 + 11 * 8 byte = 188 bytes instead of the expected 100 bytes.

Thats it with todays lesson Cheesy

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline pjt33
« Reply #2 - Posted 2013-04-04 12:52:36 »

It's actually worse than Matheus says, because an array is an object and has an object header overhead. See http://www.javaspecialists.eu/archive/Issue078.html for an old approach; or experiment with java.lang.System's information on currently used memory.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pixelprime

Junior Member


Medals: 3



« Reply #3 - Posted 2013-04-04 14:26:11 »

Thanks for this detailed and thorough explanation. I guess I must have forgotten about the inevitable assignment of a pointer to the array's memory address.

Presumably, arrays are contiguous in memory (i.e. only require a single address location and size terminator), so that would assume that the initial assignment pointer will always remain a fixed size (that being the size required to define a memory address) and never grow any larger with different sized arrays.

It's a shame that many languages now adopt the 'everything is an object' methodology - especially with primitive types. I like bytes to stay as bytes and not come with the costly overhead of having additional object requirements clogging up memory space.

Thankfully, with the (relatively) massive amounts of memory available on modern systems, these concerns are fairly trivial. But I can never let that be an excuse for sloppy, wasteful programming practices - especially with fundamental, basic data types!

Thanks again for your helpful explanations!
Offline Roquen
« Reply #4 - Posted 2013-04-04 15:35:38 »

A single dimensional array is fixed size and is a linear memory chunk.  All multi-dimensional arrays are really arrays of references to arrays.  I'm not sure what you're saying about "everything is a object"...because java isn't one of those languages.
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #5 - Posted 2013-04-04 16:17:13 »

I'm not sure what you're saying about "everything is a object"...because java isn't one of those languages.

He's talking about arrays being classes too. So there is a
int[].getClass()
, actually. That means int[] has to store class information, etc. This is a known overhead from instances.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Roquen
« Reply #6 - Posted 2013-04-04 16:36:29 »

[Devil's advocate mode] If you have the C expression:

int* array = malloc(sizeof(int)*n)

There's heap memory manager overhead with the memory chunk similar in size to Java's overhead.  But really I was referring to the "... especially with primitive types. I like bytes to stay as bytes...".
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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

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

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!