php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65300 Memory leak when initializing objects
Submitted: 2013-07-20 06:14 UTC Modified: 2013-07-20 18:31 UTC
From: zlobnygrif at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4.17 OS: debian/ubuntu
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: zlobnygrif at gmail dot com
New email:
PHP Version: OS:

 

 [2013-07-20 06:14 UTC] zlobnygrif at gmail dot com
Description:
------------
If I set object property run-time it uses more memory than equal default property value.

Test script:
---------------
printf("PHP %s %s\n\n", PHP_VERSION, PHP_OS);

class Order {
	public $id     = '123';
	public $number = '1234';
	public $date   = '12345';
}

$orders = [];

printf("Test1\nMem before: %.2f mb\n", memory_get_usage(true) / 1024 / 1024);
for ( $i = 0; ++$i < 100000; )
{
	$order = new Order;
	$orders[] = $order;
}
printf("Mem after: %.2f mb\n\n", memory_get_usage(true) / 1024 / 1024);

unset($orders, $i);
$orders = [];

printf("Test2\nMem before: %.2f mb\n", memory_get_usage(true) / 1024 / 1024);
for ( $i = 0; ++$i < 100000; )
{
	$order = new Order;
	$orders[] = $order;
}
printf("Mem after: %.2f mb\n\n", memory_get_usage(true) / 1024 / 1024);

unset($orders, $i);
$orders = [];

printf("Test3\nMem before: %.2f mb\n", memory_get_usage(true) / 1024 / 1024);
for ( $i = 0; ++$i < 100000; )
{
	$order = new Order;
	
	$order->id     = '123';
	$order->number = '12345';
	$order->date   = '123456';
	
	$orders[] = $order;
}
printf("Mem after: %.2f mb\n\n", memory_get_usage(true) / 1024 / 1024);

unset($orders, $i);

printf("Mem after: %.2f mb\n\n", memory_get_usage(true) / 1024 / 1024);

Expected result:
----------------
no memory leaks

Actual result:
--------------
PHP 5.4.17-1~dotdeb.0 Linux

Test1
Mem before: 0.25 mb
Mem after: 16.25 mb

Test2
Mem before: 4.75 mb
Mem after: 16.25 mb

Test3
Mem before: 5.00 mb
Mem after: 24.25 mb

Mem after: 5.25 mb



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-20 18:31 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2013-07-20 18:31 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

When creating the class we "prefill" the properties. Thanks to copy-on-write we (generally) don't have to copy these values when creating an instance of the object. When reassigning them with the same value those default values will e replaced. We don't compare the values.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 13:01:30 2025 UTC