I have some problem regarding memory issue using foreach on PHP. There is a process that using so much memory. The process is just take data from WebService and insert to database. But the data is big, almost thousand of data. It's really memory hungry, almost eat 2gigs of ram. Is there anyway to optimize my code ? I have tried unset() and = NULL but doesn't free up the memory. Here my code, i am using codeigniter :
$proxy = $this->proxy;
$token = $this->token;
$table = 'nilai';
$qk =$this->db->query("SELECT id_reg_pd from $databaseakm");
$tampan = $qk->result();
foreach($tampan as $keyz0 => $data0){
$id_reg_pd = $data0->id_reg_pd;
$filter3 = "p.id_reg_pd='$id_reg_pd'";
$order3= "";
$limit3 = 500;
$offset3 = 0;
$tampan3 = $proxy->GetRecordset($token, $table, $filter3, $order3, $limit3, $offset3);
$_datas = array_chunk($tampan3["result"], 50);
foreach($_datas as $key => $data)
{
$this->db->trans_start();
$this->db->query("UPDATE temp_tabel SET status_isi = 2 WHERE nama_tabel = '$databasenilai'");
$this->db->insert_batch($databasenilai, $data);
$this->db->trans_complete();
$tampan3 = NULL;
$data = NULL;
unset($tampan3);
unset($data);
}
$tampan3 = NULL;
$data = NULL;
unset($tampan3);
unset($data);
}
$tampan3 = NULL;
$data = NULL;
unset($tampan3);
unset($data);