Re: C# System.String Memory Usage



You're right - StringBuilder is the way to get [out] string values. I think
it's fair to say that every [out] string item on pinvoke.net uses
StringBuilder for the returned string.
--
Phil Wilson
[Microsoft MVP Windows Installer]

"John Saunders" <john.saunders at trizetto.com> wrote in message
news:eBO7$NuGHHA.4440@xxxxxxxxxxxxxxxxxxxxxxx
<mwhalber@xxxxxxxxx> wrote in message
news:1165579492.712295.93700@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,

I have created a service which parses real-time prices from one format
into another. This is all working very quickly and nicely, however, I
have recently found that the memory usage is astronomical after a few
hours. (1.5-2 Gig)

After a good amount of digging I found that the memory usage is is all
in System.String and contained within the pricing object. This object
makes extensive use of a single string object which is passed as an OUT
parameter to a Interop (unmanaged) object.

There are approximately 30 calls to the unmanaged interop object within
the class, and the class is called upwards of 3-4 times per second. I
know that System.Strings are immutable, therefore, I am assuming that
each time this call returns within the class, a new String object is
being created and allocated. The issue is that many of these objects
are being promoted to Gen2 and never being finalized/destroyed by the
GC. The GC apparently assumes that all these string objects are still
alive and will not remove them. I think that this is due to the
frequency at which these objects are being created.

Unfortunately I cannot mitigate the impact using StringBuilder, as the
parameters are OUT string on the interop assembly (this could be
changed by my colleague who wrote it, if neccesary.)

Anyway, does anyone have any idea how to get the memory usage down to a
manageable level?

Are you sure you can't use StringBuilder on an out parameter? I'm pretty
sure I've used it with P/Invoke.

John




.



Relevant Pages

  • Re: Fastcode Trim B&V 0.3.0
    ... most important performance metrics for me are speed and memory usage. ... such as in the case of Trim. ... The scenario in which the fact that trim does not return a unique string ...
    (borland.public.delphi.language.basm)
  • Re: Object#freeze as a basis for caching of method results?
    ... > on memory usage. ... If the String is short enough that approach is ... just marshalling... Demarshalling was the option I had thought of. ...
    (comp.lang.ruby)
  • Re: ListView FindItem
    ... Instr() seems to have a limit to the size of the string it can test. ... Do not want to double memory usage by putting the value in the collection ... ' use InstrRev to find the last delimiter, then use Left$ to get only ...
    (microsoft.public.vb.general.discussion)
  • Re: Stringbuilder and SelectCommand
    ... that the strings were large enough that their memory usage is significant. ... if the new string that you are going to construct is going ... The old "alloca" statement that allowed for allocating ... EQ, even when no players were in a particular spot, the server kept ...
    (microsoft.public.dotnet.languages.csharp)
  • performace issue in Tk
    ... To test if an object is empty it just convert every object into a string. ... it converts all lists, integers and so on into strings. ... with my fix I reduced memory usage from 200MB to 110MB by large amount of data. ...
    (comp.lang.tcl)

Loading