Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StringBuilder dimensions 3

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

Can someone explain to me why StringBuilder is auto sized to 16 when initialised?

All other Arrays & Collections are either zero size, or fixed size. (capacity)

The collections framework where size can be dynamic is always zero length to start with and grows as the elements are added.

Why is StringBuilder sized in this way?



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
You end up at nearly the same point. By starting with
Code:
StringBuilder b = new StringBuilder(100);
you avoid the internal process of creating a new array and copying the old array into the new one, several times.

-----------------------------------------
I cannot be bought. Find leasing information at
 
So Dian is correct, it's worthless and pointless it being 16 in size and you not explicitly giving it a size when created.

I guess I was right to question why it was 16 and not make sense to me!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
I wouldn't say worthless or pointless. I'd say someone had good reason to do it, and that if you don't specify a size its likely to be slightly more efficient than if it was initialized to zero or one. How they came up with the number 16, I don't know. I don't work for them yet. Dian is correct that it isn't optimal to use the default. I'm nearly positive that they've changed the internal implementation of StringBuilder but left the 16 alone. I find it difficult to believe they decided to set it to 16 with no reason whatsoever.

-----------------------------------------
I cannot be bought. Find leasing information at
 
Hey, ho, either way we still don't have an answer to the question [lol]

But thanks for all your posts :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Some points:

- You can force garbage collection
- GC policy is a compromise between memory and CPU consumption, and its use us beyond string concatenation
- What I meant (or tried to mean) with "No one should care about the default initialization size" is that if you are a performance freak, then you will estimate the size and use it, probably with a pesimistic factor and if you don't care about performance, you won't even realize there's such a value
- And last but not least: application performance uses to be far away from these kind of considerations. There's no point on deep analyzing string concatenations if your application uses a query to a database without using indexes, for example

Cheers,
Dian
 
You can force garbage collection

Huh? I've read that explicitly states although you can request garbage collection you CANNOT force it.

You might issue the command to garbage collect, but the Java VM will do what it wants, when it wants and you have no control.

Is this wrong?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
You can force JVM to "make its best effort to recycle all discarded objects" with System.gc()

Cheers,
Dian
 
Yes, so the info I read said, but it also had a caveat that System.gc() is only a request to garbage collect, it doesn't force it.

Anyways, I don't think it's going to matter either way for the current course I'm studying...not long now and it's exam time!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
System.gc() can be nearly 'forced' if called 3 to 8 times in succession... it then moves all collect-able stuff to the oldest section, so it gets cleaned asap.
 
[lol] you have to write a loop or 8 statements to try to force garbage collection.

I guess what I read was right then.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
All other Arrays & Collections are either zero size, or fixed size. (capacity)

In contrast from javadocs to Vector:
Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.
Similar for ArrayList
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

don't visit my homepage:
 
oh right, I was thinking Vector Graphics!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top