473,545 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StringBuffer replacing String for memory Issues Help!!

10 New Member
Hi All

I wanted to replace the String Concatenation using " + " with a string buffer to eliminate performance hogs.

I have a string

String str = "Testing "+ firstStrVariabl e + "NextTest" + secondStringVar iable + "ThirdTest" ;

Is it worth replacing the String in this case with StringBuffer and StringBuffer.Ap pend

Any Suggestions pl post it
-

Thanks
Vignesh.M
Apr 10 '09 #1
5 2381
JosAH
11,448 Recognized Expert MVP
@VigneshMohan
If at least one of the operands of the + operator is a String the compiler plays a few tricks: it uses a StringBuffer (or StringBuilder) to catenate the Strings. If you don't want to add Strings a million times you can safely use the + operator. Otherwise use the sb.append() method repeatedly.

kind regards,

Jos
Apr 10 '09 #2
VigneshMohan
10 New Member
@JosAH
Hi
JosAH

But appending a string using " + " will create a new string object right

Which all cases will create a new string object with the use of + ?

Will all the different arise only when we use threads?

In other cases are these all same. ?
-
Thanks
Vignesh.M
Apr 10 '09 #3
JosAH
11,448 Recognized Expert MVP
@VigneshMohan
If a, b and c are Strings and you want to 'add' them, the compiler will generate the following:

Expand|Select|Wrap|Line Numbers
  1. String a, b, c;
  2. String result= new StringBuffer(new StringBuffer(a).append(b).toString()).append(c).toString();
  3.  
Manually you could do this with just one StringBuffer:

Expand|Select|Wrap|Line Numbers
  1. String a, b, c;
  2. String result= new StringBuffer(a).append(b).append(c).toString();
  3.  
Note that this all has nothing to do with Threads.

kind regards,

Jos
Apr 10 '09 #4
VigneshMohan
10 New Member
@JosAH


Great ...

I need one more clarification

If I give like this

Expand|Select|Wrap|Line Numbers
  1. String aStringVariable = "welcome";
  2. String test = "sample"+aStringVariable+"sample2";
  3.  

What will the compiler do...?
If we give constants how the compiler interprets internally?

One more Doubt.

StringBuffer is Synchronous and StringBuilder is Unsynchronous
Could you explain this?

-
Thanks

Vignesh.M
Apr 10 '09 #5
JosAH
11,448 Recognized Expert MVP
@VigneshMohan
If the compiler sees a + operator with String operands it generates a StringBuffer given the left String operand; appends the right String operand and retrieves the (new) String by invoking the toString() method on that StringBuffer. If it sees another + operator it'll simply do the same (also see my previous reply).

If you use one Thread only for your StringBuffer operations you can better use a StringBuilder because the latter doesn't synchronize its methods (compare with Vector and ArrayList).

kind regards,

Jos
Apr 10 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

16
2857
by: Justin Lazanowski | last post by:
Cross posting this question on the recommendation of an I have a .NET application that I am developing in C# I am loading information in from a dataset, and then pushing the dataset to a grid, or other controls depending on the particular form. This application is setup with one MDI parent calling MDI children with the exception of one...
7
2424
by: Rich Denis | last post by:
Hello, I have been trying to solve a mysterious memory leak problem and was hoping that you could help me out on my stuck point. First a bit of background. We have two app servers in an app center cluster that are each running a number of web applications. For some reason one of the boxes' asp_wp process keeps recylcing on us. It seems...
16
2373
by: JCauble | last post by:
We have a large Asp.net application that is currently crashing our production servers. What we are seeing is the aspnet_wp eat up a bunch of memory and then stop unexpectedly. Does not recycle. Example: After about 5 hours with a concurrent user base of about 150 users the application raises the aspnet_wp memory usage by almost 500MB. If...
3
8925
by: shaft | last post by:
hi i have the following code : string addrress; cin >> address; cout << address; if I enter any string with space in between , it will save the first word only. e.g. if i enter "this is a test"
1
1891
by: karunashok | last post by:
hi, i want to read one html file and replace the string between the start and end tag with some string. i need help please help me.
3
1245
by: sharonrao123 | last post by:
Hello all, I have a products table (see below) Product State Apple |1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27| I have to display the state data on a webform replacing the numbers with actual state names for example replace 1 with auckland, 2 with northland and so on. How to do this in SQL. Please help. ...
1
366
by: Daniel | last post by:
i seem to be having memory issues, will deplying a release build instead of a debug build help with memory usage of my .net process?
3
3840
by: Mike Howarth | last post by:
Hi Seem to be having a bit of brainfreeze this evening. Basically I'm reducing an array of prices like so: This gives me a string of '86.00.00' which I am trying to use with decimal objects. Python 2.4 is not particularly happy with this. Additionally I can't simply convert the string to a decimal as it would be
1
4458
by: mono12 | last post by:
Hi! I have written code using STL hash_map in c++ and am running into memory issues. I have two kinds of hash_maps: A <int,int> which stores ~5million key-value pairs, and needs to be accessed often to store in memory, its not updated during the program. B <int,hash_map<int,float>> which is also ~5m key-value pairs, but I can read this in...
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.