-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Adjust GC threshold if num_roots higher than threshold after collection #13758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--TEST-- | ||
GH-13670 001 | ||
--FILE-- | ||
<?php | ||
|
||
register_shutdown_function(function () { | ||
global $shutdown; | ||
$shutdown = true; | ||
}); | ||
|
||
class Cycle { | ||
public $self; | ||
public function __construct() { | ||
$this->self = $this; | ||
} | ||
public function __destruct() { | ||
global $shutdown; | ||
if (!$shutdown) { | ||
new Cycle(); | ||
} | ||
} | ||
} | ||
|
||
$defaultThreshold = gc_status()['threshold']; | ||
for ($i = 0; $i < $defaultThreshold+1; $i++) { | ||
new Cycle(); | ||
} | ||
|
||
$objs = []; | ||
for ($i = 0; $i < 100; $i++) { | ||
$obj = new stdClass; | ||
$objs[] = $obj; | ||
} | ||
|
||
$st = gc_status(); | ||
|
||
if ($st['runs'] > 10) { | ||
var_dump($st); | ||
} | ||
?> | ||
==DONE== | ||
--EXPECT-- | ||
==DONE== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--TEST-- | ||
GH-13670 002 | ||
--FILE-- | ||
<?php | ||
|
||
register_shutdown_function(function () { | ||
global $shutdown; | ||
$shutdown = true; | ||
}); | ||
|
||
class Cycle { | ||
public $self; | ||
public function __construct() { | ||
$this->self = $this; | ||
} | ||
} | ||
|
||
class Canary { | ||
public $self; | ||
public function __construct() { | ||
$this->self = $this; | ||
} | ||
public function __destruct() { | ||
global $shutdown; | ||
if (!$shutdown) { | ||
work(); | ||
} | ||
} | ||
} | ||
|
||
function work() { | ||
global $objs, $defaultThreshold; | ||
new Canary(); | ||
// Create some collectable garbage so the next run will not adjust | ||
// threshold | ||
for ($i = 0; $i < 100; $i++) { | ||
new Cycle(); | ||
} | ||
// Add potential garbage to buffer | ||
foreach (array_slice($objs, 0, $defaultThreshold) as $obj) { | ||
$o = $obj; | ||
} | ||
} | ||
|
||
$defaultThreshold = gc_status()['threshold']; | ||
$objs = []; | ||
for ($i = 0; $i < $defaultThreshold*2; $i++) { | ||
$obj = new stdClass; | ||
$objs[] = $obj; | ||
} | ||
|
||
work(); | ||
|
||
foreach ($objs as $obj) { | ||
$o = $obj; | ||
} | ||
|
||
$st = gc_status(); | ||
|
||
if ($st['runs'] > 10) { | ||
var_dump($st); | ||
} | ||
?> | ||
==DONE== | ||
--EXPECT-- | ||
==DONE== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--TEST-- | ||
GH-13670 003 | ||
--FILE-- | ||
<?php | ||
|
||
register_shutdown_function(function () { | ||
global $shutdown; | ||
$shutdown = true; | ||
}); | ||
|
||
class Cycle { | ||
public $self; | ||
public function __construct() { | ||
$this->self = $this; | ||
} | ||
} | ||
|
||
class Canary { | ||
public $self; | ||
public function __construct() { | ||
$this->self = $this; | ||
} | ||
public function __destruct() { | ||
global $shutdown; | ||
if (!$shutdown) { | ||
work(); | ||
} | ||
} | ||
} | ||
|
||
function work() { | ||
global $objs, $defaultThreshold; | ||
new Canary(); | ||
// Create some collectable garbage so the next run will not adjust | ||
// threshold | ||
for ($i = 0; $i < 100; $i++) { | ||
new Cycle(); | ||
} | ||
// Add potential garbage to buffer | ||
foreach (array_slice($objs, 0, $defaultThreshold) as $obj) { | ||
$o = $obj; | ||
} | ||
} | ||
|
||
$defaultThreshold = gc_status()['threshold']; | ||
$objs = []; | ||
for ($i = 0; $i < $defaultThreshold*2; $i++) { | ||
$obj = new stdClass; | ||
$objs[] = $obj; | ||
} | ||
|
||
work(); | ||
|
||
// Result of array_slice() is a tmpvar that will be checked by | ||
// zend_gc_check_root_tmpvars() | ||
foreach (array_slice($objs, -10) as $obj) { | ||
$o = $obj; | ||
} | ||
|
||
$st = gc_status(); | ||
|
||
if ($st['runs'] > 10) { | ||
var_dump($st); | ||
} | ||
?> | ||
==DONE== | ||
--EXPECT-- | ||
==DONE== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this lead to store after
gc_grow_root_buffer()
instead of immediate collection?I didn't understand, how this is related to the main part of the patch and why this might be a problem.
Please explain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes exactly
The main part of the patch increases gc_threshold when num_roots is still >= gc_threshold after collection, to prevent a collection from being triggered immediately. However, this adjustment happens after
zend_gc_check_root_tmpvars()
, so this function may still trigger an immediate collection. Moreover this happens recursively and it leads to a stack overflow in one of the test cases.By setting
gc_active=1
beforezend_gc_check_root_tmpvars()
, we prevent collections temporarily.An alternative would be to adjust the threshold before
zend_gc_check_root_tmpvars()
. This requires a few more changes because the threshold should not be adjusted when collection is triggered manually, for example.