1

I've got a little problem that I need to solve. I've written a cycle that parses long strings into predefined lenght strings by substr and saves them in array. When I give him long strings, it breaks after exactly 1000 cycles and gives me this error: PHP: Fatal Error: Allowed Memory Size of 1073741824 Bytes Exhausted - 1 GB.
Already tried setting: memory_limit = -1

Info about input file:

  • first line is count of all strings in file
  • second line contains string lenght and lenght of word that i search
  • third line contains string
  • second and third point repeats untill end of file

    Quote from start of input file:

    1005
    828 9 ddaacddabaccacdbbdacdadddaadddccccdabcbccdbcbadcddadaacdadddcbbbaabbccaabbccbcabbbabbcbbbaaabbbbccdabdbcadbbbbcdccadbbacbacdcdaacdadcbbcbcacacbcbcccdcabddabcabaabdcdbbadbcddacacacbcdcbcccaddbdbababcabdcadccdacccbbacddbdcaabbbcbcbbdaaadccddbbbccbababaccbddccbadcadcadbccaddbbbcccddcbbdbbbdcccacbdcaaacaabbbcddacddaacbbaadccaaddcadccdcaaacdddccccdadaadacbacadaacddbbdbbbabacdcacddddcdcddaacbaaadbbccdacaabddbccbbbdadddadbbdbbdccbabbcbcdbbbddcaabaaadadabdbccdaddcbbddbaacddaddbddccdbcaadcccdcbbddacabccabcaaddabcddaadacccbaacdddbdadbaadaacaaacadcacccabaabcdabadbdacddcabcabaaadadbddcdadbdddacadccdbbddddaaabdaaadddcdcadcddadcdcbadadcaacabbabcaabcadcdbbccbabdcbccbacbdccaddcdddbddacabaccbdbdbdbcaaddcbdbacadbcdacbbdcdcdddcaddcddbcccbdcdabbcabaccdacbdbbbbaaddaabcccbacbbddcadaddabadcbadbacaacbcaaaacbbbbdbadaddbacdbccddaddcbccbccddaa
    993 4 dcacdabdcdbbccacabadccbbbbbcbddabdcaaddcdbdbddddbdcdbabdbcbcbaccdbddacbddabcaacbaaababacababbdabadaabcabcbdcbbdbcadcbdabbbdcaddacaadcaaabaddbcadcdcadcbadacddcdbcabaabacbdbdcbcabaaadcacccccabdcbacccdadcbcacbbdbbdadddccbaccdbaadccccbbdabbbcbcaaddacbcdbabbbbbbdddbaaaaccbadaaddadbbbadcbaddbacaaaaaabcccdbcdbbdacbccaaaaddbabbabbaccdabccdbdbbddcbccbccbbdbcababbcaacbcaaadbbdbdadbcbddccaacbadccddbabbbbbccadbacccdbbbdbcbccbbbaacbbdcdaabaadbcbdcdbdccbababcbbcdcdcacdbadbdadaabdbbcdccacdcdaacdabdcaacdbbabcacbbddacbbaadabacaaddcdabdbcdcadbcaabacdbddbdabbabbadaaadbdcadbbbbbdcacddbaccbdccaccbccaabdababcccbacddbbaddbdbadacacabccacdadbccdcacccdcbcdaadaabaccbbabddcdaabddbcbdbdbacbacbadccbddbbdadcbddacacdaacbabcadaacccabcbcbcbddbcddccdccbdccccbccaaaabcbaddbcccaccdabbccbabdccbacbaccdaccdabccbaaaabccadcbcaddacaaacdabcabdcdacabcbdabcccaccdcadcabcbcabdddddbdadaddcbbabdcabcdddbbadbbdaacabbaabaddbaaddcdabddaabadcbcdbadcbdcddbcbbcaabdaccdccaccddacabbccaabababccbcdacbdbdbcbdbaadabacdacbadbbbdcdddbacdadbbdc

If you have any idea what may help I would gladly hear you and try them.

<html>
<head>
</head>
<body>
    <?php
$lines = file('input.txt');
$pocet = $lines[0];

for ($radek = 1; $radek < $pocet*2; $radek = $radek+2) {
$vyskyt = array();
$slovnik = array();

$line = $lines[$radek];
$numbers = explode(" ", $line);

$lenghtString = $numbers[0];
$lenghtWord = $numbers[1];
$magic = $lines[$radek + 1];

for ($i = 0; $i < intval($lenghtString); $i++) {
    if ($i == intval($lenghtString) - intval($lenghtWord) + 1) {
        break;
    }    

    $neco = substr($magic, $i, intval($lenghtWord));
    $neco2 = array_search($neco, $slovnik, TRUE);

    if ($neco2 === FALSE) {
        array_push($slovnik, $neco);
        array_push($vyskyt, 1);
    } else {
        $vyskyt[$neco2]++;
    }
}

$file = 'output.txt';
$current = file_get_contents($file);

$current .= count($slovnik) . " " . max($vyskyt) . " " . 
count(array_keys($vyskyt, max($vyskyt), TRUE)) . "\n";
//echo "<br />";
file_put_contents($file, $current);
}
    ?>
</body>
</html>

Edit: I think I found the problem. It seems like this code is okay but last 5 strings are just too big for substr function to handle... they are about 1-2 milion charracters long and cause memory error. Any idea what to do? I guess that I'll need to use different function to parse those strings.

Input file

dkNykita
  • 11
  • 3
  • 2
    Possible duplicate of [Allowed memory size of 134217728 bytes exhausted](https://stackoverflow.com/questions/12264253/allowed-memory-size-of-134217728-bytes-exhausted) – Vishnu Bhadoriya Feb 06 '18 at 11:39
  • 1
    Solution tried - doesn't work – dkNykita Feb 06 '18 at 11:41
  • please edit and add code which you have applied for memory limit @dkNykita – Vishnu Bhadoriya Feb 06 '18 at 11:44
  • 1
    This code is very hard to parse. What is the content of `input.txt`? – Chris Lear Feb 06 '18 at 12:21
  • Add a line `if ($radek > 998) echo('breakpoint");` the set a breakpoint on the line `echo('breakpoint");`, run the code in your IDE and when it stops, start examining variable,. to try to find out what is wrong. Sorry, but I can't think of anything else. OR, look at @ RafaSashi answer to the [related question](https://stackoverflow.com/questions/12264253/allowed-memory-size-of-134217728-bytes-exhausted) and step through the whole thing, seeing how `memory_limit` changes each time through the main loop – Mawg says reinstate Monica Feb 06 '18 at 13:10
  • To parse files like these you will need to read them incrementally and devise an algorithm to run on limited zone. Forget about `file_get_contents`, go back to `fgets` and `fputs`. – ob-ivan Feb 06 '18 at 16:40

0 Answers0