I am getting:
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 79 bytes) in /path/dbclass.php on line 225

line 225  :  $row   =   mysql_fetch_assoc($res);  
  1. It was 64M; increased to 128M but no luck.
  2. I added ini_set('memory_limit', '-1'); at top of the page but no luck.
  3. I added the php_value memory_limit 512M in the htaccess but no luck. Site went down; got fatal error.

    Options +FollowSymlinks
    RewriteEngine on

    RewriteBase /prolinc/

    RewriteBase /

    RewriteCond %{HTTP_HOST} ^domainname.com$ [NC]
    RewriteRule ^(.*)$ [L,R=301]

    php_value memory_limit 512M

Dani AI

Generated

The error means PHP ran out of heap while building the result in memory. already showed the classic symptom: the script is trying to load more data than the memory limit allows. Raising the limit can hide the problem but does not address the root cause. was right to point at buffering — the goal is to avoid pulling the entire resultset (or large BLOB/TEXT columns) into memory at once.

Quick diagnostics

  • Measure memory before/after the query to see the spike:
    $before = memory_get_usage(true);
    // run the query and fetch a few rows...
    $after = memory_get_usage(true);
    error_log('Delta bytes: '.($after - $before).', peak: '.memory_get_peak_usage(true));
  • Check how many rows and approximate size (SELECT COUNT(*) and sample a few rows with LENGTH() on big columns). Identify BLOB/TEXT columns and whether they must be fetched for every row.

Practical fixes (apply one or more)

  • Fetch and process in chunks instead of loading everything. Key-based pagination is safer than OFFSET for large tables:
    $batch = 1000;
    $lastId = 0;
    while (1) {
    $sql = "SELECT id, col1, col2 FROM big_table WHERE id > $lastId ORDER BY id LIMIT $batch";
    $res = $mysqli->query($sql);
    if ($res->num_rows === 0) break;
    while ($r = $res->fetch_assoc()) {
      // process row
      $lastId = $r['id'];
    }
    $res->free();
    unset($r);
    gc_collect_cycles();
    }
  • Use an unbuffered fetch mode (mysqli or PDO) so PHP does not buffer the whole result. When using unbuffered results you must free the result before issuing another query on the same connection; also be careful with transactions and result metadata.
  • Avoid selecting large columns you do not need; fetch BLOBs with a separate query or stream them to disk.

Memory hygiene and migration

  • After processing large batches unset() big arrays/objects, call $result->free() and gc_collect_cycles() where appropriate.
  • The old mysql_ extension is deprecated. Move to mysqli or PDO to get unbuffered options and better control over fetching.

Follow these steps to identify whether the problem is row count, big columns, or a logic bug that accumulates results in arrays; then apply chunking or unbuffered fetch to fix it.

Recommended Answers

All 3 Replies

Hi,

check if the new value was applied by using ini_get():

echo ini_get('memory_limit');

some hostings does not allow to change the core directives.

It is showing 128M

Ok,

then it can be the result set, that is buffered into PHP and that hits the limit. You can limit the query, but if you are retrieving blob data or large chunk of text even in small data sets you don't solve.

By default, MySQL queries are buffered, but you can avoid this behaviour. You should switch to PDO or MySQLi as the MySQL API is deprecated and now removed from PHP 7.*

But, anyway, instead of mysql_query() try mysql_unbuffered_query() as explained here:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.