Skip to content

Commit 6744009

Browse files
Added: [zend_]memory_reset_peak_usage() (#8151)
1 parent 51f750e commit 6744009

File tree

7 files changed

+50
-0
lines changed

7 files changed

+50
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ PHP NEWS
2727
. Finished AVIF support in getimagesize(). (Yannis Guyon)
2828
. Fixed bug GH-7847 (stripos with large haystack has bad performance).
2929
(ilutov)
30+
. New function memory_reset_peak_usage(). (Patrick Allaert)
3031

3132
- Zip:
3233
. add ZipArchive::clearError() method

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ PHP 8.2 UPGRADE NOTES
114114
6. New Functions
115115
========================================
116116

117+
- Standard:
118+
. The peak memory usage can now be reset to the current usage thanks to
119+
memory_reset_peak_usage().
120+
117121
========================================
118122
7. New Classes and Interfaces
119123
========================================
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
void memory_reset_peak_usage();
3+
--SKIPIF--
4+
<?php
5+
if (getenv("USE_ZEND_ALLOC") === "0") {
6+
die("skip Zend MM disabled");
7+
}
8+
?>
9+
--INI--
10+
memory_limit=-1
11+
--FILE--
12+
<?php
13+
$array0 = range(1, 1024 * 1024);
14+
$m0 = memory_get_peak_usage();
15+
$array1 = range(1, 1024 * 1024);
16+
var_dump(($m1 = memory_get_peak_usage()) > $m0);
17+
unset($array0, $array1);
18+
memory_reset_peak_usage();
19+
var_dump(memory_get_peak_usage() < $m1);
20+
?>
21+
--EXPECT--
22+
bool(true)
23+
bool(true)

Zend/zend_alloc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,6 +2724,14 @@ ZEND_API size_t zend_memory_peak_usage(bool real_usage)
27242724
return 0;
27252725
}
27262726

2727+
ZEND_API void zend_memory_reset_peak_usage(void)
2728+
{
2729+
#if ZEND_MM_STAT
2730+
AG(mm_heap)->real_peak = AG(mm_heap)->real_size;
2731+
AG(mm_heap)->peak = AG(mm_heap)->size;
2732+
#endif
2733+
}
2734+
27272735
ZEND_API void shutdown_memory_manager(bool silent, bool full_shutdown)
27282736
{
27292737
zend_mm_shutdown(AG(mm_heap), full_shutdown, silent);

Zend/zend_alloc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ ZEND_API bool is_zend_ptr(const void *ptr);
229229

230230
ZEND_API size_t zend_memory_usage(bool real_usage);
231231
ZEND_API size_t zend_memory_peak_usage(bool real_usage);
232+
ZEND_API void zend_memory_reset_peak_usage(void);
232233

233234
/* fast cache for HashTables */
234235
#define ALLOC_HASHTABLE(ht) \

ext/standard/basic_functions_arginfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,6 +2175,9 @@ ZEND_END_ARG_INFO()
21752175

21762176
#define arginfo_memory_get_peak_usage arginfo_memory_get_usage
21772177

2178+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_memory_reset_peak_usage, 0, 0, IS_VOID, 0)
2179+
ZEND_END_ARG_INFO()
2180+
21782181
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_version_compare, 0, 2, MAY_BE_LONG|MAY_BE_BOOL)
21792182
ZEND_ARG_TYPE_INFO(0, version1, IS_STRING, 0)
21802183
ZEND_ARG_TYPE_INFO(0, version2, IS_STRING, 0)
@@ -2824,6 +2827,7 @@ ZEND_FUNCTION(serialize);
28242827
ZEND_FUNCTION(unserialize);
28252828
ZEND_FUNCTION(memory_get_usage);
28262829
ZEND_FUNCTION(memory_get_peak_usage);
2830+
ZEND_FUNCTION(memory_reset_peak_usage);
28272831
ZEND_FUNCTION(version_compare);
28282832
#if defined(PHP_WIN32)
28292833
ZEND_FUNCTION(sapi_windows_cp_set);
@@ -3479,6 +3483,7 @@ static const zend_function_entry ext_functions[] = {
34793483
ZEND_FE(unserialize, arginfo_unserialize)
34803484
ZEND_FE(memory_get_usage, arginfo_memory_get_usage)
34813485
ZEND_FE(memory_get_peak_usage, arginfo_memory_get_peak_usage)
3486+
ZEND_FE(memory_reset_peak_usage, arginfo_memory_reset_peak_usage)
34823487
ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE(version_compare, arginfo_version_compare)
34833488
#if defined(PHP_WIN32)
34843489
ZEND_FE(sapi_windows_cp_set, arginfo_sapi_windows_cp_set)

ext/standard/var.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,14 @@ PHP_FUNCTION(memory_get_peak_usage) {
14781478
}
14791479
/* }}} */
14801480

1481+
/* {{{ Resets the peak PHP memory usage */
1482+
PHP_FUNCTION(memory_reset_peak_usage) {
1483+
ZEND_PARSE_PARAMETERS_NONE();
1484+
1485+
zend_memory_reset_peak_usage();
1486+
}
1487+
/* }}} */
1488+
14811489
PHP_INI_BEGIN()
14821490
STD_PHP_INI_ENTRY("unserialize_max_depth", "4096", PHP_INI_ALL, OnUpdateLong, unserialize_max_depth, php_basic_globals, basic_globals)
14831491
PHP_INI_END()

0 commit comments

Comments
 (0)