Re: impl a collection (watch out for big strings)

From: NOBODY (antispam_at_0.0.0.0)
Date: 06/24/04


Date: Thu, 24 Jun 2004 15:44:13 GMT


"Tony Morris" <dibblego@optusnet.com.au> wrote in
news:cb89ri$tq2$1@news.btv.ibm.com:

> "Roedy Green" <look-on@mindprod.com.invalid> wrote in message
> news:ke8fd0l62jlt5lciftqvvvk5fjcvggtqhm@4ax.com...
>> On Tue, 22 Jun 2004 11:49:32 +1000, "Tony Morris"
>> <dibblego@optusnet.com.au> wrote or quoted :
>>
>> >No you aren't.
>> >Where are you receiving this false information?
>>
>> It is correct. Go look at the code for substring.
>> and similar code for StringBuffer.toString.
>
> The original statement was generalised - the topic of this forum is
> Java so it was assumed that the statement was related to Java, in
> which case, it is incorrect.
>
> Is there a particular VM implementation that behaves in that fashion?
> No VM implementation that I am looking at causes that statement to
> become true (even after being given a context).
>

I looked at sun's jdk 1.4.2.
Source code show it.
Real time debugging shows it.
Test yourself and you will see it.
Now this is with string buffer. But you may take the string out of the
string buffer and call substring(0, 1), and you will see that you cannot
create more strings, they are still holding the char[] underneath.

import java.util.LinkedList;

public class TestStringBufferToStringHeapCost {
        
        public static void main(String[] args) {
                LinkedList ll;
                int i = 0;
                
                try {
                        ll = new LinkedList();
                        i = 0;
                        while(true) {
                                StringBuffer sb = new StringBuffer(100);
                                sb.append('x');
                                ll.add(sb.toString());
                                i++;
                                if(i%1000 == 0) {
                                        //System.out.println(i);
                                        System.out.print('.');
                                        System.gc();
                                }
                        }
                } catch(OutOfMemoryError e) {
                        ll = null;
                        e.printStackTrace();
                } finally {
                        ll = null;
                        System.err.println(i);
                }

                
                try {
                        ll = new LinkedList();
                        i = 0;
                        while(true) {
                                StringBuffer sb = new StringBuffer(10000);
                                sb.append('x');
                                ll.add(sb.toString());
                                i++;
                                if(i%10 == 0) {
                                        //System.out.println(i);
                                        System.out.print('.');
                                        System.gc();
                                }
                        }
                } catch(OutOfMemoryError e) {
                        ll = null;
                        e.printStackTrace();
                } finally {
                        ll = null;
                        System.err.println(i);
                }
                
        }
}

/* because of:

    public String (StringBuffer buffer) {
        synchronized(buffer) {
            buffer.setShared();
            this.value = buffer.getValue(); //THIS!!!!!!!!!!!!!!
            this.offset = 0;
            this.count = buffer.length();
        }
    }
*/



Relevant Pages

  • Re: SuSE 9.1/2 and Java
    ... but fails with SuSE 9.1/2. ... > Java VM's convert a String Buffer to a String. ...
    (alt.os.linux.suse)
  • Re: How to search files for text string most efficiently?
    ... Is not the maximum size for a string buffer something like 0 to 2 ... >overlap the ends of two chunks separately. ... reading the second, back the byte pointer by at least the size of the ... substring to be found. ...
    (microsoft.public.dotnet.languages.vb)