sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Analyzer uses too much memory when there is many lints enabled

Open pedromassango opened this issue 4 years ago • 22 comments

The Dart Analyzer is taking 1.8110MG of RAM when I have 125 lint rules enabled in analysis_options.yaml. After removing the 125 lint rules the memory usage decreased to 450MB (max).

analysis_1

  • Analyzer
  • Dart SDK Version: the stable version before 2.8.1
  • Windows

The lint rules that I had enabled in analysis_options.yaml file are:

analysis_options.yaml
linter:
  rules:
    - always_declare_return_types
    - always_put_control_body_on_new_line
    # conflicts with the Flutter convention of putting {Key key} first and {Widget child} last
    # - always_put_required_named_parameters_first
    - always_require_non_null_named_parameters
    - annotate_overrides
    # conflicts with type_annotate_public_apis and implicit-dynamic:false
    # - avoid_annotating_with_dynamic
    # conflicts with `implicit-dynamic: false`
    #- avoid_as
    - avoid_bool_literals_in_conditional_expressions
    - avoid_catches_without_on_clauses
    - avoid_catching_errors
    - avoid_classes_with_only_static_members
    - avoid_double_and_int_checks
    - avoid_empty_else
    - avoid_field_initializers_in_const_classes
    - avoid_function_literals_in_foreach_calls
    - avoid_implementing_value_types
    - avoid_init_to_null
    - avoid_js_rounded_ints
    - avoid_null_checks_in_equality_operators
    - avoid_positional_boolean_parameters
    - avoid_private_typedef_functions
    - avoid_relative_lib_imports
    - avoid_renaming_method_parameters
    - avoid_return_types_on_setters
    - avoid_returning_null
    - avoid_returning_null_for_void
    - avoid_returning_this
    - avoid_setters_without_getters
    - avoid_single_cascade_in_expression_statements
    - avoid_slow_async_io
    - avoid_types_as_parameter_names
    - avoid_unused_constructor_parameters
    - avoid_void_async
    - await_only_futures
    - camel_case_types
    - cancel_subscriptions
    #- cascade_invocations
    - close_sinks
    - comment_references
    - constant_identifier_names
    - control_flow_in_finally
    - curly_braces_in_flow_control_structures
    - directives_ordering
    - empty_catches
    - empty_constructor_bodies
    - empty_statements
    - file_names
    #- flutter_style_todos
    - hash_and_equals
    - implementation_imports
    - invariant_booleans
    - iterable_contains_unrelated_type
    - join_return_with_assignment
    - library_names
    - library_prefixes
    - list_remove_unrelated_type
    - literal_only_boolean_expressions
    - no_adjacent_strings_in_list
    - no_duplicate_case_values
    - non_constant_identifier_names
    - null_closures
    - omit_local_variable_types
    - one_member_abstracts
    - only_throw_errors
    - overridden_fields
    - package_api_docs
    - package_names
    - package_prefixed_library_names
    - parameter_assignments
    - prefer_adjacent_string_concatenation
    - prefer_asserts_in_initializer_lists
    - prefer_collection_literals
    - prefer_conditional_assignment
    - prefer_const_constructors
    - prefer_const_constructors_in_immutables
    - prefer_const_declarations
    - prefer_const_literals_to_create_immutables
    - prefer_constructors_over_static_methods
    - prefer_contains
    - prefer_equal_for_default_values
    - prefer_expression_function_bodies
    - prefer_final_fields
    - prefer_final_locals
    - prefer_foreach
    - prefer_function_declarations_over_variables
    - prefer_generic_function_type_aliases
    - prefer_initializing_formals
    - prefer_int_literals
    - prefer_interpolation_to_compose_strings
    - prefer_is_empty
    - prefer_is_not_empty
    - prefer_iterable_whereType
    - prefer_mixin
    - prefer_single_quotes
    - prefer_typing_uninitialized_variables
    - prefer_void_to_null
    - recursive_getters
    - slash_for_doc_comments
    - sort_constructors_first
    - sort_pub_dependencies
    - sort_unnamed_constructors_first
    - test_types_in_equals
    - throw_in_finally
    - type_annotate_public_apis
    - type_init_formals
    - unawaited_futures
    - unnecessary_brace_in_string_interps
    - unnecessary_const
    - unnecessary_getters_setters
    - unnecessary_lambdas
    - unnecessary_new
    - unnecessary_null_aware_assignments
    - unnecessary_null_in_if_null_operators
    - unnecessary_overrides
    - unnecessary_parenthesis
    - unnecessary_statements
    - unnecessary_this
    - unrelated_type_equality_checks
    - use_rethrow_when_possible
    - use_string_buffers
    - use_to_and_as_if_applicable
    - valid_regexps
    - void_checks

