CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: PHP and Memory

  1. #1
    Join Date
    Sep 2007
    Posts
    28

    PHP and Memory

    Hello
    Im building a browser based game (text and images)

    I have a debugger/benchmark script that I run at every opage load which gives me various information. One thing it logs is script exectution times (including how long it took for all of the php to load and execute) as well as memory usage.

    Im developing on a shared host so I guess things differ a bit when I go dedicated, but I have noticed a fully loaded profile page takes around 0.05 - 1 second to execute and consumes 1.1 - 1.3 MB of memory (using phps memory_get_usage() function)

    Im wondering if that much memory is a bad thing or not... Imagine 100 users load a page, then imagine 10,000 users. Granted if the system had 4GB+ of memory it might be able to handle it but it seems high.

    Its not a script more than its all scripts that run before the server sends the html to the browser.

    But these scripts are working to build the HTML page loading profile info from a db query or sessions, performing calculations, accessing classes, functions etc..

    And at the end stripping whitespace from the HTML (using ob_start with a strip function)

    Weird thing is, I have very optimized queries, and I never use *
    I free my results all the time and I never get big results anyway (we are talking 1000 bytes max in most cases, sometimes more)

    I have a debugger class that handles all this info, Ill post an example of what it spits out:

    Example 1 Initial page load where I run all queries and store info in session data:
    http://img651.imageshack.us/img651/3912/68408024.jpg

    Example 2 second page load (same one) where we skip most queries due to session info being present:
    http://img27.imageshack.us/img27/779/31897689.jpg

    I dont expect you guys to understand it all, but bascialyl I load all the modules I need first then run my scripts for that particular page.
    MU is memory usage, and I also put memory usage and peak usage on top and bottom of the output. For memory usage the first value is the memory size and the second value is the real usage (real allocation)

    It seems most of the data comes from loading the modules, but the modules themselves are just full of functions or classes, and they dont go over 100KB in size total.

    So Im not sure why the memory usage is so high.

    Any ideas?

    edit: as you can see, my queries and subsequent result usage etc only really use up to 100KB MAX of memory. So it couldnt be my queries. Even the first image shows that a lot of queries and calculations does little to add to the memory usage.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: PHP and Memory

    Memory consumed isn't made up of just the file sizes of the documents containing the functions/methods but also the memory allocated by the creation of variables and calculations.

    When you are done with variables, are you unsetting them? That is key if you're developing large processing web applications.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Sep 2007
    Posts
    28

    Re: PHP and Memory

    Those files just contain functions or classes, none of those functions or classes are even called at the time those files are loaded (they are called after) and there is little to no variables outside functions and classes in those files. Nevertheless, I once read that there is no point in unsetting variables inside a functions scope. Is this true?

    I already looked at unsetting but it seems it would be useless if just loading the files and functions within them takes 1MB.

    I made sure that the modules I loaded never started to call functions just by loading them.

    So when my page is executed, it includes those files which contain functions or classes, then starts running whatever smaller scripts I have (outside those modules) which call on those functions or classes. Granted even the classes that I use are only started once (no multiple new initializations)

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: PHP and Memory

    Once a function is finished with execution, all non-global variables will automatically be destroyed. But, unsetting them within a function can help clear memory at the time...especially if a function is heavy. However, if it's a small function with little processing, don't worry about it.

    Out of curiosity, what function are you using to convert the memory to KB? Is it a custom function you have written?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Sep 2007
    Posts
    28

    Re: PHP and Memory

    Thanks, most of my functions dont exceed 5-10 lines of code...which confuses me more!

    As for the memory usage, I use php for that:

    round((memory_get_usage() /1024), 2)

    and

    round((memory_get_usage(true) /1024), 2)



    Im developing on shared hosting, but on a linux system using apache, PHP5.2.

  6. #6
    Join Date
    Sep 2007
    Posts
    28

    Re: PHP and Memory

    On another note

    If I have a function like so:

    function hello($a, $b, $c)
    {
    //stuff here using $a, $b and $c, arrays, exploding etc...
    }

    none of the passed in variables would need to be unset right? Those variables could be strings, ints, arrays etc...


    Global I asusme you mean a varible that was initiated outside the function and not passed in to it.

  7. #7
    Join Date
    May 2002
    Posts
    10,943

    Re: PHP and Memory

    If you're using real_usage, then I can understand the large memory allocation. When you use real_usage, you're also getting a return on what Apache and PHP has to allocate for the loading of it's modules and methods to read the data you have.

    And no, garbage collection takes care of all parameter allocations.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured