Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

clearing a StringBuffer and re-use his memory

807598May 19 2006 — edited May 5 2008
Hi,

At the moment I have following code:
public void run()
{
	while (true)
		// here collecting data
		StringBuffer tx = new StringBuffer(128);
		tx.append(...);
		// sending the data to some remote peer
This is of course not good. Every time new memory is allocated, while it is better to reuse the memory whitch has a stable point after a few loops. But then I need a method to clear the buffer witch I cannot find. Something like this:
public void run()
{
	StringBuffer tx = new StringBuffer(128);
	while (true)
		// here collecting data
		tx.clear(); // but I connot find sutch a method !!!
		tx.append(...);
		// sending the data to some remote peer
But I cannot find a method Clear or something similar. Someone knows ?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 2 2008
Added on May 19 2006
12 comments
1,612 views