{"id":745,"date":"2014-09-28T18:57:48","date_gmt":"2014-09-28T18:57:48","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=745"},"modified":"2015-08-17T06:15:57","modified_gmt":"2015-08-17T06:15:57","slug":"php-memory_get_peak_usage-and-ini_setmemory_limit-1","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/php-memory_get_peak_usage-and-ini_setmemory_limit-1\/","title":{"rendered":"PHP memory_get_peak_usage and ini_set(&#8216;memory_limit&#8217;, &#8216;-1&#8217;)"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[745]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>My <a href=\"http:\/\/stackoverflow.com\/questions\/13495281\/php-memory-get-peak-usage-and-ini-setmemory-limit-1\">quesiton<\/a> was:<\/p>\n<p style=\"color: #000000;\">I recently ran into memory allocation problems, so I started experimenting with the\u00a0<code>ini_set('memory_limit', value);<\/code>\u00a0directive where I tried to enter values incrementaly. Now, searching through the web (and SO) I found out that I can put\u00a0<code>-1<\/code>\u00a0as the\u00a0<code>value<\/code>. So, I did and now the script runs fully to the end without breaking (before I used to get the memory allocation error).<\/p>\n<p style=\"color: #000000;\">What I don&#8217;t understand, however, is that given these two lines at the end of the script&#8217;s file:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\" style=\"color: #000000;\"><code><span class=\"pln\">$mem <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> memory_get_peak_usage<\/span><span class=\"pun\">(<\/span><span class=\"kwd\" style=\"color: #00008b;\">true<\/span><span class=\"pun\">);<\/span><span class=\"pln\">         \r\necho <\/span><span class=\"str\" style=\"color: #800000;\">\"Peak mem. usage: &lt;b&gt;\"<\/span><span class=\"pun\">.<\/span><span class=\"pln\"> round<\/span><span class=\"pun\">(<\/span><span class=\"pln\">$mem <\/span><span class=\"pun\">\/<\/span><span class=\"lit\" style=\"color: #800000;\">1024<\/span><span class=\"pun\">\/<\/span><span class=\"lit\" style=\"color: #800000;\">10124<\/span><span class=\"pun\">,<\/span><span class=\"lit\" style=\"color: #800000;\">2<\/span><span class=\"pun\">)<\/span><span class=\"pun\">.<\/span><span class=\"str\" style=\"color: #800000;\">\"&lt;\/b&gt; MB\"<\/span><span class=\"pun\">;<\/span><\/code><\/pre>\n<p style=\"color: #000000;\">produce around\u00a0<strong>10.8MB<\/strong>\u00a0and when I look into the\u00a0<code>\/var\/log\/messages<\/code>\u00a0I can see this line:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\" style=\"color: #000000;\"><code><span class=\"typ\" style=\"color: #2b91af;\">Nov<\/span><span class=\"lit\" style=\"color: #800000;\">21<\/span><span class=\"lit\" style=\"color: #800000;\">13<\/span><span class=\"pun\">:<\/span><span class=\"lit\" style=\"color: #800000;\">52<\/span><span class=\"pun\">:<\/span><span class=\"lit\" style=\"color: #800000;\">26<\/span><span class=\"pln\"> mail suhosin<\/span><span class=\"pun\">[<\/span><span class=\"lit\" style=\"color: #800000;\">1153<\/span><span class=\"pun\">]:<\/span><span class=\"pln\"> ALERT<\/span><span class=\"pun\">-<\/span><span class=\"pln\">SIMULATION <\/span><span class=\"pun\">-<\/span><span class=\"pln\"> script tried to increase  \r\nmemory_limit to <\/span><span class=\"lit\" style=\"color: #800000;\">4294967295<\/span><span class=\"pln\"> bytes which <\/span><span class=\"kwd\" style=\"color: #00008b;\">is<\/span><span class=\"pln\"> above the allowed value <\/span><span class=\"pun\">(<\/span><span class=\"pln\">attacker  \r\n<\/span><span class=\"str\" style=\"color: #800000;\">'xx.xxx.xxx.xxx'<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> file <\/span><span class=\"str\" style=\"color: #800000;\">'\/var\/www\/html\/file.php'<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> line <\/span><span class=\"lit\" style=\"color: #800000;\">5<\/span><span class=\"pun\">)<\/span><\/code><\/pre>\n<p style=\"color: #000000;\">which means the script tried to alocate\u00a0<strong>4096MB<\/strong>!<\/p>\n<p style=\"color: #000000;\">How can this be? And also, what interest me the most is why didn&#8217;t the script execution stop in this case? Is it because of the\u00a0<code>ini_set('memory_limit', '-1');<\/code>? I mean, I did read that putting\u00a0<code>-1<\/code>\u00a0as the\u00a0<code>value<\/code>is not recomended and I know where the problem lies in the script (reading too big amount of data at once in the memory), and I will go and fix it with sequential reading, but I&#8217;m just baffled about these data differences, so I would be gratefull if someone can shed some light on it.<\/p>\n<p style=\"color: #000000;\">The answer, by user\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/stackoverflow.com\/users\/1300892\/sverri-m-olsen\">Sverri M. Olsen<\/a>\u00a0was:<\/p>\n<blockquote><p>It is because the suhosin patch uses its own &#8220;hard&#8221; maximum memory limit,\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/www.hardened-php.net\/suhosin\/configuration.html#suhosin.memory_limit\" rel=\"nofollow\"><code>suhosin.memory_limit<\/code><\/a>.<\/p>\n<p>From the configuration reference:<\/p>\n<blockquote style=\"font-style: normal;\"><p>Suhosin [&#8230;] disallows setting the\u00a0<code>memory_limit<\/code>\u00a0to a value\u00a0<strong>greater than the one the script started with<\/strong>, when this option is left at 0.<\/p><\/blockquote>\n<p>In other words, if you change the\u00a0<code>memory_limit<\/code>\u00a0so that it is bigger than suhosin&#8217;s upper limit then it will simply assume that you are an &#8220;attacker&#8221; trying to do something suspicious<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-745","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=745"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/745\/revisions"}],"predecessor-version":[{"id":1710,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/745\/revisions\/1710"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}