HighTechTalks DotNet Forums  

String Immutability & StreamReader

Dotnet Framework (Performance) microsoft.public.dotnet.framework.performance


Discuss String Immutability & StreamReader in the Dotnet Framework (Performance) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Paul Nations
 
Posts: n/a

Default String Immutability & StreamReader - 10-21-2004 , 11:57 AM






Since strings are immutable, isn't this code:

Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String

Do
line = sr.ReadLine()
Loop Until line Is Nothing

causing the string object 'line' to be constantly recreated for each line in
the file?

I have an app that reads very large (90K+lines) text files, parses the line
and inserts rows into a SQL Server database. It's running pretty well but
the memory usage is topping out over 30 MB, this is alarming to me. The old
VB6 app would run at about 7 MB.

Am I misunderstanding the whole immutability thing or obsessing over
nothing?





Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: String Immutability & StreamReader - 10-21-2004 , 12:07 PM






Paul Nations <pauln (AT) adhe (DOT) arknet.nospam.edu> wrote:
Quote:
Since strings are immutable, isn't this code:

Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String

Do
line = sr.ReadLine()
Loop Until line Is Nothing

causing the string object 'line' to be constantly recreated for each line in
the file?
line isn't an object. It's a variable. But yes, each iteration, line
takes a different value.

Quote:
I have an app that reads very large (90K+lines) text files, parses the line
and inserts rows into a SQL Server database. It's running pretty well but
the memory usage is topping out over 30 MB, this is alarming to me. The old
VB6 app would run at about 7 MB.

Am I misunderstanding the whole immutability thing or obsessing over
nothing?
It could be that you're missing something else entirely. Have you tried
using the file-reading code on its own, and seeing what the memory
usage is like there?

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Reply With Quote
  #3  
Old   
Sriram Krishnan
 
Posts: n/a

Default Re: String Immutability & StreamReader - 10-21-2004 , 12:26 PM



Try using CLRProfiler and seeing where that 30 MB is coming from

--
Sriram Krishnan

http://www.dotnetjunkies.com/weblog/sriram


"Jon Skeet [C# MVP]" <skeet (AT) pobox (DOT) com> wrote

Quote:
Paul Nations <pauln (AT) adhe (DOT) arknet.nospam.edu> wrote:
Since strings are immutable, isn't this code:

Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String

Do
line = sr.ReadLine()
Loop Until line Is Nothing

causing the string object 'line' to be constantly recreated for each line
in
the file?

line isn't an object. It's a variable. But yes, each iteration, line
takes a different value.

I have an app that reads very large (90K+lines) text files, parses the
line
and inserts rows into a SQL Server database. It's running pretty well
but
the memory usage is topping out over 30 MB, this is alarming to me. The
old
VB6 app would run at about 7 MB.

Am I misunderstanding the whole immutability thing or obsessing over
nothing?

It could be that you're missing something else entirely. Have you tried
using the file-reading code on its own, and seeing what the memory
usage is like there?

--
Jon Skeet - <skeet (AT) pobox (DOT) com
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Reply With Quote
  #4  
Old   
Bob Grommes
 
Posts: n/a

Default Re: String Immutability & StreamReader - 10-21-2004 , 12:29 PM



Yes, a new string is created each time.

A cursory scan of any .NET newsgroup or list will yield thousands of
panicked messages about apprarent memory-hogging events. The short answer
is that released memory is reflected in Task Manager when it's needed by the
OS, not when .NET is through with it. Also, you must allow for the runtime,
which is shared amongst any .NET apps that are running.

I have written a lot of programs of the sort you mention, and the
performance is excellent, even in debug builds. Frankly I've never even
checked memory consumption because (1) they work all day long, day after
day, unattended, for years, (2) they're plenty fast. In fact, I use a
generalized DelimitedTextFile() class that returns ArrayLists of field
values for each record in a comma-delimited file, and even with all that
parsing and assigning, and with tons of data conversion overhead
(rearranging field orders, tranforming values and the like), I can plow
through dozens of megabytes of input data in seconds.

In my experience if you encoutner actual performance problems it's a
solvable issue by refactoring your code. The only time I've had code that
ran slower than I would have liked was in a memory-intensive code section
that made extensive use of string interning, but that was a concious tradoff
of lower speed for better memory efficiency, and it's still adequately fast.

So, likely you *are* obsessing over nothing. ;-)

C# has good memory management -- you need look at memory consumption and
performance only if actual out-of-memory or slow performance issues arise,
and when they do, they will almost always be because you can improve your
code, not because you have to work around or fight some terrible deficiency
in the runtime environment.

--Bob

"Paul Nations" <pauln (AT) adhe (DOT) arknet.nospam.edu> wrote

Quote:
Since strings are immutable, isn't this code:

Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String

Do
line = sr.ReadLine()
Loop Until line Is Nothing

causing the string object 'line' to be constantly recreated for each line
in the file?

I have an app that reads very large (90K+lines) text files, parses the
line and inserts rows into a SQL Server database. It's running pretty
well but the memory usage is topping out over 30 MB, this is alarming to
me. The old VB6 app would run at about 7 MB.

Am I misunderstanding the whole immutability thing or obsessing over
nothing?



Reply With Quote
  #5  
Old   
Paul Nations
 
Posts: n/a

Default Re: String Immutability & StreamReader - 10-21-2004 , 01:27 PM



Actually, the .Net version is faster than the old VB6 app. It will process
the 90K+ line file in about 10 min .vs. 15 for the VB6 version. And the 30
MB is easily within the specs of modern workstations. But, I have to plan
for this system to run at a few very small colleges with limited hardware.

