0

Im new in laravel

Always get this

Symfony\Component\ErrorHandler\Error\FatalError Allowed memory size of 2147483648 bytes exhausted (tried to allocate 2147483656 bytes) Error on this line

$arrayGejala = collect([
        substr($gejala1, 14, 1), 
        substr($gejala2, 14, 1), 
        substr($gejala3, 14, 1),
    ])->implode(',');

    $belief = DB::table('pengetahuan')->select(DB::raw("GROUP_CONCAT(penyakit.kode_penyakit)"), 'pengetahuan.bobot')
        ->from('pengetahuan')
        ->join('penyakit', 'pengetahuan.id_penyakit', '=', 'penyakit.id_penyakit')
        ->whereIn('pengetahuan.id_gejala', [(array)$arrayGejala[0],(array)$arrayGejala[2],(array)$arrayGejala[4]])
        ->groupBy('pengetahuan.id_gejala')
        ->get();
        
        $evidence = array();
        while ($row = $belief->first()) {
            $evidence[]=$row;
        }
1
  • 2
    while ($row = $belief->first()) will result in an infinite loop. If $belief->first() is truthy once it will be truthy forever (the function is "pure" i.e. it doesn't change the state of the collection). You might want to do something like $evidence = $belief->all() instead of a loop Commented Jan 8, 2022 at 7:15

3 Answers 3

1

Increase your maximum memory limit to 64MB in your php.ini file. Google search But could I ask why you are trying to allocate that much memory? What line of code does it fail at?

  1. in your PHP.ini file, change the line in PHP.ini If your line shows 32M try 64M: memory_limit = 64M ; Maximum amount of memory a script may consume (64MB)

  2. If you don't have access to PHP.ini try adding this to an .htaccess file: php_value memory_limit 64M

Sign up to request clarification or add additional context in comments.

3 Comments

i want to fetch data from $belief, and the code always say the error is on 'while'
Increasing the memory limit is not a good solution.
Yeah, maybe im just gonna delete that infinte loop and find another solution. Thanks for answering guys
0

This is because your Laravel version is not compatible with the Doctrine DBAL version you are using.

1. Check your version : composer why carbonphp/carbon-doctrine-types

2. Downgrade your version : composer require nesbot/carbon:^2.66(whatever your version is)

3. Remove your dependencies : composer remove carbonphp/carbon-doctrine-types

4. Install the compatible ones : composer require doctrine/dbal:^3.0(whatever your version is)

Comments

0

The script is trying to use more memory than allowed (2GB in your case):

  1. Inefficient Query Handling: You are fetching a large dataset with GROUP_CONCAT and storing it in memory. When you use $belief->first() repeatedly inside a loop, it loads data inefficiently, causing the memory usage to spike.

  2. Memory Usage in implode(): The implode() function can also consume a lot of memory if $arrayGejala has too many elements or if it's creating large strings.

Solution:

Use foreach instead of while: Avoid using $belief->first() inside the loop, as it's inefficient.

$evidence = [];
foreach ($belief as $row) {
    $evidence[] = $row;
}

Optimize implode(): Ensure $arrayGejala is not too large. If possible, reduce the data size.

Increase PHP memory limit (if necessary): Temporarily increase memory by adding this at the top:

ini_set('memory_limit', '4G');

Noted:

  • Large datasets are being processed and stored in memory without efficient pagination or limiting the results.

  • Repeated calls to first() in a loop fetch the same data multiple times instead of iterating directly.

These lead to excessive memory consumption.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.