Activity Stream
48,167 MEMBERS
61066 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Results 1 to 10 of 10
  1.     
    #1
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com

    Default PHP TIP: Unset() : Memory Usage!

    This is just a tip for when your building your applications in PHP and why you should use the unset() function.

    For unset there are 2 main reason why you would use such a command.

    1. To unset a variable so you can redeclare it later as a fresh variable!
    2. Reduce memory usage dure the execution of your script


    Now the main problem in PHP scripts is keeping large variables throught the whole execution of the script, causing the script to slow down dramatically after the variable is set..

    here is an example of not using unset and the ram levels changing.

    PHP Code: 
    //First we check the state of the memory at the start.

    echo memory_get_peak_usage() . "\n"// 36640

    /*
        * as you can see above the ram usage is at 36640.. 36.6Mb
        * but dont forget the 36.6Mb is also apache,mysql and other services
    */

    //Lets run a test to see if we can get it to peak

    $a str_repeat("KWWHunction"4000); // repeat a string 4000 times


    //Lets check what effect that above has had on the memory usage
    echo memory_get_peak_usage() . "\n"// 60765

    /*
        * As you can see the memory is 60.7Mb thats 24.1Mb increase
    */ 
    Now if you carry on coding the memory usage will just grow as the variable $ is still in the memory waiting to be used again...

    but if theres no use for $a, so lets take a look at the effect if we unset the variable after we have finished with it..

    PHP Code: 
    //First we check the state of the memory at the start.

    echo memory_get_peak_usage() . "\n"// 36640

    /*
        * as you can see above the ram usage is at 36640.. 36.6Mb
        * but dont forget the 36.6Mb is also apache,mysql and other services
    */

    //Lets run a test to see if we can get it to peak

    $a str_repeat("KWWHunction"4000); // repeat a string 4000 times

    //UNSET HERE
    unset($a);

    //Lets check what effect that above has had on the memory usage
    echo memory_get_peak_usage() . "\n"// 38230

    /*
        * instead of a 24.1Mb increase we only have a 0.2Mb increase
    */ 
    I hope you can see the importance of unset() and how it will help you make your site load a HELL of a lot faster, especially if your new to programming in PHP and you do stuff with code when you don't fully understand what it does to your memory usage.

    Peace out
    litewarez Reviewed by litewarez on . PHP TIP: Unset() : Memory Usage! This is just a tip for when your building your applications in PHP and why you should use the unset() function. For unset there are 2 main reason why you would use such a command. To unset a variable so you can redeclare it later as a fresh variable! Reduce memory usage dure the execution of your script Now the main problem in PHP scripts is keeping large variables throught the whole execution of the script, causing the script to slow down dramatically after the variable is set.. Rating: 5
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  2.   Sponsored Links

  3.     
    #2
    Member
    Website's:
    CodeSociety.net
    ur right, i completely underestimated this, now i feel like a retard! shit. also closing mysql connections too when they are done. oh well never to late!

    Thanks bro



  4.     
    #3
    Member
    Website's:
    warezxtc.com
    Always knew about this but never bothered about it :/

  5.     
    #4
    Respected Developer
    Website's:
    PlatinumW.org NexusDDL.com HD-United.org CheckLinks.org FLVD.org
    Using too much unset is not a good idea as well.

  6.     
    #5
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    Yes Dman but lets say you had a 20 40Mb clustors being used, im sure 14 unsets would not peak the memory and will be better all round..


    the reason i tell these tales to you is alot of people leave like resources open suchs mysql cons,and file wrappers etc and they need to be unset for the better performance and cyber you should know better, treat your pc with love my friend because no matter what.. it will always be better that your girl looool
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  7.     
    #6
    (╯?□?)╯︵ ┻━┻
    Website's:
    Xenu.ws WarezLinkers.com SerialSurf.com CracksDirect.com
    You'd have to have a rather large script to be honest.

    I only really unset things in huge pieces of code I write. In your usual standard script that isn't made for some huge traffic site you don't really need to do it.

    Dman does have a point too. If you use unset a lot it can also make code look messy and sometimes remove vars which would be useful in future.

    Apart from those points though, it *can* be good practice on large scales.
    Projects:
    WCDDL - The Professional DDL Script
    Top Secret Project: In Development - ZOMG
    ImgTrack - Never Have Dead Images Again!

  8.     
    #7
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    ok lets say your script run in like a sequence!!

    Frist you start up and load constants then a registry then DB ect.. you could unset after each sequence like this


    LOAD Config/Constants
    LOAD DATABASE - unset(...,...,...,...,...,);
    LOAD MIAN CLASS unset(...,...,);

    unset takes multiple vats so if you unset the vars after each block of code.. block as like 6 classes for a certain task im sure that would be clean and better than not using it...

    Also id rather my site be the best performance than keep my code 100% clean

    But both your points are right
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  9.     
    #8
    Respected Developer
    I used to have the habit of unset()'ing 80% of all vars i used back in the days, made the script slower instead (obviously)

  10.     
    #9
    (╯?□?)╯︵ ┻━┻
    Website's:
    Xenu.ws WarezLinkers.com SerialSurf.com CracksDirect.com
    Quote Originally Posted by litewarez View Post
    ok lets say your script run in like a sequence!!

    Frist you start up and load constants then a registry then DB ect.. you could unset after each sequence like this


    LOAD Config/Constants
    LOAD DATABASE - unset(...,...,...,...,...,);
    LOAD MIAN CLASS unset(...,...,);

    unset takes multiple vats so if you unset the vars after each block of code.. block as like 6 classes for a certain task im sure that would be clean and better than not using it...

    Also id rather my site be the best performance than keep my code 100% clean

    But both your points are right
    Yes, you can do that. But it will only be a microoptimisation unless done in a large script is what im saying.
    Projects:
    WCDDL - The Professional DDL Script
    Top Secret Project: In Development - ZOMG
    ImgTrack - Never Have Dead Images Again!

  11.     
    #10
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    yea so the best time to use it mis only on LARGE entities then but non the less people know now that it can be REALLY Helpfull if used correctly
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [HELP] High memory usage on the server
    By damnyou in forum Technical Help Desk Support
    Replies: 9
    Last Post: 16th Mar 2012, 09:00 PM
  2. To much memory usage
    By WarezFreaks in forum Technical Help Desk Support
    Replies: 18
    Last Post: 11th Aug 2010, 04:59 AM
  3. high memory usage help
    By accyuklad in forum Hosting Discussion
    Replies: 5
    Last Post: 6th Jul 2010, 04:45 PM
  4. Memory Usage
    By -Im.z2ight- in forum Server Management
    Replies: 3
    Last Post: 21st Mar 2010, 09:08 PM
  5. [HOW TO]Lower Your FireFox Memory Usage
    By Pyro in forum Tutorials and Guides
    Replies: 12
    Last Post: 3rd Aug 2009, 03:38 AM

Tags for this Thread

BE SOCIAL