470,836 Members | 1,846 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,836 developers. It's quick & easy.

Releasing the resource retrieved by Assembly.GetManifestResourceStream()

Hello,

I'm developing an application that periodically reads embedded assembly
resources. The resources are text-based, and are about 3-7KB in size,
and the application accesses them by calling the
Assembly.GetManifestResourceStream() method.

During a long period of execution, I've noticed that the memory usage
of the application increases; specifically, it appears that the
System.IO.__UnmanagedMemoryStream returned by
GetManifestResourceStream() is never collected, even when the owning
thread leaves the scope in which it is defined.

I used SciTech's memory profiler to track down this issue and was able
to reproduce it with the appended code.

Does someone know if there is a way to force the release of the
resources used by System.IO.__UnmanagedMemoryStream? Perhaps I am not
using Assembly.GetManifestResourceStream() as it is intended.

Any ideas or suggestions are appreciated!
Sample Program:

// Wrapper for Assembly.GetManifestResourceStream().
class ResourceReader {

public ResourceReader( string strResourceFile ) {
cstrResourceFile = strResourceFile;
}

public string GetText() {
Stream oResourceStream = null;
TextReader oReader = null;

try {
oResourceStream =
Assembly.GetExecutingAssembly().GetManifestResourc eStream(
cstrResourceFile );
oReader = new StreamReader( oResourceStream );
return oReader.ReadToEnd();
}
catch( Exception ) {
return String.Empty;
}
finally {
if( oResourceStream != null ) {
oResourceStream.Flush();
oResourceStream.Close();
}

if( oReader != null ) {
oReader.Close();
}
}
}

private string cstrResourceFile;
}

// Program entry point.
class TestHarness {

static void Main(string[] args) {
string data = null;
ResourceReader oReader = new ResourceReader(
"ConsoleApplication1.TextFile1.txt" );
int n = 0;

while( true ) {
data = oReader.GetText();
System.Threading.Thread.Sleep( 3000 );

if( n % 250 == 0 ) {
System.GC.Collect();
}

++n;
}
}
}

Sincerely,
Steve Guidi

Nov 22 '05 #1
0 1508

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Drew | last post: by
12 posts views Thread by bj7lewis | last post: by
11 posts views Thread by Garrett | last post: by
6 posts views Thread by cql90 | last post: by
reply views Thread by poojo hackma | last post: by
4 posts views Thread by Peter Larsen [] | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.