# Memory leak in the system function: how to free memory without restarting the kernel?

$HistoryLength = 0;  Warming up: TextSentences[StringJoin@RandomChoice[Join[Alphabet["Russian"], {" ", "."}], 100]];  No memory leak: MemoryInUse[]  118502448 Do[ StringJoin@RandomChoice[Join[Alphabet["Russian"], {" ", "."}], 100], {i, 10000} ]  MemoryInUse[]  118502448 Memory leak in TextSentences: Do[ TextSentences[StringJoin@RandomChoice[Join[Alphabet["Russian"], {" ", "."}], 100]], {i, 10000} ] // AbsoluteTiming  {32.6916,Null} MemoryInUse[]  260521400 How to free memory without restarting the kernel? Possible workaround (but very slow - up to 5x): LaunchKernels[1]; MemoryInUse[]  263313480 Do[ ParallelEvaluate@TextSentences[StringJoin@RandomChoice[Join[Alphabet["Russian"], {" ", "."}], 100]], {i, 10000} ] // AbsoluteTiming  {161.063, Null} MemoryInUse[]  263313480 • Check the help files for Share Jul 12 '19 at 17:41 • @BillWatts Thank you for your comment! Share will not help. Here is the good answer. But I am looking for a workaround without parallel kernel. Jul 12 '19 at 18:58 ## 1 Answer Please report this to support. You can get rid of some of the memory leak by executing the following: cleanupTextSentences[] := Remove[ "NaturalLanguageProcessingTextCasesPackagePrivateinterpretations$@",
"NaturalLanguageProcessingTextCasesPackagePrivateprobabilities$$@", "NaturalLanguageProcessingTextPositionPackagePrivatepunctuationPositions$$@",
"NaturalLanguageProcessingTextPositionPackagePrivatetokenPositions$@", "NaturalLanguageProcessingTextPositionPackagePrivatewordPositions$@"
]


MemoryInUse[]

Do[
TextSentences[StringJoin@RandomChoice[Join[Alphabet["Russian"], {" ", "."}], 100]],
{i, 10000}
] // AbsoluteTiming

MemoryInUse[]

cleanupTextSentences[]
MemoryInUse[]


123158784

{26.6266, Null}

231403704

167386720

If you want to use parallel processing, why not use ParallelDo?

MemoryInUse[]

ParallelDo[
TextSentences[StringJoin@RandomChoice[Join[Alphabet["Russian"],{" ","."}],100]],
{i,10000}
] //AbsoluteTiming

MemoryInUse[]
`

182588776

{7.46125, Null}

182591464

• Thank you for your answer! Already reported. I will process my data sequentially. Do you know a workaround for ANY system function that may have a memory leak? Jul 16 '19 at 7:28