From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7bcba1db9ed24fa7 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-07-07 20:46:42 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!wn1feed!worldnet.att.net!135.173.83.71!wnfilter1!worldnet-localpost!bgtnsc04-news.ops.worldnet.att.net.POSTED!not-for-mail Message-ID: <3B47D830.830E4E31@worldnet.att.net> From: James Rogers X-Mailer: Mozilla 4.76 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: is ada dead? References: <3B460DA9.C2965042@ix.netcom.com> <9ff447f2.0107061757.34ca0723@posting.google.com> <9i6lak$bqi$1@geraldo.cc.utexas.edu> <3B475916.E4548A5D@worldnet.att.net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Sun, 08 Jul 2001 03:46:42 GMT NNTP-Posting-Host: 12.86.34.45 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 994564002 12.86.34.45 (Sun, 08 Jul 2001 03:46:42 GMT) NNTP-Posting-Date: Sun, 08 Jul 2001 03:46:42 GMT Organization: AT&T Worldnet Xref: archiver1.google.com comp.lang.ada:9627 Date: 2001-07-08T03:46:42+00:00 List-Id: "Stephen J. Bevan" wrote: > > James Rogers writes: > > In other words, if > > you want to loop through all the characters in an existing 1024 > > character String, replacing each one, you will require the creation > > and garbage collection of 1024 1024 character strings. Since Java > > characters are all 16 bits, this means that you need to chew up > > over 2 Megabytes of data to edit a 1024 character String. > > You could do it like that. However, it would be more efficient to > turn your String into a StringBuffer, which supports in-place updates. > Make all the changes you want to the StringBuffer and then turn it > back into a String. This way you'd only chew up approximately 3K. No, this is approximately 6k. Remember that every Java character requires 2 bytes. Yes. The Stringbuffer class is much more efficient than the String class for editing purposes. It is still terribly inefficient compared to, for instance, Ada strings. Remember the extra processing overhead needed to use a StringBuffer also. Consider the StringBuffer a linked list of characters. I say this because the StringBuffer is resizable, while the String class, and Java arrays are not. This means that the StringBuffer cannot be implemented using an array. First you allocate the space necessary to implement a String containing 1024 characters. The memory used is slightly in excess of 2k bytes. Next you must allocate 1024 characters and associate them with the StringBuffer object. This is not explicit to the programmer, because it is done by the methods in the StringBuffer class. Finally you must allocate another String containing copies of each of those StringBuffer characters, another 2k bytes. If the original String object is now unreferenced you must garbage collect roughly 2k bytes. You must also garbage collect the StringBuffer object and the 1024 elements of two byte characters. The catch to this is that Java does not define what it means by garbage collection. Some Java garbage collectors do not defragment memory after collection. Heavy use of a StringBuffer can result in highly fragmented memory. This may not be a memory leak, but it can result in the eventual inability to allocate a needed block of memory. The StringBuffer is still highly inefficient compared to the ability to edit characters in place in a string. Such editing requires no memory allocation or deallocation. Neither does it require any gratuitous copying to convert from one type to another. It certainly does not run the risk of fragmenting memory. Jim Rogers Colorado Springs, Colorado USA