|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-06-17 15:49 UTC] smacky86 at gmail dot com
Description:
------------
session_start() and session_write_close() don't free unused memory. When they are called multiple times, the memory usage reported by memory_get_usage() constantly grows.
The session data is stored in files, ini settings are default.
# php -v
PHP 5.2.9 (cli) (built: Sep 1 2009 11:43:38)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
with Suhosin v0.9.31, Copyright (c) 2007-2010, by SektionEins GmbH
Test script:
---------------
session_start();
$_SESSION["foo"] = str_repeat("bar", 5000);
session_write_close();
$memory_before = round(memory_get_usage() / 1024, 2) . "KB";
for ($i=0; $i<1000; $i++)
{
session_start();
session_write_close();
}
$memory_after = round(memory_get_usage() / 1024, 2) . "KB";
echo "Before: " . $memory_before . "\n";
echo "After: " . $memory_after;
Expected result:
----------------
Before: 88.34KB
After: 88.34KB
(or something small amount)
Actual result:
--------------
Before: 88.34KB
After: 19906.61K
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 03:00:02 2025 UTC |
In my case I start a database synchronization from a website, send the job to the background, and show the actual result to the user. e.g.: if (isset($_POST["user_clicked_on_the_synch_button"])) { header("Location: ?result_page"); header("Content-length: 0"); header("Connection: close"); session_write_close(); usleep(100); // at this point the browser goes to the result page, and we can start the synchronization process in the background synch(); // this event handler is called after every synchronized product function onSynchEvent() { // here i need to save the actual state of the synch process session_start(); $_SESSION["blah"]++; session_write_close(); } } On the result page I can check this session variable with periodic ajax requests. The session has to be closed due to the file lock. This is why I have to call session start/close multiple times. Since the browser is already detached, it throws the "cannot start session, headers already sent" errors, but even then the data is stored in session.