Bug 25858 - iOS:Big memory block mallocing fails.
Summary: iOS:Big memory block mallocing fails.
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: master
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-01-08 23:11 UTC by Neil
Modified: 2015-01-12 02:54 UTC (History)
3 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 Developer Community or GitHub 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 ANSWERED

Description Neil 2015-01-08 23:11:28 UTC
Only happens on real iOS device(iPhone etc), not in simulator.

create a single view app, 

drop a button on the view.

Handle the  "HandleCompressTouched()" event of the button.

In this event, only write one line of code like following:

============================
private void HandleCompressTouched(object sender, EventArgs e) {

  int[] temp = new int[1024* 1024 * 64] ; //malloc 64M * 4 bytes memory, and drop it.
  //do nothing but drop the memory.
}

============================

Run the app in a real deivce, eg: iPhone6.

Touch the button for multiple times.  The app will crash at a "OutOfMemoryException" after 2 or 3 times.
Comment 1 Ram Chandra 2015-01-09 08:14:48 UTC
I have tried to reproduce this issue but I am unable to reproduce this issue.

To reproduce this issue I have created a Single view application of iOS and add a button to the view but I didn't able to find the  "HandleCompressTouched()" event of button. 

I tried to reproduce this behavior when I put the following code in button "touchupinside" event I observed when I touch the button multiple times, app throws the "OutOfMemeoryException". 

int[] temp = new int[1024* 1024 * 64] ;

Could you please provide sample application, build info and logs (IDE + Application output)? So that we can reproduce this issue at our end. Also please let me know if I am missing something.

You can get build info from here:
Xamarin Studio => About Xamarin Studio => Show Details => Copy Info

You can get IDE logs from here:
Xamarin Studio => Help => Open log directory (attached the latest timestamp lDE logs )

You can get application output from here:
Xamarin Studio => View => Pad => Application Output
Comment 2 Neil 2015-01-09 08:36:18 UTC
Thanks for your reply.

If I understand correctly, you reproduced the "OutOfMemoryException", right?


Just double click the button in the designer,  the xamarin ide will guide you to handle the HandleCompressTouched event.

Then copy my code above, into the event.

This issue only happens in real device, not in simulator. I tested in my iPhone6.
Comment 3 Sebastien Pouliot 2015-01-09 15:12:16 UTC
That's 256MB of continuous* memory that you expect to be given on the spot (out of a maximum** of 1GB shared with an OS and other applications) on devices with a maximum of 1GB (RAM) and without virtual memory (for data).

Expecting to get such a large buffer once is likely* (at least on recent devices) but you're unlikely*** to get it two times (even less 3). IOW the OutOfMemoryException is not surprising.

For such large buffers I suggest you to use an unmanaged buffer and manually dispose of it ASAP (a la IDisposable****) or it might be better* to reuse the buffer - at least if it's something you'll have to request often.

* memory fragmentation can become a problem (not XI specific)
** there was only 256MB RAM in the first iPad and there's still many iOS devices with 512MB;
*** you don't control the GC
**** e.g. UnmanagedMemoryStream or directly call Marshal.* API
Comment 4 Neil 2015-01-12 02:54:48 UTC
Hi Sebastien Pouliot,

Yes, it's 256MB memeory, but nobody holds the memory. Shouldn't it be released by GC automatically sometime after the click event?

Why it succeeds for the first time, but fails for the 2 or 3 time?

Does it mean the memory was not released in time?

Thanks.