[HD Pg 12, Sec. 1.6.0 - stack-and-heap]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 12, Sec. 1.6.0 - stack-and-heap]

Post by OCAJO1 »

I came across a note on some website stating that in Java one must be careful not to interchangeably use object references and local object references.

What are they saying? Thanks

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 12, Sec. 1.6.0 - stack-and-heap]

Post by admin »

Difficult to say. Probably they mean static/instance variables vs local i.e. method variables.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 12, Sec. 1.6.0 - stack-and-heap]

Post by OCAJO1 »

Too bad I did not save the page; so all they're saying is that don't mix up local variables (object references) with class variables (object references)?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 12, Sec. 1.6.0 - stack-and-heap]

Post by admin »

OCAJO1 wrote:
Mon Jan 14, 2019 6:20 pm
Too bad I did not save the page; so all they're saying is that don't mix up local variables (object references) with class variables (object references)?
I guess, but even that doesn't make much sense to me.
If you like our products and services, please help us by posting your review here.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

Re: [HD Pg 12, Sec. 1.6.0 - stack-and-heap]

Post by enthunoob »

"In Java, the default stack space size is 64KB but it can be changed at the time of executing the program using command line option -Xss ." on page 17

"Since a reference variable stores only the address of a memory location, the size of a reference variable depends on the addressing mechanism of the machine. On a system with 32 bit OS, a reference variable will be of 4 bytes, while on a 64 bit systems, it will be of 8 bytes." on page 61

So if a reference variable is 8 bytes and the (default) stack size is only 64kb, does this mean only 8000 references can be stored on a stack (by default)?

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

Re: [HD Pg 12, Sec. 1.6.0 - stack-and-heap]

Post by enthunoob »

Besides that, page 61 doesn't say anything about optimization for memory size of reference variables.

For example, on a 64 bit OS system with 32gb ram, if a reference variable points (of all 32gb available locations) to memory location 4, then that value of 4 would fit in a much smaller size. Let's say it's stored in a byte. If the 'pointer-manager' then sees that memory variable, it sees its size and knows the memory location it is pointing to can only be one of the first 256 positions on the stack, so it automatically assumes it is pointing to something within this range. Thus the reference variable doesn't always need a full 8 bytes to store its location-value. In theory. Is there an optimization in the size that is used of reference variables,

Or,

Are they just always 8 byte (no matter the location on the memory it is pointing to)?

Or even if the 64 os machine only has 4gb of ram, it could still do with 4 bytes for one reference variable. Is the reference variable still 8 bytes in that case?

I'm just a 'bit' surprised that a reference variable is always 8 bytes (as is how I read it in this chapter).

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 12, Sec. 1.6.0 - stack-and-heap]

Post by admin »

This is not discussed in detail because it very much outside the scope of the exam. The Java Virtual Machine specification does not actually specify the exact size of a reference variable and leaves it open for the JVM provider. But without any optimization, conceptually, the memory required to store the address address of a memory location would depend on the addressing size of the CPU, (which is 64 bits for a 64 bit machine). Of course, the JVM is free to use optimization as you have written.

Since a programmer never accesses a memory location directly, the discussion on actual size of a reference variable is moot.

>So if a reference variable is 8 bytes and the (default) stack size is only 64kb, does this mean only 8000 references can be stored on a stack (by default)?

Conceptually, yes. You can actually try out a simple recursive code that puts a reference variable on the stack and see how long it takes to bomb. For example:

Code: Select all

public class TestClass {
   public static int count = 0;
   public static void recurse(Object obj){
      count++;
      if(count%1000 == 0) System.out.println("count is "+count);
      Object obj1 = new Object();
      recurse(obj1);
    }

    public static void main(String[] args) throws Exception{
      recurse(new Object());
    }
}
On my machine, it gave a StackOverflowError after 1000 when I ran it with a stack size of 180K (it wouldn't accept anything less than 180k) using this command:
java -Xss180K TestClass

You may inspect the generated class file using javap -c TestClass.class and see the exact number of references being put on the frame and, based on the count, get an estimate on the size it consumes to store a reference.

You may check out section 2.5.2 and 2.6 of JVM specification to know more about how the JVM handles stack, heap, and frame: https://docs.oracle.com/javase/specs/jv ... l#jvms-2.6

Just for make it clear, this is way out of scope of the exam and is something that you will *almost* never need to know as a Java developer.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 61 guests