pedromassango avatar May 06 '20 20:05 pedromassango

Same with me I encountered this bug after I upgraded to flutter 1.17.0 which has Dart 2.8.1... I have a 16GB of RAM and whenever i compile my code, the dart.exe file on task manager uses all the available RAM of my PC....Please fix this ASAP. It uses 99 percent of my memory......

I redownloaded the Flutter v1.12.13 w/c has Dart v2.7.0.....and everything backs to normal......PLEASE FIX THIS ASAP!!!!!

jeffreyconcha avatar May 09 '20 11:05 jeffreyconcha

So lets vote the issue so that it gets more atention.

pedromassango avatar May 11 '20 09:05 pedromassango

The way it happens to me is when I start typing Future< in an abstract class like when I want to use an abstract class like an interface and define many methods with return type of Future<SomeType> it just keeps happening. I recommend to do this for reproducing this bug.

I fix it by terminating dart process from task manager and starting it again. but this is really getting annoying. since i can't type Future< because it happens and I have to just copy another method and change that.

here is an example code where this bug shows itself :

@RestApi(baseUrl: APP_BASE_URL)
abstract class RestClient {
  factory RestClient(Dio dio, {String baseUrl}) = _RestClient;

  @GET('/courses')
  Future<List<Course>> getCourses();

  @GET('/trainers')
  Future<List<Trainer>> getTrainers();

  @POST('/signup')
  Future<HttpResponse<void>> signup(@Body() Map<String, dynamic> json);

  @POST('/login')
  Future<HttpResponse<User>> login(@Body() Map<String, dynamic> json);

  @POST('/users/avatar')
  Future<User> addUserAvatar(@Body() Map<String, dynamic> json);

}

note : and I don't even to run my code it just happens when I type

flutter doctor report:

