-1

Ok, so I am looking for a way to unset ALL variables generated by a page but NOT global variables or Sessions.

I found this in another SO question:

$list_of_vars =  array_diff(get_defined_vars(), $GLOBALS); // Was just get_defined_vars() before Marc B corrected me in his post.
foreach($list_of_vars as $var){
    unset($var);
}

The thing is that in the comments it was said that this unsets() ALL as in ALL variables the include globals and Sessions. I need a way to reset all variables that are NOT global and NOT Sessions

I am doing this for optimizing RAM. If this doesn't help then, is there any other way of optimizing RAM?

user115422
  • 4,662
  • 10
  • 26
  • 38
  • \*facepalm\* This is _not_ how to optimize RAM usage. Use more efficient data structures or query less information. – Peter C Oct 23 '12 at 21:57

2 Answers2

2

This is definitely not the way to go to optimize your script's RAM usage.

You want to identify the problem areas of your script, and work on those.

It's hard to give any hints without knowing what your script does. Here are some pointers to discussion on SO (partly very high-level and/or Framework-specific, but they still contain valuable info):

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Ok, this would make sense, I guessed that the PHP file would auto-unset all vars at the end but wanted to make sure... also I have a page: http://pastebin.com/4GtxexqU (expires in a day). This uses up a I would say the most ram because it has so many queries and fetches. Is there any logical example to what I can do to fix this (not asking you to fix everything but just wondering what I can do or need to look over to avoid an over-use of ram). The page fetches data from the database and echoes it out in HTML some of it can be into the megabytes worth of data... – user115422 Oct 23 '12 at 21:51
  • And is doing $var = $var2; using the ram of $var and $var2? If so is there any other way of renaming a variable? – user115422 Oct 23 '12 at 21:52
  • @muqman I can't go through the code right now, but @nickhar's suggestion is worth following up on: use a PHP profiler, or just [`memory_get_usage()`](http://php.net/manual/en/function.memory-get-usage.php) calls to find out where exactly how much RAM is used. On a general note, you are cramming a lot of pages into one PHP file. You might find the code easier to profile if you separate the various areas – Pekka Oct 23 '12 at 21:57
  • Totally understand about going through the code. I am not asking you to :) and how would i profile? This is how I have setup around 50 of the pages... – user115422 Oct 23 '12 at 22:00
  • I dont see ay obvious places where you'll load excessive data. Get rid of the `SELECT *` no matter what. Loading ALL columns might load data you will not need, including megabytes of image data that got into your database, but you do not remember now. – Sven Oct 23 '12 at 22:01
  • @Sven Ok, `SELECT *` for the table `contents` is needed because I need every bit of information from the page or should i still change it... to naming all my columns? – user115422 Oct 23 '12 at 22:04
  • Yes, name all your columns. What if a new column will be added later, that has a megabyte of data for some rows? Dou you really want to read this if you do not need it? Of course, IF you need it, you'll change the query in this case. – Sven Oct 23 '12 at 22:28
  • sorry for the late "mark as answer" but your answer makes total sense :) – user115422 Nov 15 '12 at 04:41
0

If you're looking to fix heavy RAM usage in your scripts, you'd be better of looking at exactly what your script is doing through profiling it!

nickhar
  • 19,981
  • 12
  • 60
  • 73
  • Try [xdebug](http://www.xdebug.org/) Used it lots in the past. Basically allows you to see what your script is doing at various points and the proc/memory usage during each part. – nickhar Oct 23 '12 at 22:01
  • ok.. how would i say profile this? (http://pastebin.com/4GtxexqU) or any other php? – user115422 Oct 23 '12 at 22:01
  • Oh my - thats a fairly involved script. It may be quite complex to do as you're doing rather a lot there. – nickhar Oct 23 '12 at 22:05