visually representing large arrays in a memory efficient way.

how to dump large arrays into formated web pages with little memory

Page 1 of 1

2 Replies - 2392 Views - Last Post: 31 October 2010 - 01:16 PM Rate Topic: -----

#1 justforgetme  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 28
  • Joined: 05-September 09

visually representing large arrays in a memory efficient way.

Posted 31 October 2010 - 12:19 PM

Hello,

i've written this function to visualise arrays I'm working on

 function printOutput($data)
        {
            //ini_set("memory_limit","512M");

            foreach ( $data as $key => $value)
            {
                print '<div style="border:1px solid #008; padding:4px; margin:8px;">'; //

                if (is_array($value)){
                    echo $key.': => (array)';
                    $this->printOutput($value);
                }else{
                    echo $key.': '.$value;
                }

                echo '</div>';
            }

        }


but it's realy nasty with memory when you pass it a big array like for example joomlas $GLOBALS array (which I tried to test it on and my VirtualServer ran out of memory).

what can I do to make it more memory efficient? I thought passing data by refference could help like this:

 function printOutput($data)
        {
            //ini_set("memory_limit","512M");

            foreach ( $data as $key => $value)
            {
                print '<div style="border:1px solid #008; padding:4px; margin:8px;">'; //

                if (is_array($value)){
                    echo $key.': => (array)';
                    $this->printOutput($value);
                }else{
                    echo $key.': '.$value;
                }

                echo '</div>';
            }

        }


but that didn't help either. i tried upping the max memory php can allocate up to 712MB (available system memory atm) but nothing...

As far as i understand the way I have written the function each new invocation needs a bit less memory than its previous one, actively multiplying the size of the variable passed by the arrays depth. is there a different way to handle arrays that just uses the one array?

So, if someone has experience in stuff like this please let me know ok? thanks

Is This A Good Question/Topic? 0
  • +

Replies To: visually representing large arrays in a memory efficient way.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5073
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: visually representing large arrays in a memory efficient way.

Posted 31 October 2010 - 01:10 PM

Are you sure it is taking a bunch of memory or is taking a long time to show on the page? I mean, even large arrays should only be under like 10 MB. If it is taking a long time to print the array to the screen and times out etc, try flushing the output buffer as you go along. When you go to print stuff to the page, if buffering is on (which it usually is by default) it saves the output up and then tries to display it all when the script is done. Sometimes this takes up a bunch of space in the buffer and makes the page appear that it has hung or just not working.

Look at the methods flush() and ob_flush(). At certain periods in the running of the script, you may want to make a call to one of these functions and push the buffered content to the user's screen for viewing.

If you are indeed using large chunks of memory on the order of hundreds of megs, I suggest you load part of the data, process it and display, then load another chunk process and display that etc.

Another thing that could be happening is that you are stuck in an infinite loop. Make sure that there will DEFINITELY be an ending to the script at some point. Try testing it on a smaller array and see if it works out fine. Also you can try using print_r() if you are wanting to see the full array content.

However, I really think what is happening is that you are buffering a lot of content and it is either filling up your buffer and not displaying or some how timing out.

Give those functions a try and see if they help. You would use them after you echo/print something. :)
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,468
  • Joined: 08-August 08

Re: visually representing large arrays in a memory efficient way.

Posted 31 October 2010 - 01:16 PM

Your codes are identical. Neither one passes by reference.

Recursive functions are nice for writing short code, but they can be memory hogs, and trying to pass by reference can be troublesome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1