Solved

stringbuilder out of memory error

Posted on 2014-07-29
5
90 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System.Security 2 27
imap mails 1 22
Footer for each row on Gridview 2 19
Save ms data to server side. 19 41
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now