12

Is there a way to see where the variable is located in the memory?

In ActionScript for example in debug mode you can see the memory location of the variable

I am using Google Chrome developer tools where I can see the variables in debug mode but there is no info about the memory location of the variable.

Are there any browser tools that show variable memory location?

4
  • 1
    I'm curious, why would you need -- or even want -- to know this? Commented Oct 12, 2013 at 12:12
  • 1
    Can be very useful in debugging some times. If there are two variables with same name you can differentiate them by the memory location. If you debug using 'Step into next function call' sometimes you can easily follow the variable if you know it's memory location. Commented Oct 12, 2013 at 12:18
  • @onetwo12: There cannot be two variables with the same name. If they're in different scopes, then either one of them is inactive or the one shadows the other. The scope chain does show them to you. Commented Oct 12, 2013 at 13:48
  • 1
    @Bergi: In JavaScript you can see which 'this' variable is in the function scope by knowing the memory location of the 'this' variable. Commented Oct 12, 2013 at 14:49

2 Answers 2

11

Take a memory snapshot. This will show in detail what kind of objects are floating around, where and how many they are.

Example view
(source: google.com)

Of course, it won't show you the exact memory or register addresses, but you hardly will need those for debugging javascript.

Sign up to request clarification or add additional context in comments.

2 Comments

I think this image needs to be updated with a new one...
@EugenSunic Why, the answer is just 7 years old :-) Though the screenshots from the current article (link updated) don't seem to be very recent either.
7

No, it isn't possible. Due to way javascript interpretes works, it is quite hard to get memory address.

5 Comments

Could you elaborate on the reasons? Values may be in registers, but that's a problem for any debugger and they solve it quite well.
Not only in registers. For example V8 internally represents some variables as objects that merges values from many variables. Its not simple to compute which variable is actually assigned to specific object and how much memory is occupated by single js variable. Thas the reason why tools for getting direct variables adress arent exists.
But it can show the current values, so it clearly does figure out where each variable is located. Even if that logic is not sound and all else fails, you can still turn off the problematic optimizations when running w/ debugger on.
Are you actually interested if two variables a and b are the same object? You could use === to see if they are equal. So the JSVM definitely have an id concept but it is not exposed. Beware that DOM object also has id field that maps to the id attribute. See here for some example to implement your own identifier.
@leesei what if they're in different environments (for instance, in a testing script versus the actual script the test is testing?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.