views:

162

answers:

6

what i'm looking for is what gets put into the call stack once a function is called recursively, how the arguments are laid on top of each other (are local variables pushed in order? are paremeters pushed in the reverse order?), what bytes exist in an array besides those you asked to have for...

searching on the internet, i've only found simple coding tutorials or information about the java memory model, which seems to be about preventing concurrency and doesn't really explain memory management the way i need.

+3  A: 

Perhaps a read on the Java Virtual Machine Specification will help you.

It explains among other things how java works on Suns VM and/or how they should work when other implement it ( IBM, BEA, etc )

OscarRyz
I think the specification is "what to do", not "how to do"
Dennis Cheung
A: 

I would have replied quite the same as Oscar Reyes did if he hadn't been first. You will definitely find stuff like that in the VM spec.

However, why do you want to know it so exactly, as you can't access this storage in Java anyway?

mh
A: 

use javap and read the ByteCode, your intution will help you much faster and better.

+1  A: 

I think it's definitely worth looking at the Java Language Specification to see if it answers some of your questions. I've also written a few pages about the memory usage of Java objects that may interest you, essentially based on Hotspot. Other sources include white papers published by Sun or other technical documentation produced by your favourite purveyor of JVMs (IBM are also quite reasonable about releasing technical details if you dig around a bit).

If you're feeling particularly "hard core", then you can also download the debug JDK, which allows you to get a dump of all code generated by the JIT compiler (turn on -XX:+PrintOptoAssembly).

You should also ask yourself:

  • do you really care, say, what order method parameters are written to the stack? so long as the answer is "the order in which the called method expects them", what difference does it make? (N.B. If the JIT compiler inlines the method, the answer in some cases could be "it does not write the parameters to the stack"...)
  • can you find the answers to your underlying problem empirically? (e.g. if what you really want to know is "to what recursion depth can I go with a method that takes these parameters and declares these local variables each time", why not just write a test program to find that out from within Java?
Neil Coffey
A: 

Keep in mind when reading the JVM Spec that although it defines how much of Java's memory model should work, it does leave certain areas (like garbage collection) open to interpretation so it might also help to try and find documentation/specifications for the memory model for virtual machines other than Suns.

Kevin Loney
A: 

This whitepaper offers the best introduction I've seen:

http://java.sun.com/j2se/reference/whitepapers/memorymanagement_whitepaper.pdf

Once you understand that document, you can find more detailed resources, such as the GC tuning guides provided by Sun:

http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html

Leigh