?
Solved

stringbuilder out of memory error

Posted on 2014-07-29
5
Medium Priority
?
201 Views
Last Modified: 2016-06-21
I have some code that gets documents from a sharepoint library via a webservice.
During testing I attempted to get a 220mb document and get this error
An error has occurred: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.Text.StringBuilder.ToString() at System.Xml.XmlTextReaderImpl.ParseText()

Is there anything I can do to get around this?
This is the code I am using.

 public void CopyFile()
        {
            string webUrl = "http://xxx";
            FileCopy.Copy copyService = new FileCopy.Copy();
            copyService.Url = webUrl +"/_vti_bin/copy.asmx";
            NetworkCredential m_credentials = new NetworkCredential("x", "x", "x");
                 copyService.Credentials = m_credentials;

            //Source and Destination Document URLs
             string sourceUrl = "http://x/Documents/1784.pdf";
             string destinationUrl = "C:\\Documents\\1784.pdf";

            //Variables for Reading metadata’s of a document
             FileCopy.FieldInformation fieldInfo = new FileCopy.FieldInformation();
             FileCopy.FieldInformation[] fieldInfoArray = { fieldInfo };
             FileCopy.CopyResult cResult1 = new FileCopy.CopyResult();
             FileCopy.CopyResult cResult2 = new FileCopy.CopyResult();
             FileCopy.CopyResult[] cResultArray = { cResult1, cResult2 };

        

            //Receive a Document Contents  into Byte array (filecontents)
             byte[] fileContent; // no need to initialize the GetItem takes care of that.
             copyService.GetItem(sourceUrl, out fieldInfoArray, out fileContent);

            //Create a new file and write contents to that document
             FileStream fStream = new FileStream(destinationUrl, FileMode.Create, FileAccess.ReadWrite);
            fStream.Write(fileContent, 0, fileContent.Length);
            fStream.Close();
        }
    }

Open in new window

0
Comment
Question by:QPR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 40
ID: 40228342
What line triggers the error.

Since the error happens in a StringBuilder and there is none in your code, it is probably triggered inside of something you are calling.

What are that FileCopy and class that you are using? If this is one of your classes, look in there. If it is something from a third party, get in contact with them.
0
 
LVL 29

Author Comment

by:QPR
ID: 40228350
It is failing on this line
copyService.GetItem(sourceUrl, out fieldInfoArray, out fileContent);

GetItem is a sharepoint webservice.
Your post has led me in the right direction tough.
Seems there is a 50mb limt lthough this can be configured up to the 2GB limit(SQL)
http://www.codeproject.com/Articles/63861/Limitations-of-SharePoint-Web-Services

If you look at the section called "Issues with Downloading Files" you will see what I am seeing.
It talks of single buffers and alternative methods but it is getting a bit beyond my expertise.
Would this webclient class be easy enough to ut in m coe and replace te SP built in webservice?
0
 
LVL 29

Accepted Solution

by:
QPR earned 0 total points
ID: 40228353
Nevermind I simply replaced all of the above code with this
 string url = "http://xx/Documents/1784.pdf";
            string localfileName = @"C:\Documents\1784.pdf";
            // Create a new WebClient instance.
            WebClient myWC = new WebClient();
            // Set the credentials
            myWC.Credentials = new NetworkCredential("x", "x", "x");
            // Download the file and save it into the local path.
            myWC.DownloadFile(url, localfileName); 

Open in new window


and it works perfectly! The limitation was SP based.
Anything I should know about the code I am using? problems/overheads/alterations?
0
 
LVL 40
ID: 41656660
Your post has led me in the right direction tough.

Isn't this enough?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question