$ flutter doctor -v
[√] Flutter (Channel stable, 1.22.2, on Microsoft Windows [Version 10.0.18363.959], locale en-US)
    • Flutter version 1.22.2 at C:\flutter
    • Framework revision 84f3d28555 (6 weeks ago), 2020-10-15 16:26:19 -0700
    • Engine revision b8752bbfff
    • Dart version 2.10.2

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at F:\sdk\Android.SDK
    • Platform android-30, build-tools 30.0.2
    • ANDROID_HOME = F:\sdk\Android.SDK
    • ANDROID_SDK_ROOT = F:\sdk\Android.SDK
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[√] Android Studio (version 4.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 50.0.1
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] IntelliJ IDEA Ultimate Edition (version 2019.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA 2019.3.2
    • Flutter plugin version 45.1.2
    • Dart plugin version 193.7361

[√] VS Code, 64-bit edition (version 1.50.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.16.0

[!] Connected device
    ! No devices available

! Doctor found issues in 2 categories.

here is my pubspec.yaml:

name: matjoy_app
description: MatJoy, the Best Fitness app you can find
version: 0.1.4+3


environment:
  sdk: ">=2.10.2 <3.0.0"


dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  footprint: ^1.1.6
  intl:
  intl_translation:
  flutter_bloc: ^6.1.1
  connectivity: ^2.0.2
#  shared_preferences: ^0.5.12
  sailor: ^0.7.1
  freezed_annotation: ^0.11.0+1
  json_annotation: ^3.0.1
  fluttertoast: ^7.1.5
  injectable: ^1.0.4
  get_it: ^4.0.4
  url_launcher: ^5.7.10
  shamsi_date: ^0.9.1
  tuple: ^1.0.3
  dio: ^3.0.10
  retrofit: ^1.3.4+1
  auto_size_text: ^2.1.0
  carousel_slider: ^2.3.1
  flutter_custom_clippers: ^1.1.2
  cached_network_image: ^2.3.3
  flushbar: ^1.10.4
  skeleton_text: ^1.0.0
  badges: ^1.1.4
#  explode_view: ^1.0.4
  #  permission_handler: ^5.0.1+1
  #  moor: ^3.3.1
  #  sqlite3_flutter_libs: ^0.2.0
#  confetti: ^0.5.4+1
  chewie: ^0.12.0

#  ff_navigation_bar: ^0.1.5
#  video_player: ^1.0.0
  video_thumbnail: ^0.2.4
  flutter_group_sliver: ^0.0.2
  flutter_verification_code: ^0.2.1
  persian_datepicker: ^1.3.1
  device_preview: ^0.4.8
  file_picker: ^1.9.0+1
  #  adaptive_dialog: ^0.9.0+1
  faker: ^1.3.0
  dotted_border: ^1.0.7
  hive: ^1.4.4+1
  lottie: ^0.7.0+1
  hive_flutter: ^0.3.1
  fl_chart: ^0.12.0
  path_provider:
  path:

  ################################# icons ##########################################
  cupertino_icons: ^1.0.0
  feather_icons_flutter: ^4.7.4 # https://feathericons.com/
#  ionicons: ^0.0.3 #https://ionicons.com/
#  community_material_icon: ^5.4.55 #https://materialdesignicons.com/
#  font_awesome_flutter: ^8.5.0 # https://fontawesome.com/icons
#  line_icons: ^0.2.0 # https://icons8.com/line-awesome

dev_dependencies:
  flutter_test:
    sdk: flutter

  injectable_generator: ^1.0.4
  retrofit_generator: ^1.4.0+2
  hive_generator: ^0.8.2
  #  moor_generator: ^3.3.1
  build_runner:
  mockito: ^4.1.1
  freezed: ^0.11.5
  # preview: ^0.0.8
  json_serializable: ^3.5.0
  flutter_launcher_icons: ^0.8.1
  test: ^1.15.2
  bloc_test: ^7.1.0

dependency_overrides:
  freezed_annotation: ^0.11.0+1

 #just run -> flutter pub run flutter_launcher_icons:main
flutter_icons:
   #    image_path: "assets/images/icon-128x128.png"
  image_path_android: "assets/icons/icon_legacy.png"
  image_path_ios: "assets/icons/icon_legacy.png"
  android: true # can specify file name here e.g. "ic_launcher"
  ios: true # can specify file name here e.g. "My-Launcher-Icon"
  adaptive_icon_background: "assets/icons/icon_back.png" # only available for Android 8.0 devices and above
  adaptive_icon_foreground: "assets/icons/icon_front.png" # only available for Android 8.0 devices and above


# run -> fluttergen -c example/pubspec.yaml
flutter_gen:
  output: lib/gen/ # Optional (default: lib/gen/)
  lineLength: 120 # Optional (default: 80)

#  integrations:
#    flutter_svg: true

#  colors:
#    inputs:
#      - assets/color/colors.xml


flutter:

  uses-material-design: true

  assets:
    - assets/images/app_logo.png
    - assets/images/app_logo_orange.png
    - assets/images/arm_image.png
    - assets/images/image_placeholder.png
    - assets/images/image_placeholder_small.png
    - assets/images/sidebar_logo.png
    - assets/images/trainer.png
    - assets/images/trainer_shadow.png
    - assets/images/trainee.png
    - assets/images/trainee_shadow.png
    - assets/images/logo_and_name.png
    - assets/images/login_top_shape_green.png
    - assets/images/login_top_shape_orange.png
    - assets/images/bar_chart.png
    - assets/json/logo_lottie.json
    - assets/video/logo_trailer.mp4
    - assets/video/trailer_1.mp4

  fonts:
    - family: IranYekan
      fonts:
        - asset: assets/fonts/iranyekan/IRANYekanMobileThin.ttf
          weight: 100
        - asset: assets/fonts/iranyekan/IRANYekanMobileLight.ttf
          weight: 300
        - asset: assets/fonts/iranyekan/IRANYekanMobileRegular.ttf
          weight: 400
        - asset: assets/fonts/iranyekan/IRANYekanMobileMedium.ttf
          weight: 500
        - asset: assets/fonts/iranyekan/IRANYekanMobileBold.ttf
          weight: 700
        - asset: assets/fonts/iranyekan/IRANYekanMobileExtraBold.ttf
          weight: 800
        - asset: assets/fonts/iranyekan/IRANYekanMobileBlack.ttf
          weight: 900
    - family: IranYekan-FD
      fonts:
        - asset: assets/fonts/iranyekan/fd/IRANYekanMobileThin-FD.ttf
          weight: 100
        - asset: assets/fonts/iranyekan/fd/IRANYekanMobileLight-FD.ttf
          weight: 300
        - asset: assets/fonts/iranyekan/fd/IRANYekanMobileRegular-FD.ttf
          weight: 400
        - asset: assets/fonts/iranyekan/fd/IRANYekanMobileMedium-FD.ttf
          weight: 500
        - asset: assets/fonts/iranyekan/fd/IRANYekanMobileBold-FD.ttf
          weight: 700
        - asset: assets/fonts/iranyekan/fd/IRANYekanMobileExtraBold-FD.ttf
          weight: 800
        - asset: assets/fonts/iranyekan/fd/IRANYekanMobileBlack-FD.ttf
          weight: 900
    - family: IranSans
      fonts:
        - asset: assets/fonts/iransans/IRANSansMobile_UltraLight.ttf
          weight: 100
        - asset: assets/fonts/iransans/IRANSansMobile_Light.ttf
          weight: 300
        - asset: assets/fonts/iransans/IRANSansMobile.ttf
          weight: 400
        - asset: assets/fonts/iransans/IRANSansMobile_Medium.ttf
          weight: 500
        - asset: assets/fonts/iransans/IRANSansMobile_Bold.ttf
          weight: 700
        - asset: assets/fonts/iransans/IRANSansMobile_Black.ttf
          weight: 900
    - family: IranSans-FD
      fonts:
        - asset: assets/fonts/iransans/fd/IRANSansMobile_FaNum_UltraLight.ttf
          weight: 100
        - asset: assets/fonts/iransans/fd/IRANSansMobile_FaNum_Light.ttf
          weight: 300
        - asset: assets/fonts/iransans/fd/IRANSansMobile_FaNum.ttf
          weight: 400
        - asset: assets/fonts/iransans/fd/IRANSansMobile_FaNum_Medium.ttf
          weight: 500
        - asset: assets/fonts/iransans/fd/IRANSansMobile_FaNum_Bold.ttf
          weight: 700
        - asset: assets/fonts/iransans/fd/IRANSansMobile_FaNum_Black.ttf
          weight: 900

    - family: MatjoyIcons
      fonts:
        - asset: assets/fonts/icons/MatjoyIcons.ttf
    - family: MatjoyIcons2
      fonts:
        - asset: assets/fonts/icons/MatjoyIcons2.ttf


easazade avatar Nov 30 '20 14:11 easazade

So.. is this issue responsible for my Dart process eating over 18GB of RAM? It only happens after a few hours. https://github.com/dart-lang/sdk/issues/44792

bernaferrari avatar Feb 11 '21 20:02 bernaferrari

Is this related to https://github.com/flutter/flutter/issues/90868?

bernaferrari avatar Oct 15 '21 04:10 bernaferrari

This is still a big issue. Our project with many packages consistently uses more than 10GB of ram.

simc avatar May 11 '22 11:05 simc

Sorry this is still an issue for you @leisim.

Are you using dart_code_metrics?

pq avatar May 18 '22 21:05 pq

@pq no, just the default flutter lints. The big issue is the number of packages in the workspace. With a VSCode workspace that only includes some of the packages, the ram usage can be reduced significantly. But it's certainly not ideal 😔

simc avatar May 18 '22 22:05 simc

Same issue here!

The codebase is quite big, but 15GB feels a lot.

image

aloisdeniel avatar Jul 05 '22 15:07 aloisdeniel

@pq any updates or workarounds here? This issue is getting worse and slowing down our entire team 😔

simc avatar Jul 20 '22 06:07 simc

@scheglov has been looking at element model memory usage and summary serialization but I don't know if anything conclusive has come out of it. We're also looking at leveraging some work on leak detection that @polina-c is driving but that's early yet.

TL;DR nothing substantial yet but folks are looking... thanks for your patience @leisim and sorry for the inconvenience!

pq avatar Jul 20 '22 16:07 pq

Let us know if you need a sample project to reproduce the issue

simc avatar Jul 20 '22 16:07 simc

A repro would be fantastic. Thanks @leisim!

pq avatar Jul 20 '22 18:07 pq

TIL Clickup uses Flutter

bernaferrari avatar Jul 20 '22 19:07 bernaferrari

Is it possible to disable Dart Analysis in IDE at least?

ezze avatar Jul 22 '22 10:07 ezze

If you're asking whether the tool currently supports having different analysis options in IDEs and the command-line, the answer is 'no'.

If you're asking whether it's possible to not run the analysis server in an IDE, the answer is 'yes', but you'll loose all of the language support: semantic highlighting, code completion, hovers, searches, everything.

If you're asking whether it's possible to stop running lints, both in the IDE and on the command-line, then the answer is 'yes'. You can disable lints in your local analysis options file even if they were enabled in an included options file.

bwilkerson avatar Jul 22 '22 15:07 bwilkerson

@bwilkerson Thanks for you answer. I was asking about stopping analysis in IDE but can't find the option to turn it off.

If you're asking whether it's possible to not run the analysis server in an IDE, the answer is 'yes', but you'll loose all of the language support: semantic highlighting, code completion, hovers, searches, everything.

Does it mean that I simply should disable Dart support in IDE? Seems that it's not what I want. I thought that I could turn linting off in IDE, and it would help me to save RAM on my laptop then.

ezze avatar Jul 22 '22 15:07 ezze

You can't turn linting off in the IDE without turning it off everywhere, but you can disable any (or all) of the lints that you want to through the analysis options file. See https://dart.dev/guides/language/analysis-options#disabling-individual-rules for details.

bwilkerson avatar Jul 22 '22 15:07 bwilkerson

Bad news. :( I still need to lint the source code in CI... Thanks anyway.

ezze avatar Jul 22 '22 15:07 ezze

Working in project with monorepo. Using include: package:lint/analysis_options.yaml and

plugins:
     - dart_code_metrics
image

It completely impossible to work in this case. Have to disable most of the linter rules.

vasilich6107 avatar Sep 13 '22 08:09 vasilich6107

Sorry for the continued trouble @vasilich6107. A few things of note for anyone digging into this:

  1. the lint set is: https://github.com/passsy/dart-lint/blob/master/lib/analysis_options.yaml
  2. dart_code_metrics is enabled

I'd be curious how different the memory profile looks like w/o lints enabled. (And if you've noticed any that seem to impact memory in particular, that'd be especially interesting.) I'd also expect the biggest drop if you disabled dart_code_metrics but that would be interesting to confirm. (Plugins are currently pretty memory intensive.)

Is this an open source project?

pq avatar Sep 13 '22 16:09 pq

I'd also expect the biggest drop if you disabled dart_code_metrics but that would be interesting to confirm. (Plugins are currently pretty memory intensive.)

Yeah, any chance https://github.com/flutter/flutter/issues/95092 and https://github.com/dart-lang/sdk/issues/48925 could be addressed sooner? Coz the problem is literally "by design" and not by how the plugin code is written

incendial avatar Sep 13 '22 18:09 incendial

It completely impossible to work in this case. Have to disable most of the linter rules.

I'm working on some perf improvements for a handful of lints that use a utility method that's problematic (https://github.com/dart-lang/linter/issues/3745). Hopefully this will help. In the meantime, I'd be curious to know if any lints in particular are especially impacting. One I'm particularly curious about is invariant_booleans since it's pretty slow and (needlessly) creates a lot of short-lived objects.

pq avatar Oct 04 '22 22:10 pq

image

with this repo: https://github.com/kodecocodes/rwf-materials.git 16G RAM usage

hongfeiyang avatar Jan 18 '23 03:01 hongfeiyang

@hongfeiyang are you opening the whole folder in VS Code? If yes - is this intentional, why not open a specific subfolder instead?

/cc @jensjoha

mraleph avatar Jan 18 '23 08:01 mraleph

@hongfeiyang are you opening the whole folder in VS Code? If yes - is this intentional, why not open a specific subfolder instead?

/cc @jensjoha

Oh yea, I was being lazy and should have opened a single sub directory only. Thanks for pointing that out, it only takes around 700MB RAM to analyse a single sub directory.

hongfeiyang avatar Jan 18 '23 15:01 hongfeiyang

Possibly related: https://twitter.com/OrestesGaolin/status/1623433133369737216 image

bernaferrari avatar Feb 09 '23 18:02 bernaferrari

My analysis server eats ~8GB memory (when freshly opened) and even more (have observed 16GB IIRC) after playing with IDE for a while on my own project...

So, I wonder whether this is a bug or not, and will it be solved in the future? Thanks!

image

fzyzcjy avatar Apr 11 '23 04:04 fzyzcjy

Well today it breaks the record: 19GB (for the same codebase) :(

image

fzyzcjy avatar May 19 '23 10:05 fzyzcjy

I just wanted to chime in after coming back from the long weekend and wondering what was chomping all my memory causing instability in other apps and found this little gremlin. It is not often you come across something more resource intensive than a chromium process but this process certainly wants to make itself known to be a competitor.

$ ps -e -orss=,args= | sort -nr | head
4857356 /home/loki/projects/flutter/bin/cache/dart-sdk/bin/dart language-server --client-id=Android-Studio --client-version=AI-222.4459.24 --protocol=analyzer

image

anonymousbitshifter avatar May 30 '23 17:05 anonymousbitshifter