Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

.Net Framework 4.0: Using memory mapped files

.Net Framework 4.0 introduces memory mapped files. Memory mapped files are useful when you need to do in-memory data manipulation and your data structures are large. For large in-memory data the performance of memory mapped file is the best. It is much faster than MemoryStream. And like files on hard disc, memory mapped files can be shared between different programs. MemoryMappedFile and other classes for memory mapped files can be found from System.IO.MemoryMappedFiles namespace.

 

NB! This blog is moved to gunnarpeipman.com

Click here to go to article

Comments

Cameron said:

Do you know if I can try these examples on VS2008,

I have installed the Net framework 4 beta 1.

Thank you

# June 24, 2009 11:13 AM

DigiMortal said:

Thanks for comments, guys! :)

Aaron: I made no tests with different boundary values. Also I made no tests in 64bit environment.

Cameron: AFAIK you cannot use .Net Framework 4 Beta 1 features in VS2008.

# July 3, 2009 3:20 AM

Bill Jenkins said:

Console based examples worked great. If I put inside a windows form, the client works fine, but the server doesn't. Am I missing something here? I used the VB code provided but wrapped in a form.

# August 5, 2009 4:37 PM

DigiMortal said:

Hello Bill!

Did your VB client worked okay against console based server?

# August 5, 2009 4:55 PM

Bill Jenkins said:

Thanks for responding.

Yes, both versions of the VB client (console-based and windows-based) worked, but only for the console-based server. Both failed with a "cannot find file" exception when I used the windows-based server.

# August 5, 2009 8:02 PM

DigiMortal said:

I was not able to find a reason for this behavior. Did you try to run both of them under same privileges? Did you try to run them under admin privileges?

# August 6, 2009 1:53 AM

Bill Jenkins said:

Thanks for asking. All were run under the same privileges. Here is the code:

Imports System.IO.MemoryMappedFiles

       Using file = MemoryMappedFile.CreateNew("myFile", Integer.MaxValue)

           Dim bytes = New Byte(23) {}

           For i = 0 To bytes.Length - 1

               bytes(i) = CByte(i + 65)

           Next

           Using writer = file.CreateViewAccessor(0, bytes.Length)

               writer.WriteArray(Of Byte)(0, bytes, 0, bytes.Length)

           End Using

       End Using

# August 6, 2009 12:19 PM

Bill Jenkins said:

Also, I have Admin privilege on my computer (XP-32) and run my programs in that mode.

# August 6, 2009 3:04 PM

DigiMortal said:

Hmm... seems like I have to handle this problem over to .Net development team.

# August 6, 2009 5:14 PM

Bill Jenkins said:

Thanks, DigiMortal. I hope it's not me. Good luck!

# August 6, 2009 7:43 PM

DigiMortal said:

Heh, good joke! :)

Bill, I reported this problem to Connect. You can find it here: connect.microsoft.com/.../ViewFeedback.aspx

If you have more details to give then feel free to drop me a line. Also you can keep your eye on this Connect ticket to see if there is solution to your problem.

# August 7, 2009 1:21 AM

amazify said:

isnt it because the writer is getting disposed? your method exits right after creating the writer etc...

# July 22, 2011 12:20 PM

dmolley said:

I am trying to use the MMF feature between a Winform application and an ASP.net program. The idea being the WinForm app creates and populates the MMF and the ASP.net Web program displays the data from the MMF.

However the ASP program returns "FileNotFound" Error. This seems to be some sort of permissions between the two processes and they are not able to see the MMF between them. Any ideas?

# February 2, 2012 11:31 PM