473,395 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

memory allocation failuer while creating dynamic HTML tables

Hi there,
I have a web page written on PHP. The PHP script itself receives the
file uploaded by user, parses it and displays HTML table with the
user data - one table row per item. Let's say that (very simplified) PHP
code in question goes like this

[PHP]echo "<table>";
foreach ($user_data as $key => $item ){
echo "<tr>";
echo "<td> Row no: ".$key."</td>";
echo "<td> user item: ".$item."</td>";
echo "/<tr>";
}

echo "</table>";[/PHP]

i.e. each element of user_data array is echoed enclosed by HTML
tags in some form, and sometimes additional table cells that do not contain user supplied data are echoed as well.

Recently I've found that for large user_data array sizes (~500 rows in my case) I'm running into memory allocation problem - I get the
Fatal error: Allowed memory size of XXX bytes exhausted (tried to allocate YYY bytes) ... message and the script dies.

I understand that the problem is that each (event constant, double quoted string with HTML inisde) string is allocated many times and is not freed properly,
and hence the memory gets exhausted.

What is the best way to handle this problem?

Thanks a lot for your help,
Igor.
Feb 5 '08 #1
3 1764
code green
1,726 Expert 1GB
Strange that you are running out of memory for about 500 rows.
Are you aware of the configuration changes you can make in php.ini?
What you are doing is not particularly memory intensive.
understand that the problem is that each (event constant, double quoted string with HTML inisde) string is allocated many times and is not freed properly,
Nothing is freed until you free it or the script ends.
But your code is the slowest method for echoing out HTML.
The concatenation is great but use single quotes instead of double, they are about 4 times faster to parse.
And this could be written in one echo statement, this saves multiple function calls.
Even faster is to close php and write raw HTML then re-open PHP.
But these are mainly speed issues.
I suspect the memory is being consumed in your file handling prior to this
Feb 5 '08 #2
Strange that you are running out of memory for about 500 rows.
Are you aware of the configuration changes you can make in php.ini?
The problem itself happens when PHP runs on the server of my web hosting provider, so I can't play with php.ini there.
What you are doing is not particularly memory intensive.
Nothing is freed until you free it or the script ends.
So is there a way to free memory allocated for echoed strings at the end
of each loop iteration?
But your code is the slowest method for echoing out HTML.
The concatenation is great but use single quotes instead of double, they are about 4 times faster to parse.
And this could be written in one echo statement, this saves multiple function calls.
Even faster is to close php and write raw HTML then re-open PHP.
But these are mainly speed issues.
I suspect the memory is being consumed in your file handling prior to this
I'll check this out...

Thanks,
Igor
Feb 6 '08 #3
code green
1,726 Expert 1GB
So is there a way to free memory allocated for echoed strings at the end
of each loop iteration?
Sorry, got a bit side-tracked by your question
Yes there are the ob() functions which buffer output.
[PHP]ob_start();
echo "<table>";
foreach ($user_data as $key => $item ){
echo "<tr>";
echo "<td> Row no: ".$key."</td>";
echo "<td> user item: ".$item."</td>";
echo "/<tr>";
}
echo "</table>";
$out = ob_get_clean();
var_dump($out);[/PHP]
Feb 6 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: PhoneJack | last post by:
I am writting an algorithm that generates very large tables in dynamic memory on a linux system. These 2 tables are of the order of (30,000 x 30,000). They are being generated by the following...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
7
by: kaul | last post by:
i want to create a 2-d array containg r rows and c columns by dynamic memory allocation in a single statement so that i will be able to access the ith and jth index as say arr how is that...
2
by: Jon Rea | last post by:
Following up on: > Any reason for creating iv dynamically? in "Re: Namespace and #Include best practises" What are the rules for when member data should be created as new memory with a member...
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
4
by: Tomassus | last post by:
Hi there, I have a problem with dynamic memory allocation. I know that it would have been easier to use vectors methods, but i want to know what i do here wrong. This is one of my methods in...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.