Is there any way I could tell the size of an object/primitive inside of the ram? I may be completely incorrect on how this works. Also, is there any way to tell how much ram is being used by your program?
For specific object usage, find a good profiler. There are many out there. YourKit Java Profiler is awesome, but it’s expensive (they do offer a trial though)
There’s also many ways to figure out how much ram you’re using (in general) without a profiler, you can see how much the JVM is using (but that doesn’t really mean the game is actually using though) by hitting CTRL+Alt+Delete and checking the taskbar. But, if you want something more accurate without using a profiler, what I did was add a debug menu to my game, inside I have a few checkers that show memory usage in megabytes. Mine prints out to a drawString, I edited it to output a println though. But you can stick it in a drawString and display it in your game easily enough if you want.
EDIT: Code corrections
public class YourClass{
private Runtime runtime = Runtime.getRuntime();
private void grabMemUsage(){
System.out.println("Free Memory: "+(runtime.freeMemory() / (1024*1024)));
System.out.println("Total Memory: "+(runtime.totalMemory() / (1024*1024)));
System.out.println("Maximum Memory: "+(runtime.maxMemory() / (1024*1024)));
}
Heh, accidentally whacked “Appreciate” instead of “Quote”. You’re welcome
Primitives are all of a specific size according to the Java Language Spec (eg. 4 bytes for an int or float, 8 for longs and doubles, etc). Hotspot has documented somewhere what the overhead is for objects and arrays (which for example in the 32-bit JVM, is 8 bytes overhead for an ordinary Object and 12 bytes for an array). It’s possibly the same for 64-bit with compressed OOPs, and possibly twice as big without.
Cas
http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
http://Java-Performance.info/overview-of-memory-saving-techniques-java/
According to the link I’ve provided, an object instance w/o any instance variables consumes 12 bytes.
But due to 8 byte alignment, it jumps to 16 bytes. Also, generally we’re gonna need to store its reference.
It means more 4 extra bytes. In short 16 bytes for each object at minimum + 4 bytes for each of its reference! :persecutioncomplex:
int bytes = 30; // 12 overhead + 18 instance variables.
int align = (bytes-1)/8*8 + 8;
System.out.println(align); // 32 bytes consumed.