https://github.com/danesz/sidewalkexample
Sample code for WebSocket-based Javascript execution in WKWebview via Sidewalk Swift SDK.
https://github.com/danesz/sidewalkexample
javascript swift webview wkwebview
Last synced: about 2 months ago
JSON representation
Sample code for WebSocket-based Javascript execution in WKWebview via Sidewalk Swift SDK.
- Host: GitHub
- URL: https://github.com/danesz/sidewalkexample
- Owner: Danesz
- License: mit
- Created: 2021-11-14T21:11:52.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-11-14T22:39:30.000Z (over 4 years ago)
- Last Synced: 2025-10-25T19:51:52.459Z (8 months ago)
- Topics: javascript, swift, webview, wkwebview
- Language: Swift
- Homepage:
- Size: 45.4 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SidewalkExample
Sample code for WebSocket-based Javascript execution in WKWebview via [Sidewalk Swift SDK](https://github.com/Danesz/Sidewalk).
## The project
The project showcases a workaround for a [memory leak in WKWebView](https://bugs.webkit.org/show_bug.cgi?id=215729) when using `evaluateJavaScript`
The webview keeps reference to the script that was executed via `evaluateJavaScript` even after the execution was finished.
In this example we send 1 MB strings in every 0,5 sec to the webview and ask to print the length of it.
## The results
The script executions and their effect on the memory were investigated on a jailbroken iPhone with `htop`, `top` and `Xcode`
### WKWebview.evaluateJavasSript(..)
Once the WKWebview was initialized, a new `com.apple.WebKit.WebContent` process appeared on the process list.
The memory usage of this process started to climb up rapidly, above **500+ MB**, more than 25% of the available memory of the device.
(Sometimes, being on this level for a longer period, the webview seemed like it crashed. It got "refreshed" and loaded an empty `about:blank` page.)

### sidewalkJavaScript(...)
By using the Sidewalk-way, the memory usage was around **87 MB**.

### Conclusion
It seems for a Javascript-heavy WKWebview-based application the WebSocket approach is more suitable.
After destorying the webview the memory will be freed, so continous re-creation is also an option when using `evaluateJavaScript`. If the goal is to have a long-living webview, some care is needed around Javascript execution. Here `Sidewalk` can come handy.
## Disclaimer
SidewalkExample and the Sidewalk projects are in a proof-of-concept phase, without any warranties, for educational purposes only.
The author will not be liable for any misinterpretation of data and damages you may suffer in connection with using, modifying, or distributing the projects.