473,395 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

StringBuilder and memory management

I'm constructing a long SQL string using StringBuilder. Every 100 or so
iterations, I write the SQL to the database, and use the
StringBuilder.Remove method to "zero out" the string: e.g.,
"oStringBuilder.Remove(0,oStringBuilder.Length )". I then reuse
oStringBuilder to build the next SQL statement.

Sometimes this coding works fine, but as often as not the system reports
an out-of-memory error. I'm guessing that the Remove method deletes the
text in a string, but does not remove its memory allocation, and when I
reuse it, the string grows ever larger.

Is there any mechanism to destroy memory used -- or shrink it to zero
(or something!) -- within the StringBuilder class? If not, any other ideas?

Many thanks.

-- Brent

//==================================================
//Code snippet builds a valid MySQL statement
sqlstart = "Insert into myTable (Field1, Field2) Values ";
string field;
int count = 1;
StringBuilder sql = new StringBuilder(sqlstart);
char[] fieldsplitter = {'|'};

//after getting an array of text rows
for(int i = 0; i < arrRows.Length; i++, count++)
{
string[] arrFields = arrRows[i].Split(fieldsplitter);
sql.Append("(");
for(int j=0; j < arrFields.Length; j++)
{
field = arrFields[j];
sql.Append("'"+field+"',");

}
if (count==100 | i == arrRows.Length - 2)
{
sql.Append(");");
try
{
//write SQL to DB using "run" class
run.exec(sql.ToString());
}
catch //error in sql
{
Response.Write("SQL error: " + sql);

}

count = 0;
sql.Remove(0,sql.Length);
sql.Append(sqlstart);

}
else{sql.Append("),");}
}

//===============================================
Nov 19 '05 #1
2 1460
why not just reinstantiate the object? i don't know how the
stringbuilder class keeps references to its data, so it might not be
killing all references to it (keeping gc from happening).
reinitializing the stringbuilder ought to fix it if it's causing the
problem.

if that doesn't fix it, i'd suggest running a profiler on your
application to see where memory is getting sucked out.

hth
terry

Nov 19 '05 #2
Thanks for the tip. Turns out that I was reading in very large strings
from a file, and the memory was eaten up by that portion of the code,
not the code I posted earlier.

Thanks for your help!

theath wrote:
why not just reinstantiate the object? i don't know how the
stringbuilder class keeps references to its data, so it might not be
killing all references to it (keeping gc from happening).
reinitializing the stringbuilder ought to fix it if it's causing the
problem.

if that doesn't fix it, i'd suggest running a profiler on your
application to see where memory is getting sucked out.

hth
terry

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
15
by: DV | last post by:
I have a StringBuilder that has a string with 12,000,000 characters. When I do a ToString(), I expect to have ~25,000,000 bytes worth of memory, yet, I end up with ~43,000,000 bytes. That's...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
7
by: Arnd Iffland | last post by:
Hi, i want to use a StringBuilder to create a large string. After instatiation, the MaxCapacity is 2.147.483.647, what is large enough for my purpose. During my procedure i get a...
26
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW:...
8
by: Arjan | last post by:
Hello, I have been looking for a couple of days now, but I can't find anything about how to deal with StringBuilder and releasing the memory used by it. When I use stringbuilder, the memory...
5
by: =?Utf-8?B?SGFycnkgVg==?= | last post by:
I'm using a StringBuilder to build a SelectCommand string that returns a number of records. If that number is >0, I want to fill an arraylist with fields from the records for a listbox. Is it...
13
by: Tony Johansson | last post by:
Hello! I read in a book and here is a question and the answer that I'm not satisfied with. When should you use the StringBuilder class instead of the String class. 1.When building a string from...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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...

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.