Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for excessive memory use caused by the receipt of an unusually large message #289

Merged
merged 1 commit into from
Dec 30, 2016

Conversation

robinmj
Copy link

@robinmj robinmj commented Dec 3, 2016

The attached file contains a simple app demonstrating the problem. Before running the app, start the server from the terminal:

cd OutOfMemoryExample/server/
./run.sh

I've never encountered this crash with iOS 8 or 9; it seems to be iOS 10-specific.
OutOfMemoryExample.zip

@acmacalister
Copy link
Collaborator

Thanks for the PR @robinmj. Does this only occur on device? The example works fine on the simulator?

@robinmj
Copy link
Author

robinmj commented Dec 4, 2016

It fails 100% of the time for me in the iPad Retina simulator running iOS 10.1
I get a SIGABRT on line 673 of WebSocket.swift with the following output. This only happens about 70% of the time on a device (iPad 4).

applicationDidBecomeActive
websocket connecting...
2016-12-03 17:50:13.436074 OutOfMemoryExample[34506:4374233] [] nw_host_stats_add_src recv too small, received 24, expected 28
2016-12-03 17:50:13.436487 OutOfMemoryExample[34506:4374233] [] nw_host_stats_add_src recv too small, received 24, expected 28
2016-12-03 17:50:13.439159 OutOfMemoryExample[34506:4374234] [] nw_host_stats_add_src recv too small, received 24, expected 28
websocket is connected
2016-12-03 17:50:14.091 OutOfMemoryExample[34506:4374236] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** NSAllocateMemoryPages(2994176) failed'
*** First throw call stack:
(
0 CoreFoundation 0x031ba212 __exceptionPreprocess + 194
1 libobjc.A.dylib 0x006afe66 objc_exception_throw + 52
2 CoreFoundation 0x0322bf15 +[NSException raise:format:] + 133
3 Foundation 0x002ae827 NSAllocateMemoryPages + 83
4 Foundation 0x001eb6d7 _NSDataCreateVMDispatchData + 37
5 Foundation 0x001e1a1f -[NSPlaceholderData initWithBytes:length:copy:deallocator:] + 217
6 Foundation 0x001e193e -[NSData(NSData) initWithBytes:length:copy:freeWhenDone:bytesAreVM:] + 88
7 Foundation 0x001e1d2c -[NSData(NSData) initWithBytes:length:] + 41
8 libswiftFoundation.dylib 0x02864fbf TFV10Foundation4DataCfT5bytesSV5countSi_S0 + 79
9 Starscream 0x00176d78 TFC10Starscream9WebSocketP33_E91840E7069A0D2D302D042308E9D92820processOneRawMessagefT8inBufferGSRVs5UInt8__GSRS1
+ 10184
10 Starscream 0x00179fab TFC10Starscream9WebSocketP33_E91840E7069A0D2D302D042308E9D92826processRawMessagesInBufferfTGSPVs5UInt8_9bufferLenSi_T + 91
11 Starscream 0x00172d57 TFC10Starscream9WebSocketP33_E91840E7069A0D2D302D042308E9D92812dequeueInputfT_T + 855
12 Starscream 0x00172945 TFC10Starscream9WebSocketP33_E91840E7069A0D2D302D042308E9D92818processInputStreamfT_T + 853
13 Starscream 0x00171ed3 TFC10Starscream9WebSocket6streamfTCSo6Stream6handleVS1_5Event_T + 2835
14 Starscream 0x001720bb TToFC10Starscream9WebSocket6streamfTCSo6Stream6handleVS1_5Event_T + 75
15 CoreFoundation 0x031ca48b _inputStreamCallbackFunc + 75
16 CoreFoundation 0x0317b90d _signalEventSync + 221
17 CoreFoundation 0x0317b810 ___signalEventQueue_block_invoke + 16
18 libdispatch.dylib 0x03fb03ee _dispatch_call_block_and_release + 15
19 libdispatch.dylib 0x03fdbcc3 _dispatch_client_callout + 14
20 libdispatch.dylib 0x03fb8449 _dispatch_queue_serial_drain + 1619
21 libdispatch.dylib 0x03fb8c5d _dispatch_queue_invoke + 1109
22 libdispatch.dylib 0x03fb909c _dispatch_queue_override_invoke + 497
23 libdispatch.dylib 0x03fbb639 _dispatch_root_queue_drain + 470
24 libdispatch.dylib 0x03fbb3fb _dispatch_worker_thread3 + 143
25 libsystem_pthread.dylib 0x0435625c _pthread_wqthread + 1050
26 libsystem_pthread.dylib 0x04353f56 start_wqthread + 34
)
libc++abi.dylib: terminating with uncaught exception of type NSException

@daltoniam
Copy link
Owner

Thanks! I'm going to merge this with the slight apprehension that it might create a small performance hit with the constant creating and destroying of an autoreleasepool. I certainly see the benefit on low memory devices or devices with large messages and constant streams. We will keep an eye on it and address if the need arises. Thanks again!

@daltoniam daltoniam merged commit 3cc4a60 into daltoniam:master Dec 30, 2016
@ashfurrow
Copy link

I believe we've been hitting this now, thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants