Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is highly unlikely that any realistic use of split would "consume lots of memory". Your input would have to be huge (many, many megabytes) and your result split into <em>many millions</em> of parts for it to even be noticed.</p> <p>Here's some code that creates a random string of approximately 1.8 million characters and splits it into over 1 million Strings and outputs the memory used and time taken.</p> <p>As you can see, it ain't much: 61Mb consumed in just 350ms.</p> <pre><code>public static void main(String[] args) throws Exception { StringBuilder sb = new StringBuilder(); for (int i = 0; i &lt; 99999; i++) { sb.append(Math.random()); } long begin = System.currentTimeMillis(); String string = sb.toString(); sb = null; System.gc(); long startFreeMem = Runtime.getRuntime().freeMemory(); String[] strings = string.split("(?=[0-5])"); long endFreeMem = Runtime.getRuntime().freeMemory(); long execution = System.currentTimeMillis() - begin; System.out.println("input length = " + string.length() + "\nnumber of strings after split = " + strings.length + "\nmemory consumed due to split = " + (startFreeMem - endFreeMem) + "\nexecution time = " + execution + "ms"); } </code></pre> <p>Output (run on fairly typical windows box):</p> <pre><code>input length = 1827035 number of strings after split = 1072788 memory consumed due to split = 71740240 execution time = 351ms </code></pre> <p>Interestingly, <em>without</em> <code>System.gc()</code> the memory used was about 1/3:</p> <pre><code>memory consumed due to split = 29582328 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload