Arhn - архитектура программирования

PHP пытается решить проблему с оперативной памятью, но webgrind не отображает миллисекунды

Я получаю следующую ошибку:

    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 64 bytes) in /var/www/AutoVerify3/php/xml2array.php on line 66
Call Stack
#   Time    Memory     Function          Location
1   0.0000  724976      {main}( )       ../index.php:0
2   1.4077  3277160     xml2array( )    ../index.php:57
3   1.9923  134175016   array_keys ( )  ../xml2array.php:66

Что делает мой скрипт, так это сохраняет большой XML (2 МБ+) в ассоциативном массиве. У меня есть настройка Xdebug и webgrind, чтобы определить, что именно потребляет всю мою оперативную память, но результаты от webgrind не ожидаются:

- Во-первых, полоса вверху (с разными цветами, показывающими распределение) крошечная - она ​​не занимает всю отображаемую сетку:

|(BAR)|    |    |    |    |    |    |

-Second webgrind не сообщает мне, сколько миллисекунд работал скрипт, поэтому вместо:

34 различных функции вызываются за 100 миллисекунд (1 выполняется, показано 34). Он говорит:

34 различных функции, вызываемые в миллисекундах (выполняется, показано 34). В-третьих, на диаграмме не отображаются какие-либо другие типы функций, кроме внутренних и включаемых. Это может быть связано с тем, что скрипт не может зайти так далеко.

Любая помощь по webgrind или проблеме с памятью очень ценится.

Редактировать Вот код из xml2array - это функция, которую я где-то позаимствовал некоторое время назад:

function xml2array($contents, $get_attributes=1, $priority = 'tag') {
if(!$contents) return array();

if(!function_exists('xml_parser_create')) {
    //print "'xml_parser_create()' function not found!";
    return array();
}

//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);

if(!$xml_values) return;//Hmm...

//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();

$current = &$xml_array; //Refference

//Go through the tags.
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array
foreach($xml_values as $data) {
    unset($attributes,$value);//Remove existing values, or there will be trouble

    //This command will extract these variables into the foreach scope
    // tag(string), type(string), level(int), attributes(array).
    extract($data);//We could use the array by itself, but this cooler.

    $result = array();
    $attributes_data = array();

    if(isset($value)) {
        if($priority == 'tag') $result = $value;
        else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
    }

    //Set the attributes too.
    if(isset($attributes) and $get_attributes) {
        foreach($attributes as $attr => $val) {
            if($priority == 'tag') $attributes_data[$attr] = $val;
            else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
        }
    }

    //See tag status and do the needed.
    if($type == "open") {//The starting of the tag '<tag>'
        $parent[$level-1] = &$current;
        if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
            $current[$tag] = $result;
            if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
            $repeated_tag_index[$tag.'_'.$level] = 1;

            $current = &$current[$tag];

        } else { //There was another element with the same tag name

            if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
                $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
                $repeated_tag_index[$tag.'_'.$level]++;
            } else {//This section will make the value an array if multiple tags with the same name appear together
                $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
                $repeated_tag_index[$tag.'_'.$level] = 2;

                if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
                    $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                    unset($current[$tag.'_attr']);
                }

            }
            $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
            $current = &$current[$tag][$last_item_index];
        }

    } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
        //See if the key is already taken.
        if(!isset($current[$tag])) { //New Key
            $current[$tag] = $result;
            $repeated_tag_index[$tag.'_'.$level] = 1;
            if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;

        } else { //If taken, put all things inside a list(array)
            if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...

                // ...push the new element into that array.
                $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;

                if($priority == 'tag' and $get_attributes and $attributes_data) {
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                }
                $repeated_tag_index[$tag.'_'.$level]++;

            } else { //If it is not an array...
                $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
                $repeated_tag_index[$tag.'_'.$level] = 1;
                if($priority == 'tag' and $get_attributes) {
                    if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well

                        $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                        unset($current[$tag.'_attr']);
                    }

                    if($attributes_data) {
                        $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                    }
                }
                $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
            }
        }

    } elseif($type == 'close') { //End of tag '</tag>'
        $current = &$parent[$level-1];
    }
}

return($xml_array);

}

29.03.2012

  • Вы используете SimpleXML? И если да, то какая версия PHP? 29.03.2012
  • @AlexHowansky - Нет, парсер XML. PHP v5.3.6 29.03.2012
  • @horatio - я добавил код, вот строка: if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag 29.03.2012
  • @ jreed121: jreed121: есть ли причина не увеличивать лимит памяти php, чтобы посмотреть, можно ли завершить? 29.03.2012
  • @wallyk - я думал об этом, но сначала хотел убедиться, что эта проблема вызвана чем-то другим. 16 МБ — это приличный объем, учитывая, что скрипт работает всего секунду или две, прежде чем выдает ошибку — после преобразования xml в массив необходимо выполнить много тяжелой работы. 29.03.2012

Ответы:


1

Совершенно очевидно, что здесь виноват array_keys. Если я правильно понял, вы запрашиваете массив данных с ключом $current. array_keys возвращает массив индексов, соответствующих этому критерию, и ваша информация показывает, что размер возвращаемого массива составляет 128 МБ: так что есть много элементов с одним и тем же ключом.

Это единственный вызов array_keys в коде, и он находится в строке, которую вы упомянули. Посмотрите, сможете ли вы найти способ заменить этот вызов: тот, который не копирует и не копирует информацию. Иногда можно использовать isset вместо in_array и т. д.

29.03.2012
Новые материалы

Коллекции публикаций по глубокому обучению
Последние пару месяцев я создавал коллекции последних академических публикаций по различным подполям глубокого обучения в моем блоге https://amundtveit.com - эта публикация дает обзор 25..

Представляем: Pepita
Фреймворк JavaScript с открытым исходным кодом Я знаю, что недостатка в фреймворках JavaScript нет. Но я просто не мог остановиться. Я хотел написать что-то сам, со своими собственными..

Советы по коду Laravel #2
1-) Найти // You can specify the columns you need // in when you use the find method on a model User::find(‘id’, [‘email’,’name’]); // You can increment or decrement // a field in..

Работа с временными рядами спутниковых изображений, часть 3 (аналитика данных)
Анализ временных рядов спутниковых изображений для данных наблюдений за большой Землей (arXiv) Автор: Рольф Симоэс , Жильберто Камара , Жильберто Кейрос , Фелипе Соуза , Педро Р. Андраде ,..

3 способа решить квадратное уравнение (3-й мой любимый) -
1. Методом факторизации — 2. Используя квадратичную формулу — 3. Заполнив квадрат — Давайте поймем это, решив это простое уравнение: Мы пытаемся сделать LHS,..

Создание VR-миров с A-Frame
Виртуальная реальность (и дополненная реальность) стали главными модными терминами в образовательных технологиях. С недорогими VR-гарнитурами, такими как Google Cardboard , и использованием..

Демистификация рекурсии
КОДЕКС Демистификация рекурсии Упрощенная концепция ошеломляющей О чем весь этот шум? Рекурсия, кажется, единственная тема, от которой у каждого начинающего студента-информатика..