Repository: flutter
Issue: [Android] App closes when rendering large text "Scudo OOM: The process has exhausted 256M for size class ...."
Labels: platform-android,engine,dependency: skia,a: typography,has reproducible steps,P2,found in release: 3.0,found in release: 3.1,team-android,triaged-android

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill out the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

1. Load a text file of at least 3 megabytes.
2. Convert to String
3. Execute textPainter.layout()
4. Execute textPainter.computeLineMetrics()
5. do something

**Expected results:** <!-- what did you want to see? -->

A problem occurs in steps 3 and 4.
It should go to the next step, but it is either fatal or lost connection to device.

**Actual results:** <!-- what did you see? -->

```
I/scudo   (15287): Scudo OOM: The process has exhausted 256M for size class 2096.
D/skia    (15287): ASSERT: 319.078125 != 315.953125
D/skia    (15287): ASSERT: 342.828125 != 339.703125
D/skia    (15287): ASSERT: 338.312500 != 335.187500
D/skia    (15287): ASSERT: 338.625000 != 335.500000
D/skia    (15287): ASSERT: 336.625000 != 333.500000
D/skia    (15287): ASSERT: 342.937500 != 339.812500
F/libc    (15287): Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xb400007060617758 in tid 15452 (2.ui), pid 15287 (text_view_draft)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'google/sdk_gphone64_arm64/emu64a:13/TPB4.220624.004/8808248:userdebug/dev-keys'
Revision: '0'
ABI: 'arm64'
Timestamp: 2022-08-14 21:14:57.390234873+0900
Process uptime: 88s
Cmdline: io.ohjongsung.text_view_draft
pid: 15287, tid: 15452, name: 2.ui  >>> io.ohjongsung.text_view_draft <<<
uid: 10157
tagged_addr_ctrl: 0000000000000001 (PR_TAGGED_ADDR_ENABLE)
pac_enabled_keys: 000000000000000f (PR_PAC_APIAKEY, PR_PAC_APIBKEY, PR_PAC_APDAKEY, PR_PAC_APDBKEY)
signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xb400007060617758
    x0  b400006e12ffa720  x1  000000000000000a  x2  000000000beb8000  x3  0000000000000000
    x4  0000000000000000  x5  0000000000000001  x6  4025608000000000  x7  4031000000000000
    x8  b400007001057758  x9  000000005f5c0000  x10 0000000000000050  x11 0000000100000000
    x12 0000000000000006  x13 407528f5c0000000  x14 0000000000000000  x15 000000000003a6fb
    x16 0000006e77ed6030  x17 00000071a0bd4720  x18 0000006e7edc8000  x19 0000006ee00ab598
    x20 b400006e12ffa720  x21 b400006e483b7618  x22 0000006ee00ab580  x23 0000000000064adb
    x24 000000000000000a  x25 000000000beb8000  x26 b400006e3cf56c90  x27 b400006e3c29eed0
    x28 0000006ee00ab59c  x29 0100006e690adac0
    lr  0000006e77a6fce0  sp  0000006ee00ab490  pc  0000006e77a6bedc  pst 0000000040001000
backtrace:
      #00 pc 000000000193bedc  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #01 pc 000000000193fcdc  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #02 pc 0000000001940028  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #03 pc 000000000194151c  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #04 pc 00000000019402a8  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #05 pc 000000000193ed40  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #06 pc 000000000194042c  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #07 pc 000000000193cb20  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #08 pc 00000000018eae88  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #09 pc 0000000001965bdc  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #10 pc 000000000195cc94  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #11 pc 000000000195cc4c  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #12 pc 0000000001a2a7a8  /data/app/~~a4_UJUSs8lPL9J9raXA22w==/io.ohjongsung.text_view_draft-9EYFiSPxberD0yxkHtC_Mg==/lib/arm64/libflutter.so (BuildId: bdbfddd1a50329f00753bdc31442cc4f3b66b4c7)
      #13 pc 0000000000002cec  [anon:dart-codespace]
```
OR

```
I/scudo   (11767): Scudo OOM: The process has exhausted 256M for size class 7120.
Lost connection to device.
```
**Flutter Doctor -v:** 

```
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.0.4, on macOS 12.5 21G72 darwin-arm, locale ko-KR)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] IntelliJ IDEA Ultimate Edition (version 2020.3.4)
[✓] VS Code (version 1.70.0)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

• No issues found!
 ohjongsung  ~/StudioProjects/text_view_draft   master ±  flutter doctor -v                
[✓] Flutter (Channel stable, 3.0.4, on macOS 12.5 21G72 darwin-arm, locale ko-KR)
    • Flutter version 3.0.4 at /Users/ohjongsung/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 85684f9300 (6주 전), 2022-06-30 13:22:47 -0700
    • Engine revision 6ba2af10bb
    • Dart version 2.17.5
    • DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/ohjongsung/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Ultimate Edition (version 2020.3.4)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.70.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.46.0

[✓] Connected device (3 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64  • Android 13 (API 33) (emulator)
    • macOS (desktop)             • macos         • darwin-arm64   • macOS 12.5 21G72 darwin-arm
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 104.0.5112.79

[✓] HTTP Host Availability
    • All required HTTP hosts are available

```

**AVD**

RAM : 4096MB
VM heap : 1028MB 

<details>
<summary>Code sample is roughly like this</summary>

<!--
      Please create a minimal reproducible sample that shows the problem
      and attach it below between the lines with the backticks.

      To create it you can use `flutter create bug` command and update the `main.dart` file.

      Alternatively, you can use https://dartpad.dev/
      which is capable of creating and running small Flutter apps.

      Without this we will unlikely be able to progress on the issue, and because of that
      we regretfully will have to close it.
-->

```dart
import 'dart:ui';
import 'dart:io';
import 'dart:typed_data';
import 'package:charset_converter/charset_converter.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        backgroundColor: Colors.green[100],
        body: Center(
          child: MaterialButton(
            onPressed: () {
              _reproduce();
            },
            color: Colors.green,
            child: const Text(
              'Pick and open file',
              style: TextStyle(color: Colors.white),
            ),
          ),
        ),
      ),
    );
  }

  void _reproduce() async {
    final result = await FilePicker.platform.pickFiles(allowMultiple: false,
        type: FileType.custom,
        allowedExtensions: ['txt']
    );

    // if no file is picked
    if (result == null) return;

    if (result.files.isNotEmpty) {
      final file = File(result.files.first.path.toString());
      Uint8List? fileBytes = file.readAsBytesSync();
      String temp = await CharsetConverter.decode("utf-8", fileBytes);

      final textSpan = TextSpan(
        text: temp,
        style: const TextStyle(
          color: Colors.black,
          fontSize: 10,
        ),
      );
      final textPainter = TextPainter(
        text: textSpan,
        textDirection: TextDirection.ltr,
      );
      textPainter.layout(
        minWidth: 0,
        maxWidth: 360.72727272727275,
      );
      
      List<LineMetrics> lines = textPainter.computeLineMetrics();
      //double currentPageBottom = 623.2727272727273;

      for (int i = 0; i < lines.length; i++) {
        final line = lines[i];

        //do something
      }
    }
  }
}

```

</details>