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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 05:01:33 2024 UTC