How to test UIWebView's 10MB limit on JavaScript?

走远了吗. 提交于 2020-01-03 05:38:09

问题


I'm trying to test UIWebView's 10MB limit on Javascript memory allocation. Here is my code so far which I'm running on the simulator (iOS 5.1).

int size = 10485760+1024000*10; 
int i = 13;
char *cStr = (char *) malloc(size); // 20MB buffer
char *lcStr = cStr;
lcStr+=i;
strcpy(cStr, "window._x = \'");
for ( ; i < size-4; i++){
    if(i%2){
        *lcStr = ' ';
    }else{
        *lcStr = 'c';
    }
    lcStr++;
}
strcpy(lcStr, "c\';\0");

NSString *longStr = [NSString stringWithCString:cStr encoding:NSASCIIStringEncoding];
// by now 40MB used

//memory usage spikes to 60MB
NSString *result = [self.webView stringByEvaluatingJavaScriptFromString: longStr];

free(cStr);

HTML/JS:

<script type="text/javascript">
    setTimeout(function(){document.getElementById("one").innerText = window._x;},3000);
</script>

My problem is that the code above doesn't result in any exception being thrown by the UIWebView even though clearly the JS allocation at one point is greater than 10MB. I've used instruments to verify the memory usage.

The end result of the entire program is that I see bunch of 'c ' are printed on the screen in a webview but no crash.

Am I doing something wrong? How can I test this JS limit?


回答1:


The limits vary by device. The Simulator does not impose the same limits as actual devices. If you test this on an iPhone, you should see an exception.



来源:https://stackoverflow.com/questions/18710006/how-to-test-uiwebviews-10mb-limit-on-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!