Cara menggunakan memory_limit php.ini unlimited

Tinggalkan Balasan

Ketikkan komentar di sini...

Isikan data di bawah atau klik salah satu ikon untuk log in:

Email (wajib) (Alamat takkan pernah dipublikasikan)

Nama (wajib)

Situs web

You are commenting using your WordPress.com account. ( Logout /  Ubah )

You are commenting using your Twitter account. ( Logout /  Ubah )

You are commenting using your Facebook account. ( Logout /  Ubah )

Batal

Connecting to %s

Beri tahu saya komentar baru melalui email.

Beritahu saya pos-pos baru lewat surat elektronik.

Δ

Cara menggunakan memory_limit php.ini unlimited

Diterbitkan 19 Sep 2020 Diperbarui 20 Sep 2020

memory_limit merupakan salah satu variable penting dalam PHP yang membantu mengatur porsi atau space memori ketika eksekusi parsingan agar dapat membantu script untuk menggunakan memori lebih banyak atau lebih sedikit tergantung pada pengaturan. Beberapa website biasanya mengalami kendala pada memory_limit disebabkan oleh script pada website yang buruk yang dapat melemahkan server.

Untuk menghindari hal itu tentu kamu harus memperbarui dan memperbaiki script yang buruk ada pada website agar tidak memberatkan server, namun false positive bisa saja terjadi.

Hal termudah dan tercepat untuk mengatasi itu adalah mengatur memory_limit untuk menentukan space meori pada setiap esksekusi secara default.

Cara menggunakan memory_limit php.ini unlimited

Cara menggunakan memory_limit php.ini unlimited

ads by posciety

Menambah memory_limit di cPanel

Jika kamu menggunakan cPanel, kamu dapat mengatur memory_limit dengan sangat mudah. Ikuti langkah di bawah ini untuk mengetahui bagaimana cara menambah atau mengurangi max_execution_time di cPanel.

  1. Masuk ke cPanel seperti biasa
  2. Pilih MultiPHP INI Editor
  3. Configure PHP INI basic settings

    Pilih lokasi atau directory web yang ingin kamu atur memory_limit nya

  4. memory_limit

    Pada bagian memory_limit silahkan masukkan angka dan inisial (M = MB) seberapa banyak kamu mengizinkan script untuk memakan space memori pada waktu eksekusi, defaultnya 128M (MB)

    Cara menggunakan memory_limit php.ini unlimited

Semakin besar angka yang diterapkan pada memory_limit artinya semakin banyak space memory yang diizinkan untuk dimakan oleh script saat eksekusi yang ditentukan pada PHP.INI web directory bersangkutan.

Jika ingin mengaturnya sebagai unlimited, kamu dapat memasukkan -1. Klik disini untuk melihat video tutorial mengatur memory_limit di cPanel.

PHP memory_limit is per-script, just as a highway’s speed limit is per-vehicle. For example, although PHP’s memory limit may be set high to 1GB, that does not mean that scripts will pile up to use that 1GB. Let’s take a quick look at understanding PHP’s memory_limit setting.

PHP memory_limit is a per-script setting

PHP.net’s documentations puts it this way:

This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server...

Source: http://php.net/manual/en/ini.core.php#ini.memory-limit

Unlike say, MySQL’s key_buffer_size or innodb_buffer_pool settings, PHP memory_limit setting is not a storage space where multiple PHP scripts pool from or grow within. Rather it’s a per-script limit. PHP memory_limit is the maximum amount of server memory a single PHP script is allowed to consume. When blocked, the resulting error output looks something like this:

Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /path/to/php/script

or like this:

PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/php/script

So, for example, if two or more scripts are requested simultaneously, each is completely independent of the other. They do not share the memory_limit setting. Remember, PHP is not designed for and does not support multithreading. Thus, if five (5) PHP scripts are simultaneously using 100MB each, that would total 500MB of PHP memory usage, and a PHP memory limit of 128M wouldn’t be hit.

That said, for scripts that request other PHP scripts inline using require, include, or include_once, this limit is then inherited and shared by all included scripts that are dependent on the parent script.

“The include statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.” – W3schools.

“The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.” – php.net

Cara menggunakan memory_limit php.ini unlimited

PHP memory_limit is per-script, just as a highway’s speed limit is per-vehicle.

Now regarding the original example mentioned at the outset. A lower setting of 128M is always better because if PHP scripts are trying to use more than 128M, those scripts would now return memory limit exceeded errors. In the above issue, that was not the case, so regardless of 128M or 1G memory_limit setting, it only comes into play if there’s inefficient script(s).

Fortunately, the PHP memory_limit setting will block inefficient code, which then alerts you to optimize your code. Until fixed, you may want to temporarily increase PHP memory_limit to avoid your web application becoming unusable due to PHP out-of-memory errors.

If you don’t have available memory on your server, you’ll sometimes be faced with deciding whether to increase PHP memory_limit to meet the requirements of scripts or to optimize your code. It would be best if you always optimized as the preferred option when possible. Also, you can increase PHP’s memory limit for specific websites. One method would be to place a php.ini file in the site’s webroot. You can even set the limit for specific scriptname.php. For example using ini_set(‘memory_limit’,’256MB’).

How to increase PHP memory_limit

To increase the PHP memory limit setting, edit your PHP.ini file. Increase the default value (example: Maximum amount of memory a script may consume = 128MB) of the PHP memory limit line in php.ini.

memory_limit = 256M

Alternatively, you can edit your .htaccess file (Not recommended. See: Apache Performance: Disable .htaccess)

php_value memory_limit 256M

If you don’t have access to these files or lack the experience to make this change, you can contact your web host and ask them to increase your PHP memory limit.

Originally published: Sep 18th, 2017.
Last updated: August 19th 2021

Tags: memory, performance, php