2012-06-13 3 views
0

Visual Studio 2010을 사용하는 C# 프로그램에서 SharePoint 문서 라이브러리에 파일을 업로드하는 중 문제가 발생했습니다. 분명히 프로세스 "sqlservr.exe * 32 "는 1GB 이상의 메모리와 같습니다. 파일을 업로드하기 위해 먼저 파일의 바이트를 byte [] 배열로 읽습니다. 그런 다음 배열을 파일로 문서 라이브러리에 업로드하십시오. 각 파일을 업로드하기 전에 메모리를 지울 수있는 방법이 필요합니다.C# 프로세스를 종료하고 시작하는 방법

프로그램은 디렉토리에서 반복되는 루프에서 파일을 업로드합니다. 바이트 배열의 메모리를 지우는 방법이 있습니까?

실제로 "sqlservr.exe * 32"프로세스를 다시 시작하여 메모리를 확보 할 수있는 방법이 있습니까?

이것은 내가 업로드하는 데 사용하는 코드입니다 :

을 내가 웹 사이트에 표시되는 오류 메시지는 (문제는 나는 그것이 메모리 밖으로에서도 때로는 다른 파일을 실행 생각하는 발생할 때 메시지) : 'Test Library/myfolder/file.txt'URL이 잘못되었습니다. 존재하지 않는 파일 또는 폴더를 참조하거나 현재 웹에없는 유효한 파일 또는 폴더를 참조 할 수 있습니다.

using System; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.WebControls; 
using System.IO; 
using Microsoft.SharePoint; 

namespace CustomApplicationPage.Layouts.CustomApplicationPage 
{ 
    public partial class CustomApplicationPage : LayoutsPageBase 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

      // Check if user posted a file 
      if (File1.PostedFile == null) 
      { 
       return; 
      } 

      try 
      { 
       // Get the directories in the path 
       string[] array1 = Directory.GetDirectories("C:\\myfolder\\"); 

       // Declare variables 
       string[] temp; 
       byte[] contents; 
       string dir_name; 
       string file_name; 
       int i, j; 
       int chunk_size = 5; 
       int chunk_index = 0; 
       int start_id = chunk_size * chunk_index; 
       int end_id = 1 + (chunk_size * (chunk_index+1)); 

       // Free the memory 
       GC.Collect(); 

       // For each directory in the main path 
       for (i = 0; i < array1.Length; i++) 
       { 
        // Get directory name 
        dir_name = Path.GetFileName(array1[i]); 

        // Skip this file 
        if (dir_name == "viDesktop_files" || i < start_id || i >= end_id) 
        { 
         continue; 
        } 

        // Get the site 
        SPSite site = new Microsoft.SharePoint.SPSite("http://mysite/"); 

        // Turn security off 
        Microsoft.SharePoint.Administration.SPWebApplication webApp = site.WebApplication; 
        webApp.FormDigestSettings.Enabled = false; 

        // Get site root 
        SPWeb spWeb = site.RootWeb; 

        // Get the specified list/library from the site root 
        SPList docLib = spWeb.Lists["Test Library"]; 

        // Get all files in the directory 
        temp = Directory.GetFiles(array1[i]); 

        // Create a folder in the list/library 
        SPListItem folder = docLib.Folders.Add(docLib.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, dir_name); 

        // Activate the newly created folder 
        folder.Update(); 

        // For each file in the directory 
        for (j = 0; j < temp.Length; j++) 
        { 
         // Get file name 
         file_name = Path.GetFileName(temp[j]); 

         // If the file is a .mht file 
         if (file_name.EndsWith(".mht")) 
         { 
          // Read the contents of the uploaded file into the variable 'contents' 
          contents = File.ReadAllBytes(temp[j]); 

          // Add the file with the specified filename in the folder 
          SPFile file = folder.Folder.Files.Add(file_name, contents); 

          // Activate the file 
          file.Update(); 
         } 
        } 

        // Turn security on 
        webApp.FormDigestSettings.Enabled = true; 

        // Close the site 
        site.Close(); 
       } 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 
    } 
} 
+0

** ** "sqlservr.exe * 32"프로세스 이름을 지정 하시겠습니까? 왜 다른 프로세스의 메모리 사용이 프로세스에 영향을 줄 것이라고 기대합니까? –

+0

@Alexei 안돼. 셰어 포인트와 SQL 서버 (32 비트)입니다. 나는 두 번째 질문에 대답 할 수 없다. –

+0

"우리의 메모리를 실행 중"- 메모리가 부족합니까? 프로세스에 OOM 예외가 발생합니까? OS가 불평? 작업 관리자의 숫자만으로도 불행하게 만들 수 있습니까? –

답변

0

사이트, webApp, spWeb, docLib, 폴더, 파일 및 기타 일회용으로 .Dispose()를 호출 해보십시오.

다른 응용 프로그램에서 소비하는 메모리가 응용 프로그램에서 OutOfMemoryException을 수신하지 않을 가능성이 높습니다. Windows는 필요할 때 가상 메모리를 사용하고 다른 응용 프로그램의 메모리를 페이지 파일에 저장합니다. 32 비트 프로세스에서 응용 프로그램은 2G의 메모리를 사용할 수 있어야합니다 (사용 가능한 RAM이 많지 않은 경우에도 마찬가지입니다).

OutOfMemory 예외에서 보았던 것에서는 일반적으로 clr이 조각화 때문에 내가하려고하는 것에 대해 충분히 큰 메모리 덩어리를 내 응용 프로그램에 제공 할 수 없기 때문에 발생합니다.

어쨌든, 일회용 일을 처리해야합니다.

그리고 btw ... GC.Collect()를 호출 할 필요가 없습니다.

관련 문제