In MikTex, the memory size can be increased permanently by this way:
1, For Windows, Open a command Window and enter:
initexmf --edit-config-file=latex For Ubuntu, try this: gedit .miktex/texmfs/config/miktex/config/latex.ini
In the file opened (it may be empty), add:
main_memory=12000000
Save the file and quite. Then in the command window, enter
initexmf --dump=latex
Then repeat the above steps for pdflatex. If the error persists, try increasing the main-memory value. For lualatex,the memory allocation is dynamic, hence the aforementioned steps are unnecessary, but if the compilation fails for lualatex, try for it as well