diff --git a/NEWS b/NEWS index ab565d58ae5bd..49d3d7bac0ae2 100644 --- a/NEWS +++ b/NEWS @@ -27,6 +27,7 @@ PHP NEWS . Finished AVIF support in getimagesize(). (Yannis Guyon) . Fixed bug GH-7847 (stripos with large haystack has bad performance). (ilutov) + . New function memory_reset_peak_usage(). (Patrick Allaert) - Zip: . add ZipArchive::clearError() method diff --git a/UPGRADING b/UPGRADING index efc6f36df0eef..b91229ec627ea 100644 --- a/UPGRADING +++ b/UPGRADING @@ -114,6 +114,10 @@ PHP 8.2 UPGRADE NOTES 6. New Functions ======================================== +- Standard: + . The peak memory usage can now be reset to the current usage thanks to + memory_reset_peak_usage(). + ======================================== 7. New Classes and Interfaces ======================================== diff --git a/Zend/tests/memory_reset_peak_usage.phpt b/Zend/tests/memory_reset_peak_usage.phpt new file mode 100644 index 0000000000000..0599e7c313c41 --- /dev/null +++ b/Zend/tests/memory_reset_peak_usage.phpt @@ -0,0 +1,23 @@ +--TEST-- +void memory_reset_peak_usage(); +--SKIPIF-- + +--INI-- +memory_limit=-1 +--FILE-- + $m0); +unset($array0, $array1); +memory_reset_peak_usage(); +var_dump(memory_get_peak_usage() < $m1); +?> +--EXPECT-- +bool(true) +bool(true) diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 9635068a6bb86..d8a7b07db77fc 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -2724,6 +2724,14 @@ ZEND_API size_t zend_memory_peak_usage(bool real_usage) return 0; } +ZEND_API void zend_memory_reset_peak_usage(void) +{ +#if ZEND_MM_STAT + AG(mm_heap)->real_peak = AG(mm_heap)->real_size; + AG(mm_heap)->peak = AG(mm_heap)->size; +#endif +} + ZEND_API void shutdown_memory_manager(bool silent, bool full_shutdown) { zend_mm_shutdown(AG(mm_heap), full_shutdown, silent); diff --git a/Zend/zend_alloc.h b/Zend/zend_alloc.h index 9603af8a27d3f..f517b1231d1d2 100644 --- a/Zend/zend_alloc.h +++ b/Zend/zend_alloc.h @@ -229,6 +229,7 @@ ZEND_API bool is_zend_ptr(const void *ptr); ZEND_API size_t zend_memory_usage(bool real_usage); ZEND_API size_t zend_memory_peak_usage(bool real_usage); +ZEND_API void zend_memory_reset_peak_usage(void); /* fast cache for HashTables */ #define ALLOC_HASHTABLE(ht) \ diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 850c3f7a3051a..10a6cfd7f2af0 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -2175,6 +2175,9 @@ ZEND_END_ARG_INFO() #define arginfo_memory_get_peak_usage arginfo_memory_get_usage +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_memory_reset_peak_usage, 0, 0, IS_VOID, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_version_compare, 0, 2, MAY_BE_LONG|MAY_BE_BOOL) ZEND_ARG_TYPE_INFO(0, version1, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, version2, IS_STRING, 0) @@ -2824,6 +2827,7 @@ ZEND_FUNCTION(serialize); ZEND_FUNCTION(unserialize); ZEND_FUNCTION(memory_get_usage); ZEND_FUNCTION(memory_get_peak_usage); +ZEND_FUNCTION(memory_reset_peak_usage); ZEND_FUNCTION(version_compare); #if defined(PHP_WIN32) ZEND_FUNCTION(sapi_windows_cp_set); @@ -3479,6 +3483,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(unserialize, arginfo_unserialize) ZEND_FE(memory_get_usage, arginfo_memory_get_usage) ZEND_FE(memory_get_peak_usage, arginfo_memory_get_peak_usage) + ZEND_FE(memory_reset_peak_usage, arginfo_memory_reset_peak_usage) ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE(version_compare, arginfo_version_compare) #if defined(PHP_WIN32) ZEND_FE(sapi_windows_cp_set, arginfo_sapi_windows_cp_set) diff --git a/ext/standard/var.c b/ext/standard/var.c index ef4b019fb6e76..09e6735034b62 100644 --- a/ext/standard/var.c +++ b/ext/standard/var.c @@ -1474,6 +1474,14 @@ PHP_FUNCTION(memory_get_peak_usage) { } /* }}} */ +/* {{{ Resets the peak PHP memory usage */ +PHP_FUNCTION(memory_reset_peak_usage) { + ZEND_PARSE_PARAMETERS_NONE(); + + zend_memory_reset_peak_usage(); +} +/* }}} */ + PHP_INI_BEGIN() STD_PHP_INI_ENTRY("unserialize_max_depth", "4096", PHP_INI_ALL, OnUpdateLong, unserialize_max_depth, php_basic_globals, basic_globals) PHP_INI_END()