[{"data":1,"prerenderedAt":1616},["ShallowReactive",2],{"doc-\u002Ferrors\u002Fmemoryerror-in-python-causes-and-fixes":3},{"id":4,"title":5,"body":6,"description":1609,"extension":1610,"meta":1611,"navigation":81,"path":1612,"seo":1613,"stem":1614,"__hash__":1615},"content\u002Ferrors\u002Fmemoryerror-in-python-causes-and-fixes.md","MemoryError in Python: Causes and Fixes",{"type":7,"value":8,"toc":1580},"minimark",[9,13,21,24,29,32,124,135,144,148,153,156,171,174,188,192,195,215,218,257,328,384,387,391,394,421,428,435,438,510,513,533,539,543,548,551,590,593,678,683,687,690,693,742,745,811,823,827,830,966,969,973,976,1033,1036,1040,1043,1046,1063,1067,1073,1077,1080,1094,1098,1101,1184,1187,1213,1217,1220,1303,1305,1339,1342,1346,1349,1377,1380,1418,1421,1425,1428,1431,1435,1438,1441,1452,1455,1459,1465,1489,1492,1503,1507,1511,1514,1518,1524,1528,1531,1535,1538,1542,1570,1576],[10,11,5],"h1",{"id":12},"memoryerror-in-python-causes-and-fixes",[14,15,16,20],"p",{},[17,18,19],"code",{},"MemoryError"," means Python tried to create or grow an object, but there was not enough memory available.",[14,22,23],{},"This usually happens when a program creates very large lists, strings, dictionaries, or reads too much data into memory at once. The fix is often not just “use more RAM.” In many cases, the better solution is to change how the program handles data.",[25,26,28],"h2",{"id":27},"quick-fix","Quick fix",[14,30,31],{},"A common fix is to avoid creating very large lists when you only need to loop through values.",[33,34,39],"pre",{"className":35,"code":36,"language":37,"meta":38,"style":38},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","numbers = range(10_000_000)\n# Better than: numbers = list(range(10_000_000))\n\nfor n in numbers:\n    if n > 5:\n        break\n","python","",[17,40,41,69,76,83,102,118],{"__ignoreMap":38},[42,43,46,50,54,58,62,66],"span",{"class":44,"line":45},"line",1,[42,47,49],{"class":48},"su5hD","numbers ",[42,51,53],{"class":52},"smGrS","=",[42,55,57],{"class":56},"sptTA"," range",[42,59,61],{"class":60},"sP7_E","(",[42,63,65],{"class":64},"srdBf","10_000_000",[42,67,68],{"class":60},")\n",[42,70,72],{"class":44,"line":71},2,[42,73,75],{"class":74},"sutJx","# Better than: numbers = list(range(10_000_000))\n",[42,77,79],{"class":44,"line":78},3,[42,80,82],{"emptyLinePlaceholder":81},true,"\n",[42,84,86,90,93,96,99],{"class":44,"line":85},4,[42,87,89],{"class":88},"sVHd0","for",[42,91,92],{"class":48}," n ",[42,94,95],{"class":88},"in",[42,97,98],{"class":48}," numbers",[42,100,101],{"class":60},":\n",[42,103,105,108,110,113,116],{"class":44,"line":104},5,[42,106,107],{"class":88},"    if",[42,109,92],{"class":48},[42,111,112],{"class":52},">",[42,114,115],{"class":64}," 5",[42,117,101],{"class":60},[42,119,121],{"class":44,"line":120},6,[42,122,123],{"class":88},"        break\n",[14,125,126,129,130,134],{},[17,127,128],{},"range(10_000_000)"," is memory-efficient because it does ",[131,132,133],"strong",{},"not"," store all ten million numbers in a list.",[14,136,137,138,143],{},"If you want to understand this idea better, see ",[139,140,142],"a",{"href":141},"\u002Flearn\u002Fgenerators-in-python-explained\u002F","generators in Python explained",".",[25,145,147],{"id":146},"what-memoryerror-means","What MemoryError means",[14,149,150,152],{},[17,151,19],{}," happens when Python cannot get enough memory for an object.",[14,154,155],{},"In simple terms:",[157,158,159,163,166],"ul",{},[160,161,162],"li",{},"Your code asked Python to store too much data",[160,164,165],{},"Python could not fit that data into available memory",[160,167,168,169],{},"The program stopped with a ",[17,170,19],{},[14,172,173],{},"This often appears when:",[157,175,176,179,182,185],{},[160,177,178],{},"Creating a huge list",[160,180,181],{},"Building a very large string",[160,183,184],{},"Loading a large file all at once",[160,186,187],{},"Growing a data structure in a loop for too long",[25,189,191],{"id":190},"common-situations-that-cause-it","Common situations that cause it",[14,193,194],{},"Some common causes are:",[157,196,197,203,206,209,212],{},[160,198,199,200],{},"Building a huge list with ",[17,201,202],{},"list(range(...))",[160,204,205],{},"Reading a very large file all at once",[160,207,208],{},"Creating many copies of large objects",[160,210,211],{},"Using nested lists or dictionaries that grow too much",[160,213,214],{},"Infinite loops that keep appending data",[14,216,217],{},"Here are some typical examples:",[33,219,221],{"className":35,"code":220,"language":37,"meta":38,"style":38},"# Huge list\nnumbers = list(range(10**8))\n",[17,222,223,228],{"__ignoreMap":38},[42,224,225],{"class":44,"line":45},[42,226,227],{"class":74},"# Huge list\n",[42,229,230,232,234,238,240,243,245,248,251,254],{"class":44,"line":71},[42,231,49],{"class":48},[42,233,53],{"class":52},[42,235,237],{"class":236},"sZMiF"," list",[42,239,61],{"class":60},[42,241,242],{"class":56},"range",[42,244,61],{"class":60},[42,246,247],{"class":64},"10",[42,249,250],{"class":52},"**",[42,252,253],{"class":64},"8",[42,255,256],{"class":60},"))\n",[33,258,260],{"className":35,"code":259,"language":37,"meta":38,"style":38},"# Reading the whole file into memory\nwith open(\"big_file.txt\", \"r\") as file:\n    data = file.read()\n",[17,261,262,267,310],{"__ignoreMap":38},[42,263,264],{"class":44,"line":45},[42,265,266],{"class":74},"# Reading the whole file into memory\n",[42,268,269,272,275,277,281,285,287,290,293,296,298,301,304,308],{"class":44,"line":71},[42,270,271],{"class":88},"with",[42,273,274],{"class":56}," open",[42,276,61],{"class":60},[42,278,280],{"class":279},"sjJ54","\"",[42,282,284],{"class":283},"s_sjI","big_file.txt",[42,286,280],{"class":279},[42,288,289],{"class":60},",",[42,291,292],{"class":279}," \"",[42,294,295],{"class":283},"r",[42,297,280],{"class":279},[42,299,300],{"class":60},")",[42,302,303],{"class":88}," as",[42,305,307],{"class":306},"sMMDD"," file",[42,309,101],{"class":60},[42,311,312,315,317,319,321,325],{"class":44,"line":78},[42,313,314],{"class":48},"    data ",[42,316,53],{"class":52},[42,318,307],{"class":306},[42,320,143],{"class":60},[42,322,324],{"class":323},"slqww","read",[42,326,327],{"class":60},"()\n",[33,329,331],{"className":35,"code":330,"language":37,"meta":38,"style":38},"# Infinite growth\nitems = []\n\nwhile True:\n    items.append(\"hello\")\n",[17,332,333,338,348,352,363],{"__ignoreMap":38},[42,334,335],{"class":44,"line":45},[42,336,337],{"class":74},"# Infinite growth\n",[42,339,340,343,345],{"class":44,"line":71},[42,341,342],{"class":48},"items ",[42,344,53],{"class":52},[42,346,347],{"class":60}," []\n",[42,349,350],{"class":44,"line":78},[42,351,82],{"emptyLinePlaceholder":81},[42,353,354,357,361],{"class":44,"line":85},[42,355,356],{"class":88},"while",[42,358,360],{"class":359},"s39Yj"," True",[42,362,101],{"class":60},[42,364,365,368,370,373,375,377,380,382],{"class":44,"line":104},[42,366,367],{"class":48},"    items",[42,369,143],{"class":60},[42,371,372],{"class":323},"append",[42,374,61],{"class":60},[42,376,280],{"class":279},[42,378,379],{"class":283},"hello",[42,381,280],{"class":279},[42,383,68],{"class":60},[14,385,386],{},"In each case, memory use keeps growing until Python can no longer continue.",[25,388,390],{"id":389},"example-that-causes-memoryerror","Example that causes MemoryError",[14,392,393],{},"A classic example is trying to create a very large list:",[33,395,397],{"className":35,"code":396,"language":37,"meta":38,"style":38},"numbers = list(range(10**10))\n",[17,398,399],{"__ignoreMap":38},[42,400,401,403,405,407,409,411,413,415,417,419],{"class":44,"line":45},[42,402,49],{"class":48},[42,404,53],{"class":52},[42,406,237],{"class":236},[42,408,61],{"class":60},[42,410,242],{"class":56},[42,412,61],{"class":60},[42,414,247],{"class":64},[42,416,250],{"class":52},[42,418,247],{"class":64},[42,420,256],{"class":60},[14,422,423,424,427],{},"This is a problem because Python tries to store ",[131,425,426],{},"every value"," in memory at the same time.",[14,429,430,431,434],{},"For a very large number like ",[17,432,433],{},"10**10",", that can require far more RAM than most computers have.",[14,436,437],{},"By comparison, this is much lighter:",[33,439,441],{"className":35,"code":440,"language":37,"meta":38,"style":38},"numbers = range(10**10)\nprint(numbers[0])\nprint(numbers[1])\nprint(numbers[2])\n",[17,442,443,461,480,495],{"__ignoreMap":38},[42,444,445,447,449,451,453,455,457,459],{"class":44,"line":45},[42,446,49],{"class":48},[42,448,53],{"class":52},[42,450,57],{"class":56},[42,452,61],{"class":60},[42,454,247],{"class":64},[42,456,250],{"class":52},[42,458,247],{"class":64},[42,460,68],{"class":60},[42,462,463,466,468,471,474,477],{"class":44,"line":71},[42,464,465],{"class":56},"print",[42,467,61],{"class":60},[42,469,470],{"class":323},"numbers",[42,472,473],{"class":60},"[",[42,475,476],{"class":64},"0",[42,478,479],{"class":60},"])\n",[42,481,482,484,486,488,490,493],{"class":44,"line":78},[42,483,465],{"class":56},[42,485,61],{"class":60},[42,487,470],{"class":323},[42,489,473],{"class":60},[42,491,492],{"class":64},"1",[42,494,479],{"class":60},[42,496,497,499,501,503,505,508],{"class":44,"line":85},[42,498,465],{"class":56},[42,500,61],{"class":60},[42,502,470],{"class":323},[42,504,473],{"class":60},[42,506,507],{"class":64},"2",[42,509,479],{"class":60},[14,511,512],{},"Expected output:",[33,514,516],{"className":35,"code":515,"language":37,"meta":38,"style":38},"0\n1\n2\n",[17,517,518,523,528],{"__ignoreMap":38},[42,519,520],{"class":44,"line":45},[42,521,522],{"class":64},"0\n",[42,524,525],{"class":44,"line":71},[42,526,527],{"class":64},"1\n",[42,529,530],{"class":44,"line":78},[42,531,532],{"class":64},"2\n",[14,534,535,538],{},[17,536,537],{},"range()"," does not build the full list in memory. It produces values as needed.",[25,540,542],{"id":541},"how-to-fix-it","How to fix it",[544,545,547],"h3",{"id":546},"use-iterators-or-generators-instead-of-large-lists","Use iterators or generators instead of large lists",[14,549,550],{},"If you only need to loop through values, avoid creating a full list.",[33,552,554],{"className":35,"code":553,"language":37,"meta":38,"style":38},"for number in range(10_000_000):\n    if number > 5:\n        break\n",[17,555,556,574,586],{"__ignoreMap":38},[42,557,558,560,563,565,567,569,571],{"class":44,"line":45},[42,559,89],{"class":88},[42,561,562],{"class":48}," number ",[42,564,95],{"class":88},[42,566,57],{"class":56},[42,568,61],{"class":60},[42,570,65],{"class":64},[42,572,573],{"class":60},"):\n",[42,575,576,578,580,582,584],{"class":44,"line":71},[42,577,107],{"class":88},[42,579,562],{"class":48},[42,581,112],{"class":52},[42,583,115],{"class":64},[42,585,101],{"class":60},[42,587,588],{"class":44,"line":78},[42,589,123],{"class":88},[14,591,592],{},"You can also use a generator expression:",[33,594,596],{"className":35,"code":595,"language":37,"meta":38,"style":38},"squares = (n * n for n in range(1_000_000))\n\nfor value in squares:\n    print(value)\n    if value > 100:\n        break\n",[17,597,598,631,635,649,661,674],{"__ignoreMap":38},[42,599,600,603,605,608,611,614,616,618,620,622,624,626,629],{"class":44,"line":45},[42,601,602],{"class":48},"squares ",[42,604,53],{"class":52},[42,606,607],{"class":60}," (",[42,609,610],{"class":48},"n ",[42,612,613],{"class":52},"*",[42,615,92],{"class":48},[42,617,89],{"class":88},[42,619,92],{"class":48},[42,621,95],{"class":88},[42,623,57],{"class":56},[42,625,61],{"class":60},[42,627,628],{"class":64},"1_000_000",[42,630,256],{"class":60},[42,632,633],{"class":44,"line":71},[42,634,82],{"emptyLinePlaceholder":81},[42,636,637,639,642,644,647],{"class":44,"line":78},[42,638,89],{"class":88},[42,640,641],{"class":48}," value ",[42,643,95],{"class":88},[42,645,646],{"class":48}," squares",[42,648,101],{"class":60},[42,650,651,654,656,659],{"class":44,"line":85},[42,652,653],{"class":56},"    print",[42,655,61],{"class":60},[42,657,658],{"class":323},"value",[42,660,68],{"class":60},[42,662,663,665,667,669,672],{"class":44,"line":104},[42,664,107],{"class":88},[42,666,641],{"class":48},[42,668,112],{"class":52},[42,670,671],{"class":64}," 100",[42,673,101],{"class":60},[42,675,676],{"class":44,"line":120},[42,677,123],{"class":88},[14,679,680,681,143],{},"This creates values one at a time instead of storing them all. For more on this idea, see ",[139,682,142],{"href":141},[544,684,686],{"id":685},"process-files-line-by-line-instead-of-reading-the-whole-file","Process files line by line instead of reading the whole file",[14,688,689],{},"This is a very common fix.",[14,691,692],{},"Instead of this:",[33,694,696],{"className":35,"code":695,"language":37,"meta":38,"style":38},"with open(\"big_file.txt\", \"r\") as file:\n    data = file.read()\n",[17,697,698,728],{"__ignoreMap":38},[42,699,700,702,704,706,708,710,712,714,716,718,720,722,724,726],{"class":44,"line":45},[42,701,271],{"class":88},[42,703,274],{"class":56},[42,705,61],{"class":60},[42,707,280],{"class":279},[42,709,284],{"class":283},[42,711,280],{"class":279},[42,713,289],{"class":60},[42,715,292],{"class":279},[42,717,295],{"class":283},[42,719,280],{"class":279},[42,721,300],{"class":60},[42,723,303],{"class":88},[42,725,307],{"class":306},[42,727,101],{"class":60},[42,729,730,732,734,736,738,740],{"class":44,"line":71},[42,731,314],{"class":48},[42,733,53],{"class":52},[42,735,307],{"class":306},[42,737,143],{"class":60},[42,739,324],{"class":323},[42,741,327],{"class":60},[14,743,744],{},"Use this:",[33,746,748],{"className":35,"code":747,"language":37,"meta":38,"style":38},"with open(\"big_file.txt\", \"r\") as file:\n    for line in file:\n        print(line.strip())\n",[17,749,750,780,794],{"__ignoreMap":38},[42,751,752,754,756,758,760,762,764,766,768,770,772,774,776,778],{"class":44,"line":45},[42,753,271],{"class":88},[42,755,274],{"class":56},[42,757,61],{"class":60},[42,759,280],{"class":279},[42,761,284],{"class":283},[42,763,280],{"class":279},[42,765,289],{"class":60},[42,767,292],{"class":279},[42,769,295],{"class":283},[42,771,280],{"class":279},[42,773,300],{"class":60},[42,775,303],{"class":88},[42,777,307],{"class":306},[42,779,101],{"class":60},[42,781,782,785,788,790,792],{"class":44,"line":71},[42,783,784],{"class":88},"    for",[42,786,787],{"class":48}," line ",[42,789,95],{"class":88},[42,791,307],{"class":306},[42,793,101],{"class":60},[42,795,796,799,801,803,805,808],{"class":44,"line":78},[42,797,798],{"class":56},"        print",[42,800,61],{"class":60},[42,802,44],{"class":323},[42,804,143],{"class":60},[42,806,807],{"class":323},"strip",[42,809,810],{"class":60},"())\n",[14,812,813,814,818,819,143],{},"This reads one line at a time, which uses much less memory. See ",[139,815,817],{"href":816},"\u002Fhow-to\u002Fhow-to-read-a-file-line-by-line-in-python\u002F","how to read a file line by line in Python"," and ",[139,820,822],{"href":821},"\u002Flearn\u002Fpython-file-handling-basics-read-and-write\u002F","Python file handling basics: read and write",[544,824,826],{"id":825},"break-work-into-smaller-chunks","Break work into smaller chunks",[14,828,829],{},"If your input is large, process part of it, then continue.",[33,831,833],{"className":35,"code":832,"language":37,"meta":38,"style":38},"numbers = range(1_000_000)\nchunk_size = 100_000\n\nfor start in range(0, 1_000_000, chunk_size):\n    end = start + chunk_size\n    chunk = range(start, end)\n    print(f\"Processing {start} to {end - 1}\")\n",[17,834,835,849,859,863,890,905,926],{"__ignoreMap":38},[42,836,837,839,841,843,845,847],{"class":44,"line":45},[42,838,49],{"class":48},[42,840,53],{"class":52},[42,842,57],{"class":56},[42,844,61],{"class":60},[42,846,628],{"class":64},[42,848,68],{"class":60},[42,850,851,854,856],{"class":44,"line":71},[42,852,853],{"class":48},"chunk_size ",[42,855,53],{"class":52},[42,857,858],{"class":64}," 100_000\n",[42,860,861],{"class":44,"line":78},[42,862,82],{"emptyLinePlaceholder":81},[42,864,865,867,870,872,874,876,878,880,883,885,888],{"class":44,"line":85},[42,866,89],{"class":88},[42,868,869],{"class":48}," start ",[42,871,95],{"class":88},[42,873,57],{"class":56},[42,875,61],{"class":60},[42,877,476],{"class":64},[42,879,289],{"class":60},[42,881,882],{"class":64}," 1_000_000",[42,884,289],{"class":60},[42,886,887],{"class":323}," chunk_size",[42,889,573],{"class":60},[42,891,892,895,897,899,902],{"class":44,"line":104},[42,893,894],{"class":48},"    end ",[42,896,53],{"class":52},[42,898,869],{"class":48},[42,900,901],{"class":52},"+",[42,903,904],{"class":48}," chunk_size\n",[42,906,907,910,912,914,916,919,921,924],{"class":44,"line":120},[42,908,909],{"class":48},"    chunk ",[42,911,53],{"class":52},[42,913,57],{"class":56},[42,915,61],{"class":60},[42,917,918],{"class":323},"start",[42,920,289],{"class":60},[42,922,923],{"class":323}," end",[42,925,68],{"class":60},[42,927,929,931,933,937,940,943,945,948,951,953,956,959,962,964],{"class":44,"line":928},7,[42,930,653],{"class":56},[42,932,61],{"class":60},[42,934,936],{"class":935},"sbsja","f",[42,938,939],{"class":283},"\"Processing ",[42,941,942],{"class":64},"{",[42,944,918],{"class":323},[42,946,947],{"class":64},"}",[42,949,950],{"class":283}," to ",[42,952,942],{"class":64},[42,954,955],{"class":323},"end ",[42,957,958],{"class":52},"-",[42,960,961],{"class":64}," 1}",[42,963,280],{"class":283},[42,965,68],{"class":60},[14,967,968],{},"This approach is useful when working with large datasets.",[544,970,972],{"id":971},"remove-data-you-no-longer-need","Remove data you no longer need",[14,974,975],{},"If a large object is finished, do not keep it around.",[33,977,979],{"className":35,"code":978,"language":37,"meta":38,"style":38},"data = list(range(1_000_000))\nprint(len(data))\n\n# Finished using data\ndel data\n",[17,980,981,1000,1016,1020,1025],{"__ignoreMap":38},[42,982,983,986,988,990,992,994,996,998],{"class":44,"line":45},[42,984,985],{"class":48},"data ",[42,987,53],{"class":52},[42,989,237],{"class":236},[42,991,61],{"class":60},[42,993,242],{"class":56},[42,995,61],{"class":60},[42,997,628],{"class":64},[42,999,256],{"class":60},[42,1001,1002,1004,1006,1009,1011,1014],{"class":44,"line":71},[42,1003,465],{"class":56},[42,1005,61],{"class":60},[42,1007,1008],{"class":56},"len",[42,1010,61],{"class":60},[42,1012,1013],{"class":323},"data",[42,1015,256],{"class":60},[42,1017,1018],{"class":44,"line":78},[42,1019,82],{"emptyLinePlaceholder":81},[42,1021,1022],{"class":44,"line":85},[42,1023,1024],{"class":74},"# Finished using data\n",[42,1026,1027,1030],{"class":44,"line":104},[42,1028,1029],{"class":88},"del",[42,1031,1032],{"class":48}," data\n",[14,1034,1035],{},"Deleting variables can help in some cases, especially when large objects are no longer needed.",[544,1037,1039],{"id":1038},"use-more-memory-efficient-data-structures-when-possible","Use more memory-efficient data structures when possible",[14,1041,1042],{},"Sometimes the problem is not just the amount of data, but how you store it.",[14,1044,1045],{},"For example:",[157,1047,1048,1057,1060],{},[160,1049,1050,1051,1053,1054,1056],{},"Use ",[17,1052,537],{}," instead of ",[17,1055,202],{}," when possible",[160,1058,1059],{},"Avoid unnecessary copies",[160,1061,1062],{},"Avoid building large nested structures unless you really need them",[25,1064,1066],{"id":1065},"debugging-steps","Debugging steps",[14,1068,1069,1070,1072],{},"When you get a ",[17,1071,19],{},", try these steps.",[544,1074,1076],{"id":1075},"_1-find-the-line-where-large-objects-are-created","1. Find the line where large objects are created",[14,1078,1079],{},"Look for lines that:",[157,1081,1082,1085,1088,1091],{},[160,1083,1084],{},"Build large lists",[160,1086,1087],{},"Read full files",[160,1089,1090],{},"Copy data",[160,1092,1093],{},"Append in loops",[544,1095,1097],{"id":1096},"_2-print-sizes-or-lengths-as-data-grows","2. Print sizes or lengths as data grows",[14,1099,1100],{},"These simple checks can help:",[33,1102,1104],{"className":35,"code":1103,"language":37,"meta":38,"style":38},"print(len(my_list))\nprint(type(data))\nprint(my_list[:5])\n\nimport sys\nprint(sys.getsizeof(my_list))\n",[17,1105,1106,1121,1136,1152,1156,1164],{"__ignoreMap":38},[42,1107,1108,1110,1112,1114,1116,1119],{"class":44,"line":45},[42,1109,465],{"class":56},[42,1111,61],{"class":60},[42,1113,1008],{"class":56},[42,1115,61],{"class":60},[42,1117,1118],{"class":323},"my_list",[42,1120,256],{"class":60},[42,1122,1123,1125,1127,1130,1132,1134],{"class":44,"line":71},[42,1124,465],{"class":56},[42,1126,61],{"class":60},[42,1128,1129],{"class":236},"type",[42,1131,61],{"class":60},[42,1133,1013],{"class":323},[42,1135,256],{"class":60},[42,1137,1138,1140,1142,1144,1147,1150],{"class":44,"line":78},[42,1139,465],{"class":56},[42,1141,61],{"class":60},[42,1143,1118],{"class":323},[42,1145,1146],{"class":60},"[:",[42,1148,1149],{"class":64},"5",[42,1151,479],{"class":60},[42,1153,1154],{"class":44,"line":85},[42,1155,82],{"emptyLinePlaceholder":81},[42,1157,1158,1161],{"class":44,"line":104},[42,1159,1160],{"class":88},"import",[42,1162,1163],{"class":48}," sys\n",[42,1165,1166,1168,1170,1173,1175,1178,1180,1182],{"class":44,"line":120},[42,1167,465],{"class":56},[42,1169,61],{"class":60},[42,1171,1172],{"class":323},"sys",[42,1174,143],{"class":60},[42,1176,1177],{"class":323},"getsizeof",[42,1179,61],{"class":60},[42,1181,1118],{"class":323},[42,1183,256],{"class":60},[14,1185,1186],{},"What these do:",[157,1188,1189,1195,1201,1207],{},[160,1190,1191,1194],{},[17,1192,1193],{},"len(my_list)"," shows how many items are in the list",[160,1196,1197,1200],{},[17,1198,1199],{},"type(data)"," confirms what kind of object you are using",[160,1202,1203,1206],{},[17,1204,1205],{},"my_list[:5]"," shows the first few items without printing everything",[160,1208,1209,1212],{},[17,1210,1211],{},"sys.getsizeof(my_list)"," shows the size of the container object in bytes",[544,1214,1216],{"id":1215},"_3-check-for-loops-that-append-forever","3. Check for loops that append forever",[14,1218,1219],{},"A loop may be adding data without stopping.",[33,1221,1223],{"className":35,"code":1222,"language":37,"meta":38,"style":38},"items = []\ncount = 0\n\nwhile count \u003C 5:\n    items.append(count)\n    count += 1\n\nprint(items)\n",[17,1224,1225,1233,1243,1247,1261,1276,1287,1291],{"__ignoreMap":38},[42,1226,1227,1229,1231],{"class":44,"line":45},[42,1228,342],{"class":48},[42,1230,53],{"class":52},[42,1232,347],{"class":60},[42,1234,1235,1238,1240],{"class":44,"line":71},[42,1236,1237],{"class":48},"count ",[42,1239,53],{"class":52},[42,1241,1242],{"class":64}," 0\n",[42,1244,1245],{"class":44,"line":78},[42,1246,82],{"emptyLinePlaceholder":81},[42,1248,1249,1251,1254,1257,1259],{"class":44,"line":85},[42,1250,356],{"class":88},[42,1252,1253],{"class":48}," count ",[42,1255,1256],{"class":52},"\u003C",[42,1258,115],{"class":64},[42,1260,101],{"class":60},[42,1262,1263,1265,1267,1269,1271,1274],{"class":44,"line":104},[42,1264,367],{"class":48},[42,1266,143],{"class":60},[42,1268,372],{"class":323},[42,1270,61],{"class":60},[42,1272,1273],{"class":323},"count",[42,1275,68],{"class":60},[42,1277,1278,1281,1284],{"class":44,"line":120},[42,1279,1280],{"class":48},"    count ",[42,1282,1283],{"class":52},"+=",[42,1285,1286],{"class":64}," 1\n",[42,1288,1289],{"class":44,"line":928},[42,1290,82],{"emptyLinePlaceholder":81},[42,1292,1294,1296,1298,1301],{"class":44,"line":1293},8,[42,1295,465],{"class":56},[42,1297,61],{"class":60},[42,1299,1300],{"class":323},"items",[42,1302,68],{"class":60},[14,1304,512],{},[33,1306,1308],{"className":35,"code":1307,"language":37,"meta":38,"style":38},"[0, 1, 2, 3, 4]\n",[17,1309,1310],{"__ignoreMap":38},[42,1311,1312,1314,1316,1318,1321,1323,1326,1328,1331,1333,1336],{"class":44,"line":45},[42,1313,473],{"class":60},[42,1315,476],{"class":64},[42,1317,289],{"class":60},[42,1319,1320],{"class":64}," 1",[42,1322,289],{"class":60},[42,1324,1325],{"class":64}," 2",[42,1327,289],{"class":60},[42,1329,1330],{"class":64}," 3",[42,1332,289],{"class":60},[42,1334,1335],{"class":64}," 4",[42,1337,1338],{"class":60},"]\n",[14,1340,1341],{},"If you forget to update the loop condition, the list may keep growing.",[544,1343,1345],{"id":1344},"_4-test-with-a-much-smaller-input-first","4. Test with a much smaller input first",[14,1347,1348],{},"If this fails:",[33,1350,1352],{"className":35,"code":1351,"language":37,"meta":38,"style":38},"numbers = list(range(10**9))\n",[17,1353,1354],{"__ignoreMap":38},[42,1355,1356,1358,1360,1362,1364,1366,1368,1370,1372,1375],{"class":44,"line":45},[42,1357,49],{"class":48},[42,1359,53],{"class":52},[42,1361,237],{"class":236},[42,1363,61],{"class":60},[42,1365,242],{"class":56},[42,1367,61],{"class":60},[42,1369,247],{"class":64},[42,1371,250],{"class":52},[42,1373,1374],{"class":64},"9",[42,1376,256],{"class":60},[14,1378,1379],{},"Try a much smaller version:",[33,1381,1383],{"className":35,"code":1382,"language":37,"meta":38,"style":38},"numbers = list(range(1000))\nprint(len(numbers))\n",[17,1384,1385,1404],{"__ignoreMap":38},[42,1386,1387,1389,1391,1393,1395,1397,1399,1402],{"class":44,"line":45},[42,1388,49],{"class":48},[42,1390,53],{"class":52},[42,1392,237],{"class":236},[42,1394,61],{"class":60},[42,1396,242],{"class":56},[42,1398,61],{"class":60},[42,1400,1401],{"class":64},"1000",[42,1403,256],{"class":60},[42,1405,1406,1408,1410,1412,1414,1416],{"class":44,"line":71},[42,1407,465],{"class":56},[42,1409,61],{"class":60},[42,1411,1008],{"class":56},[42,1413,61],{"class":60},[42,1415,470],{"class":323},[42,1417,256],{"class":60},[14,1419,1420],{},"If the small version works, the problem is likely memory usage, not syntax.",[544,1422,1424],{"id":1423},"_5-replace-eager-loading-with-streaming-or-chunking","5. Replace eager loading with streaming or chunking",[14,1426,1427],{},"“Eager loading” means loading everything at once. A better approach is often to process data as it comes in.",[14,1429,1430],{},"This is especially useful when working with files, CSV data, or API results.",[25,1432,1434],{"id":1433},"when-the-problem-is-system-memory","When the problem is system memory",[14,1436,1437],{},"Sometimes the program is simply asking for more memory than the machine can provide.",[14,1439,1440],{},"A few things to know:",[157,1442,1443,1446,1449],{},[160,1444,1445],{},"Closing other programs may help",[160,1447,1448],{},"Very large datasets may require a different algorithm, not just more RAM",[160,1450,1451],{},"A 32-bit Python installation can hit memory limits sooner than 64-bit Python",[14,1453,1454],{},"So even if your code is correct, it may still fail if the approach needs too much memory.",[25,1456,1458],{"id":1457},"common-causes","Common causes",[14,1460,1461,1462,1464],{},"Here are the most common reasons beginners see ",[17,1463,19],{},":",[157,1466,1467,1473,1480,1483,1486],{},[160,1468,1469,1470,1472],{},"Creating a huge list instead of using ",[17,1471,537],{}," or a generator",[160,1474,1475,1476,1479],{},"Using ",[17,1477,1478],{},"file.read()"," on a very large file",[160,1481,1482],{},"Keeping unnecessary copies of data",[160,1484,1485],{},"Appending inside an infinite or very long loop",[160,1487,1488],{},"Loading a large JSON or CSV file fully into memory",[14,1490,1491],{},"A good habit is to ask:",[157,1493,1494,1497,1500],{},[160,1495,1496],{},"Do I need all this data at once?",[160,1498,1499],{},"Can I process one item at a time?",[160,1501,1502],{},"Can I stop storing old data?",[25,1504,1506],{"id":1505},"faq","FAQ",[544,1508,1510],{"id":1509},"is-memoryerror-the-same-as-my-computer-running-out-of-memory","Is MemoryError the same as my computer running out of memory?",[14,1512,1513],{},"Usually yes, or Python reached the memory limit available to the process.",[544,1515,1517],{"id":1516},"can-i-fix-memoryerror-by-using-range-instead-of-listrange","Can I fix MemoryError by using range() instead of list(range())?",[14,1519,1520,1521,1523],{},"Often yes, because ",[17,1522,537],{}," does not store all numbers in memory at once.",[544,1525,1527],{"id":1526},"why-does-reading-a-file-cause-memoryerror","Why does reading a file cause MemoryError?",[14,1529,1530],{},"If you read the whole file at once, Python may try to store too much data in RAM.",[544,1532,1534],{"id":1533},"will-deleting-variables-help","Will deleting variables help?",[14,1536,1537],{},"Sometimes. It can help if large objects are no longer needed, but better program design is usually the real fix.",[25,1539,1541],{"id":1540},"see-also","See also",[157,1543,1544,1549,1554,1558,1564],{},[160,1545,1546],{},[139,1547,1548],{"href":141},"Generators in Python explained",[160,1550,1551],{},[139,1552,1553],{"href":816},"How to read a file line by line in Python",[160,1555,1556],{},[139,1557,822],{"href":821},[160,1559,1560],{},[139,1561,1563],{"href":1562},"\u002Ferrors\u002Foverflowerror-numerical-result-out-of-range-fix\u002F","OverflowError: numerical result out of range",[160,1565,1566],{},[139,1567,1569],{"href":1568},"\u002Ferrors\u002Fruntimeerror-in-python-causes-and-fixes\u002F","RuntimeError in Python: causes and fixes",[14,1571,1572,1573,1575],{},"Learning to fix ",[17,1574,19],{}," is a good step toward writing more memory-efficient Python code.",[1577,1578,1579],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}",{"title":38,"searchDepth":71,"depth":71,"links":1581},[1582,1583,1584,1585,1586,1593,1600,1601,1602,1608],{"id":27,"depth":71,"text":28},{"id":146,"depth":71,"text":147},{"id":190,"depth":71,"text":191},{"id":389,"depth":71,"text":390},{"id":541,"depth":71,"text":542,"children":1587},[1588,1589,1590,1591,1592],{"id":546,"depth":78,"text":547},{"id":685,"depth":78,"text":686},{"id":825,"depth":78,"text":826},{"id":971,"depth":78,"text":972},{"id":1038,"depth":78,"text":1039},{"id":1065,"depth":71,"text":1066,"children":1594},[1595,1596,1597,1598,1599],{"id":1075,"depth":78,"text":1076},{"id":1096,"depth":78,"text":1097},{"id":1215,"depth":78,"text":1216},{"id":1344,"depth":78,"text":1345},{"id":1423,"depth":78,"text":1424},{"id":1433,"depth":71,"text":1434},{"id":1457,"depth":71,"text":1458},{"id":1505,"depth":71,"text":1506,"children":1603},[1604,1605,1606,1607],{"id":1509,"depth":78,"text":1510},{"id":1516,"depth":78,"text":1517},{"id":1526,"depth":78,"text":1527},{"id":1533,"depth":78,"text":1534},{"id":1540,"depth":71,"text":1541},"Master memoryerror in python causes and fixes in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Fmemoryerror-in-python-causes-and-fixes",{"title":5,"description":1609},"errors\u002Fmemoryerror-in-python-causes-and-fixes","7SkL0-JAUnj0IyhSudFZ9ydNZAmkPoeHut9zueIXTTc",1777585480659]