Memory leak in StringSplit in Mathematica 7
- To: mathgroup at smc.vnet.net
- Subject: [mg97255] Memory leak in StringSplit in Mathematica 7
- From: Scott Morrison <scott.morrison at gmail.com>
- Date: Mon, 9 Mar 2009 01:06:19 -0500 (EST)
I'm on OSX 10.5, running Mathematica 7. There's apparently a memory leak in StringSplit -- 16 bytes get used up every call. See below for a "by hand" implementation of StringSplit, and two test runs showing the difference. The problem had caused big problems; a long run of one of my programs was mysteriously starting to page memory unnecessarily, and switching to the "by hand" implementation fixes the problem. In[135]:= StringSplitWithoutMemoryLeak[S_,V_]:=StringTake[S,#]&/ @Partition[{1}~Join~Flatten[#+{-1,1}&/@StringPosition[S,V]]~Join~{-1}, 2] In[145]:= StringSplitWithoutMemoryLeak["12341234","2"]==StringSplit ["12341234","2"] Out[145]= True In[137]:= randomString:=StringJoin@@RandomChoice[{"0,","1","2"},10] In[143]:= Do[Print["Memory in use: ",MemoryInUse[]];Print["Time for 100000 StringSplits: ",AbsoluteTiming[Do[StringSplit[randomString,"0"], {100000}]][[1]]],{4}] During evaluation of In[143]:= Memory in use: 130297096 During evaluation of In[143]:= Time for 100000 StringSplits: 1.312322 During evaluation of In[143]:= Memory in use: 131897496 During evaluation of In[143]:= Time for 100000 StringSplits: 1.345231 During evaluation of In[143]:= Memory in use: 133497496 During evaluation of In[143]:= Time for 100000 StringSplits: 1.314813 During evaluation of In[143]:= Memory in use: 135097496 During evaluation of In[143]:= Time for 100000 StringSplits: 1.403105 In[144]:= Do[Print["Memory in use: ",MemoryInUse[]];Print["Time for 100000 StringSplits: ",AbsoluteTiming[Do[StringSplitWithoutMemoryLeak [randomString,"0"],{100000}]][[1]]],{4}] During evaluation of In[144]:= Memory in use: 136701120 During evaluation of In[144]:= Time for 100000 StringSplits: 3.918605 During evaluation of In[144]:= Memory in use: 136701480 During evaluation of In[144]:= Time for 100000 StringSplits: 3.948706 During evaluation of In[144]:= Memory in use: 136701480 During evaluation of In[144]:= Time for 100000 StringSplits: 3.952957 During evaluation of In[144]:= Memory in use: 136701480 During evaluation of In[144]:= Time for 100000 StringSplits: 3.916070
- Follow-Ups:
- Re: Memory leak in StringSplit in Mathematica 7
- From: Curtis Osterhoudt <cfo@lanl.gov>
- Re: Memory leak in StringSplit in Mathematica 7
- From: Oyvind Tafjord <tafjord@wolfram.com>
- Re: Memory leak in StringSplit in Mathematica 7