1

When the user leaves the singup part of the page..the global is no longer needed.

How do I get rid of it..so it does not waste memory?

it is declared in the global namespace as follows:

var local =  
  {  
  client_validation:1, 
  persistent_element:'hide_1' 
  }; 
Steve Adams
  • 79
  • 1
  • 2
  • 9
  • 2
    How much memory do you think that wastes? The answer is to not have it on the page. – Dave Newton Nov 09 '11 at 23:53
  • 3
    It's good to be mindful of memory usage, but worrying about a single JavaScript variable is excessive. – James Johnson Nov 09 '11 at 23:54
  • I think you should probably read this SO question - http://stackoverflow.com/questions/1596782/how-to-unset-a-javascript-variable – mrtsherman Nov 10 '11 at 00:03
  • @mrtsherman I did; if one page doesn't require the variable, don't have it on the page. That's the only cross-browser solution guaranteed to work without relying on the JS engine and whatever JITting it may do. – Dave Newton Nov 10 '11 at 00:05
  • @mrtsherman And that question is for a variable on the same page-removing a defined variable from scope. This question is regarding two pages, only one of which uses the variable. – Dave Newton Nov 10 '11 at 00:07
  • @DaveNewton - I was assuming his sign up was ajax based and the reason he needed to clear the variable. Can you have js globals that survive page changes? I didn't think this was possible which is why I didn't understand your comment - 'the answer is not to have it on the page.' – mrtsherman Nov 10 '11 at 00:10
  • @mrtsherman Hm, didn't see anything about ajax, but could be. No, JS values aren't preserved across pages-but values aren't the only potential artifact of a variable declaration, there's also a symbol table entry, and who knows what else. – Dave Newton Nov 10 '11 at 00:13
  • 1
    `var global = { var page_var; }` is invalid JavaScript. Besides, defining a `global` namespace would be easily confused with the [Global](http://msdn.microsoft.com/en-us/library/52f50e9t(VS.94).aspx) scope. – gilly3 Nov 10 '11 at 00:13
  • @DaveNewton - ack, you obviously know more than I do about what's going on. I wish I understood! – mrtsherman Nov 10 '11 at 00:16
  • @mrtsherman I doubt *that*, but it's among the reasons worrying about a single variable declaration isn't worth the effort--it won't be a gating issue :) – Dave Newton Nov 10 '11 at 00:21
  • Yes, if multiple pages include the same .js file they do _not_ share or retain data via that .js file. – nnnnnn Nov 11 '11 at 01:23

2 Answers2

1

I'm not sure what you mean by "How do I have a global variable that is used for only one of two pages". Global variables are only "global" within the current page. If you navigate to another page they automatically disappear along with everything else on the page you are leaving.

Anyway, if you have some data structure using up memory you can allow the garbage collector to reclaim the memory if you remove all references to the structure.

In your case, either of these statements should do it:

global.page_var = null;
// or
delete global.page_var;

(Assuming you don't have other variables or closures with their own references to the same data structure.)

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
0

Assuming you can still access the variable, this should take care of it:

if (page_var){
    page_var = null;
}
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • It wasn't meant to be condescending, but I can see how it could be taken that way so I've changed it. – James Johnson Nov 10 '11 at 00:17
  • Ok, no worries. I deleted my previous comment. The only addition I'd have to the OP is to be careful and not unset a global variable used by something else (probably, use another approach instead). – Jared Farrish Nov 10 '11 at 00:19
  • I think the best solution is probably to just leave it alone. OP is concerned about memory usage, which is negligible in this case. – James Johnson Nov 10 '11 at 00:28