• Post Reply Bookmark Topic Watch Topic
  • New Topic

Where do you live in my memory?  RSS feed

 
Miles Williams
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone.

I have another absolute beginners question that I probably should know the answer to after 3 months of playing with Java but I have either not come across the information or have forgotten it.

What I would like to know is where certain things live in memory.

Now.... I "think" that all instance variables live on the heap along with any objects created using the 'new' keyword. That static variables have their own "special" storage area (does anything else live in this region or is it only statics?) And that variables created inside a method live on the stack. Am I right about these things?

What I am totally unaware of though is where any values passed in as arguments to a method live? And when passing the main() method of a console application any values upon execution from the windows command prompt, do these live in the same place as any other arguments passed to methods? I am guessing yes considering that main() is just another method but just want to confirm. Also the variable you use to iterate through in a for loop eg.

or

Where does that integer i live?

Also. When you give a method parameters, is the storage for the parameters created before any arguments are passed into it?


When is storage created for the int i being passed to the doIt() method?
 
Ben Allen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a fun question and I wish I had an answer for you. You motivated me to fire up Cheat Engine ( http://www.cheatengine.org/ ) and look at memory while running a Java program. It's neat. You can search for variable names by string, or integer values by 4-byte chunk, doubles by doubles, and so on. Just give them distinctive names so they're easy to single out, and look at the memory while it runs.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Values passed as parameters to a method (or constructor) are treated just the same as local variables inside that method. As for exactly when they are created, I have no idea and I don't think it's useful to find out.
 
Miles Williams
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Values passed as parameters to a method (or constructor) are treated just the same as local variables inside that method. As for exactly when they are created, I have no idea and I don't think it's useful to find out.


So they live on the stack then? And the arguments passed in to the main() method when invoked at the command prompt are the same too?
And I agree that there's not much use in knowing when the storage is actually allocated to parameters or arguments ( I've seen people use these two terms interchangeably but I think there is a slight difference. Parameters are what your method expects to receive and arguments are what actually get passed to it - I don't know the "official" definitions and whether I am right or wrong but that's the way I look at it) but just for completeness and peace of mind I would like to know. I am the kind of person who gets caught up in minutiae, which I know can become quite a hindrance to learning some things but hey that's just me.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the BCEL handbook? Start here. That might help, though it is rather old.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miles Williams wrote:So they live on the stack then? And the arguments passed in to the main() method when invoked at the command prompt are the same too?

I would presume that all methods behave the same, but the simple fact is that it's just not important - and believe me, if Paul doesn't know, then it's probably not worth knowing.

I am the kind of person who gets caught up in minutiae, which I know can become quite a hindrance to learning some things but hey that's just me.

Then I suggest you change your habits, or maybe read the DontSweatIt page, which has exactly this question as a topic.

Java is a memory-managed language, and it was made so precisely so that you don't have to worry about things like this.
Unless you need it for the SCJP exam - in which case, you should probably read the study guide (which still seems to be the latest version) - you're almost certainly wasting your time. And we'd hate you to do that.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please justify what you mean about memory not being well managed.
Agree people should use singletons correctly: that means never.
 
Ben Allen
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Unless you need it for the SCJP exam - in which case, you should probably read the study guide (which still seems to be the latest version)


Come out from that cave, Oracle has owned Java for over four years
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Allen wrote:Come out from that cave, Oracle has owned Java for over four years

Yeah, more's the pity (I worked at Sun for a while). They've threatened a new guide for OCJP7 for 2014, but I don't see any sign of it as yet - not that I could afford the exams anyway now that those turkeys have taken over.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!