I'm not desperate for another method, but I was just trying to find out if
there were some other method of reading a text file into a string. It
appears this is the standard method and I will run with it.

Thanks all.


"Bob Grommes" <bob (AT) bobgrommes (DOT) com> wrote

Quote:
Yes, a new string is created each time.

A cursory scan of any .NET newsgroup or list will yield thousands of
panicked messages about apprarent memory-hogging events. The short answer
is that released memory is reflected in Task Manager when it's needed by
the OS, not when .NET is through with it. Also, you must allow for the
runtime, which is shared amongst any .NET apps that are running.

I have written a lot of programs of the sort you mention, and the
performance is excellent, even in debug builds. Frankly I've never even
checked memory consumption because (1) they work all day long, day after
day, unattended, for years, (2) they're plenty fast. In fact, I use a
generalized DelimitedTextFile() class that returns ArrayLists of field
values for each record in a comma-delimited file, and even with all that
parsing and assigning, and with tons of data conversion overhead
(rearranging field orders, tranforming values and the like), I can plow
through dozens of megabytes of input data in seconds.

In my experience if you encoutner actual performance problems it's a
solvable issue by refactoring your code. The only time I've had code that
ran slower than I would have liked was in a memory-intensive code section
that made extensive use of string interning, but that was a concious
tradoff of lower speed for better memory efficiency, and it's still
adequately fast.

So, likely you *are* obsessing over nothing. ;-)

C# has good memory management -- you need look at memory consumption and
performance only if actual out-of-memory or slow performance issues arise,
and when they do, they will almost always be because you can improve your
code, not because you have to work around or fight some terrible
deficiency in the runtime environment.

--Bob

"Paul Nations" <pauln (AT) adhe (DOT) arknet.nospam.edu> wrote in message
news:eWirG74tEHA.260 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
Since strings are immutable, isn't this code:

Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String

Do
line = sr.ReadLine()
Loop Until line Is Nothing

causing the string object 'line' to be constantly recreated for each line
in the file?

I have an app that reads very large (90K+lines) text files, parses the
line and inserts rows into a SQL Server database. It's running pretty
well but the memory usage is topping out over 30 MB, this is alarming to
me. The old VB6 app would run at about 7 MB.

Am I misunderstanding the whole immutability thing or obsessing over
nothing?





Reply With Quote
  #6  
Old   
cody
 
Posts: n/a

Default Re: String Immutability & StreamReader - 11-03-2004 , 08:47 AM



You could use the mthode ReadToEnd() which will read the entire file into
one string which should be faster but the difference is that you have all of
the 90k at once in memory.
You could also use Read or ReadBlock so you can use a char array instead of
having created temporary strings all the time.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Paul Nations" <pauln (AT) adhe (DOT) arknet.nospam.edu> schrieb im Newsbeitrag
news:e1JQZt5tEHA.2804 (AT) TK2MSFTNGP14 (DOT) phx.gbl...
Quote:
Actually, the .Net version is faster than the old VB6 app. It will
process
the 90K+ line file in about 10 min .vs. 15 for the VB6 version. And the
30
MB is easily within the specs of modern workstations. But, I have to plan
for this system to run at a few very small colleges with limited hardware.

I'm not desperate for another method, but I was just trying to find out if
there were some other method of reading a text file into a string. It
appears this is the standard method and I will run with it.

Thanks all.


"Bob Grommes" <bob (AT) bobgrommes (DOT) com> wrote in message
news:%234nK6N5tEHA.1400 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
Yes, a new string is created each time.

A cursory scan of any .NET newsgroup or list will yield thousands of
panicked messages about apprarent memory-hogging events. The short
answer
is that released memory is reflected in Task Manager when it's needed by
the OS, not when .NET is through with it. Also, you must allow for the
runtime, which is shared amongst any .NET apps that are running.

I have written a lot of programs of the sort you mention, and the
performance is excellent, even in debug builds. Frankly I've never even
checked memory consumption because (1) they work all day long, day after
day, unattended, for years, (2) they're plenty fast. In fact, I use a
generalized DelimitedTextFile() class that returns ArrayLists of field
values for each record in a comma-delimited file, and even with all that
parsing and assigning, and with tons of data conversion overhead
(rearranging field orders, tranforming values and the like), I can plow
through dozens of megabytes of input data in seconds.

In my experience if you encoutner actual performance problems it's a
solvable issue by refactoring your code. The only time I've had code
that
ran slower than I would have liked was in a memory-intensive code
section
that made extensive use of string interning, but that was a concious
tradoff of lower speed for better memory efficiency, and it's still
adequately fast.

So, likely you *are* obsessing over nothing. ;-)

C# has good memory management -- you need look at memory consumption and
performance only if actual out-of-memory or slow performance issues
arise,
and when they do, they will almost always be because you can improve
your
code, not because you have to work around or fight some terrible
deficiency in the runtime environment.

--Bob

"Paul Nations" <pauln (AT) adhe (DOT) arknet.nospam.edu> wrote in message
news:eWirG74tEHA.260 (AT) TK2MSFTNGP11 (DOT) phx.gbl...
Since strings are immutable, isn't this code:

Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String

Do
line = sr.ReadLine()
Loop Until line Is Nothing

causing the string object 'line' to be constantly recreated for each
line
in the file?

I have an app that reads very large (90K+lines) text files, parses the
line and inserts rows into a SQL Server database. It's running pretty
well but the memory usage is topping out over 30 MB, this is alarming
to
me. The old VB6 app would run at about 7 MB.

Am I misunderstanding the whole immutability thing or obsessing over
nothing?







Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.