I agree with what your saying but other people have had this issue...it worked for me and haven't worried about it.
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Mark Hurd Sent: Friday, 13 September 2013 6:20 PM To: ozDotNet Subject: Re: out of memory..urgent...Solution I am surprised this solves your memory problem, as although the UTF8 GetString(Byte[]) defers to Encoding's base method, that returns the result of GetString(Byte[], int, int) which is overridden by UTF8, which calls String.CreateStringFromEncoding. This uses String.FastAllocateString to create the string of size based upon UTF8's override of GetCharCount, which I haven't reviewed closely, but it doesn't look like it's an estimate :-) The String's internal Char[] is manipulated directly by UTF8's internal GetChars, so unless GetCharCount does get it vastly wrong, I don't see how your "fix", which starts with a 16 byte StringBuilder buffer that will be increased by the 100000 chars each time you Append, with the existing content copied across each time. In summary, in your last loop iteration there will need to be almost twice your whole string required in memory for a short time as the last chunk is Appended. Whereas as far as I see in the (Reflected) code, the simple GetString should just hold the whole String once and work within it. So if your fix really is a fix, I suggest there's a bug in UTF8's GetCharCount (or I'm wrong and it /is/ just estimating how many Chars are needed). -- Regards, Mark Hurd, B.Sc.(Ma.)(Hons.) On 13 September 2013 14:16, <[email protected]> wrote: > If you are interested..memeory issue was resolved by doing the > following. > > > > > > Public Shared Function byteArrayToString(ByVal b() As Byte) As > String > > Dim ss As New System.Text.UTF8Encoding > > Dim sString As String > > Dim sb As New StringBuilder > > Dim cursor As Integer > > Dim sChunk As String > > Try > > > > > > > > ' sString = System.Text.Encoding.UTF8.GetString(b) > > > > While cursor < b.Length > > > > Dim arr2() As Byte > > > > If (cursor + 100000) > (b.Length) Then > > arr2 = New Byte(b.Length - cursor - 1) {} > > Array.Copy(b, cursor, arr2, 0, b.Length - cursor) > > Else > > arr2 = New Byte(100000 - 1) {} > > Array.Copy(b, cursor, arr2, 0, 100000) > > End If > > > > > > sChunk = System.Text.Encoding.UTF8.GetString(arr2) > > sb.Append(sChunk) > > cursor += 100000 > > > > End While > > > > ' sString = ss.GetString(b) > > Return sb.ToString > > Catch ex As Exception > > Throw ex > > End Try > > > > End Function > > > > > > > > Anthony > > Melbourne StuffUps.learn from others, share with others! > > http://www.meetup.com/Melbourne-Ideas-Incubator-Stuffups-Failed-Startu > ps/ > > > ---------------------------------------------------------------------- > ------------ NOTICE : The information contained in this electronic > mail message is privileged and confidential, and is intended only for > use of the addressee. > If you are not the intended recipient, you are hereby notified that > any disclosure, reproduction, distribution or other use of this > communication is strictly prohibited. > If you have received this communication in error, please notify the > sender by reply transmission and delete the message without copying or > disclosing it. (*13POrtC*) > ---------------------------------------------------------------------- > ------------- > > > > From: [email protected] > [mailto:[email protected]] > On Behalf Of David Kean > Sent: Wednesday, 11 September 2013 2:20 AM > To: ozDotNet > Subject: RE: out of memory..urgent > > > > Memory isn't unlimited. Basically, when you convert from a byte array > -> string, you have two copies of the same data (one for the byte > array and one for the string) in memory. > > > > What exactly are you doing? You are typically better off chunking and > reading smaller amounts of data at a time. Use something like a > StreamWriter over a stream to automatically handles the byte -> text conversion. > > > > From: [email protected] > [mailto:[email protected]] > On Behalf Of [email protected] > Sent: Monday, September 9, 2013 8:05 PM > To: ozDotNet > Subject: out of memory..urgent > > > > Getting out of memory exception when I try to > > > > Dim s as string > > Dim b() as Byte > > > > s=System.Text.Encoding.GetEncoding("utf-8).GetString(b) > > > > Definitely something about the length of b..works fine most of the > time except if b length is very large > > > > Anthony > > Melbourne StuffUps.learn from others, share with others! > > http://www.meetup.com/Melbourne-Ideas-Incubator-Stuffups-Failed-Startu > ps/ > > > ---------------------------------------------------------------------- > ------------ NOTICE : The information contained in this electronic > mail message is privileged and confidential, and is intended only for > use of the addressee. > If you are not the intended recipient, you are hereby notified that > any disclosure, reproduction, distribution or other use of this > communication is strictly prohibited. > If you have received this communication in error, please notify the > sender by reply transmission and delete the message without copying or > disclosing it. (*13POrtC*) > ---------------------------------------------------------------------- > ------------- > >
