Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How does this (static/hidden/variable) answer look in memory?

 
Ranch Hand
Posts: 477
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Does anyone know how this (static/hidden/variable) answer would look (like) in memory and/or point me to a similar fully illustrated/diagram/picture example? (There's an unrelated Figure 8.4 in the book that I ideally wanted to model this after so that I can accurately work through these types of problems faster.)  

Question is from Sysbex CSG page 361 #22.  


Thanks!
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can probably annotate each line if that helps:

 
Charles O'Leary
Ranch Hand
Posts: 477
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:I can probably annotate each line if that helps


That wonderful explanation was (also) provided in the book.  However, I was looking for something more visually supplemental.
 
Saloon Keeper
Posts: 24595
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to understand that there are two types of storage aggregates in Computer Science: data structures and storage structures.

A Data Structure is a logical (abstract) concept. It's what programming languages use to group associations of data elements together (for example, to define classes in OOP and structs in C and data division items in COBOL).

A Storage Structure is a Data Structure with a clearly-defined layout in memory. In C, for example, given a struct, you can predict precisely where every element in that struct would be located and how big its memory footprint would be - for a given computer architecture and set of compiler options and pragmas.

But Java doesn't work on a physical machine. It runs in a virtual machine, so it doesn't have "RAM" as such. The only allowable access to objects in Java is via its JVM object and property locators. So instead of simple address arithmetic, the JVM implementor is free to store objects (and primitives) in any way it wants to. The Java specs do no demand a specific RAM organization, only that the appropriate items be allocable and referenceable in terms of the JVM's bytecode operations.

So the answer is that there is no way to know what a given variable looks like in memory in Java. Unless you have studied the internals of the particular JVM that you are working with. There are many evil tricks you can do to optimize memory usage and access speeds and the absrtaction of the JVM gives the designer free reins.
 
Charles O'Leary
Ranch Hand
Posts: 477
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For those that don't have the book, I was looking for an equivalent visual to the unrelated attached, if possible.  

However, it's likely that Tim has just answered this question?
Untitled.png
[Thumbnail for Untitled.png]
 
Saloon Keeper
Posts: 1672
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charles:

Besides beating the dead moose that you should always beware for references to static members using instance variables or this, because the exam loves to trick you with them, and should pretty much not be doing that yourself in code that people need to read and understand....

1. What Tim Said

2. There is some stuff to know about the JVM memory model in general, that would not vary or be overly specific to a given JVM implementation.
This could include understanding whether static/class variables were sharing the same heap as instance variables and a lot more.
These questions do come up on interviews, as do various questions about garbage collection not in scope for the exams.

We are in the OCPJP forum at the moment, and I think the question "Hey, are my class/static variables in the heap with my instance variables or somewhere else?" is indeed defined, but if I recall correctly, out of scope for the OCPJP.

This video is short, mostly in English, and seems to address some of what you were curious about:
https://www.youtube.com/watch?v=OJ-ue22ozpM

However, I suspect you are already overflowing with millions of details that ARE in scope for the exams, and should probably not spend more than 8 or 9 minutes on it.

Were you targeting a specific exam?  People might be able to guarantee that worrying about the internals of where primitives, static members, and instance variables are kept in memory are out-of-scope for cert exams, but interesting both to people who like to know what's going on at runtime and interviewers looking for those people.
 
Jesse Silverman
Saloon Keeper
Posts: 1672
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look here:
https://docs.oracle.com/javase/specs/jvms/se11/jvms11.pdf

Which you probably shouldn't unless you are good at keeping yourself from going down Rabbit Holes

Section 2.7 loudly makes Tim's point, Section 2.5 seems to make my point.

I definitely don't have time to look at that now, but it is for sure interesting, and almost as sure probably not in scope for what you are trying to achieve right now.
 
Tim Holloway
Saloon Keeper
Posts: 24595
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Figure 8.4 is misleading. You don't have a separate code instance for the member functions - code doesn't work that way. The code is part of the class object, not the class instance object, whether it's a class method or an instance method, And since a class is an instance of Class, the exact memory location of the code is unknowable. Doubly-so, since the JIT compiler may take the original bytecode and construct machine code on the fly, and that machine code's size and location would be even less predictable.

Also, as I said, the actual memory locations of elements of a class are entirely up to the JVM implementor, so it's not necessarily true that there will be a single block of memory containing the instance properties in adjacent RAM. For example, rather than dedicate a separate byte to each boolean property and allocating properties in order of source code definition, the JVM might scoop them all up and pack them into a single byte or bytes.

If this all sounds like magic, well it is. That's what a JVM is for. Magical elves doing work according to instructions. But they live in a hollow tree with the door bolted shut, so you can't tell what their secrets are and how they follow the instructions, only that whatever they do and however they store it, it will ultimately comply with the Java language spec.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic