php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79504 json_decode memory leak
Submitted: 2020-04-21 16:45 UTC Modified: 2020-04-22 07:36 UTC
From: lyrixx at lyrixx dot info Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.4.5 OS: linux
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: lyrixx at lyrixx dot info
New email:
PHP Version: OS:

 

 [2020-04-21 16:45 UTC] lyrixx at lyrixx dot info
Description:
------------
Hello

I notice a bug in `json_decode()`.
With the following script, the memory is not reclaimed.

But if I use `json_decode($content, true);` the memory is well reclaimed.

By the way, I'm not sure it's an issue with JSON, may be it's only about objects

Test script:
---------------
<?php

ini_set('memory_limit', -1);

function m()
{
    echo round(memory_get_usage() / 1024 / 1024, 2) . "\n";
}


m();
$content = file_get_contents('https://gist.githubusercontent.com/lyrixx/3542faea462e5d101d0d755886ea79bd/raw/b83466f2dc61348e414d3507eeb5740f7751e8f3/file.json');
m();
$data = json_decode($content);
m();
unset($content, $data);
m();
gc_mem_caches();
m();
gc_collect_cycles();
m();



Expected result:
----------------
0.56
75.01
754.69
0.56
0.56
0.56


Actual result:
--------------
0.56
75.01
817.65
16.55
16.55
16.55


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-22 06:32 UTC] sjon@php.net
this is not an issue with json, the same can be reproduced by doing simply:

<?php

ini_set('memory_limit', -1);

function m(){ echo round(memory_get_usage() / 1024 / 1024, 2) . "\n"; }

m();

$l=[];
for ($i = 0; $i < 2e6; $i++)
	array_push($l, (object)["url"=>"https://www.example.org/"]);

m();

unset($l);

m();

gc_mem_caches();

m();

gc_collect_cycles();

m();
 [2020-04-22 07:36 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2020-04-22 07:36 UTC] nikic@php.net
PHP reserves 8 bytes per simultaneously live object for the object store. This memory is retained when objects are destroyed, but will be reused when new objects are created.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 09:01:34 2025 UTC