islamdidarmd / flutter_adblocker_webview

A webview implementation of in Flutter that blocks most of the ads that appear inside of the webpages
https://pub.dev/packages/adblocker_webview
BSD 3-Clause "New" or "Revised" License
10 stars 7 forks source link

java.lang.OutOfMemoryError when loading a large DNS Block List. #24

Open jpolstre opened 2 months ago

jpolstre commented 2 months ago

I'm trying to use this list: https://raw.githubusercontent.com/hagezi/dns-blocklists/main/domains/pro.txt Modify AdBlockerRepositoryImpl to avoid comments:

....
final response = await _getDataWithCache(_url);
 final results = <Host>[];
 for (final e in LineSplitter.split(response)) { 
    if (!e.startsWith('#')) { 
     final host = Host(authority: e);
     results.add(host);
    } 
 } 
return results;

The list loads, but when I try to use it to block ads, the app closes and I receive:

W/flutter.example( 6170): Throwing OutOfMemoryError "Failed to allocate a 16 byte allocation with 7704 free bytes and 7632B until OOM, target footprint 201326592, growth limit 201326592; giving up on allocation because <1% of heap free after GC." (VmSize 18446416 kB) E/AndroidRuntime( 6170): FATAL EXCEPTION: main E/AndroidRuntime( 6170): Process: com.islamdidarmd.example.adblockerwebview.flutter.example, PID: 6170 E/AndroidRuntime( 6170): java.lang .OutOfMemoryError: Failed to allocate a 16 byte allocation with 7704 free bytes and 7632B until OOM, target footprint 201326592, growth limit 201326592; giving up on allocation because <1% of heap free after GC.
E/AndroidRuntime( 6170): at io.flutter.plugin.common.StandardMessageCodec.readValueOfType(StandardMessageCodec.java:381) E/AndroidRuntime( 6170): at io.flutter.plugin.common.StandardMessageCodec.readValue(StandardMessageCodec.java: 340) E/AndroidRuntime( 6170): at io.flutter.plugin.common.StandardMessageCodec.readValueOfType(StandardMessageCodec.java:434) E/AndroidRuntime( 6170): at io.flutter.plugin.common.StandardMessageCodec.readValue(StandardMessageCodec.java:340) E/AndroidRuntime( 6170): at io.flutter.plugin.common.StandardMessageCodec.readValueOfType(StandardMessageCodec.java:434) E/AndroidRuntime( 6170) : at io.flutter.plugin.common.StandardMessageCodec.readValue(StandardMessageCodec.java:340) E/AndroidRuntime( 6170): at io.flutter.plugin.common.StandardMessageCodec.readValueOfType(StandardMessageCodec.java:424) E/AndroidRuntime( 6170): at io.flutter.plugin.common.StandardMessageCodec.readValue(StandardMessageCodec.java:340)
E/AndroidRuntime( 6170): at io.flutter.plugin.common.StandardMessageCodec.readValueOfType(StandardMessageCodec.java:434)

I also tried adding AndroidManifest.xml :

android:hardwareAccelerated="true"
android:largeHeap="true",

but I still get the same error, it seems flutter_inappwebview, _inAppWebViewOptions?.crossPlatform.contentBlockers, is not optimized to handle large lists.

islamdidarmd commented 2 months ago

Hi thank you for trying this out raising this issue. I will investigate more about this.

If flutter_inappwebview is indeed the one what is causing the issue, I might have to use native webview for this. Implementing own webview will take time. Meanwhile, can you provide a minimum reproducible sample?