Bug 13670 - System.IO.Compression.DeflateStream finalizer leaks native memory.
Summary: System.IO.Compression.DeflateStream finalizer leaks native memory.
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-07-31 19:12 UTC by Adrian Gallero
Modified: 2013-08-08 04:31 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Adrian Gallero 2013-07-31 19:12:52 UTC
If for any reason you don't dispose a DeflateStream, the finalizer will fail to release all the native memory.

Steps to reproduce:

1) In a new iOS single view application, drop a button and use the following event handler:
       
 partial void test(NSObject sender)
        {
            for (int i = 0; i < 100000; i++) 
            {
                         
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress);
            }
            }
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
        }

2)Deploy to a device (Tested here with iPhone 5). Note that the emulator leaks too, but it will take more time to crash because it has much more memory available.

Expected results:
It works

Actual results:
It crashes. In the emulator, it doesn't crash but memory usage goes over 2 gb.

If you run it under Instruments there are a lot of z_stream leaks that aren't returned to the system.

Looking at the implementation, the issue seems to be that CloseZStream is only called if disposing. It should also be called form the finalizer, or the z_stream member won't be released: 

		protected override void Dispose (bool disposing)
		{
			if (disposing && !disposed) {
				disposed = true;
				IntPtr zz = z_stream;
				z_stream = IntPtr.Zero;
				int res = 0;
				if (zz != IntPtr.Zero)
					res = CloseZStream (zz); // This will Flush() the remaining output if any
Comment 1 Marek Safar 2013-08-08 04:31:41 UTC
Fixed. However the finalizer may not run before OOM is reach therefore you need to Dispose the object manually to get reliable behaviour.