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

@capacitor/filesystem leaks memory on Filesystem.writeFile #2315

Closed
nicolascotton opened this issue Feb 14, 2025 · 2 comments
Closed

@capacitor/filesystem leaks memory on Filesystem.writeFile #2315

nicolascotton opened this issue Feb 14, 2025 · 2 comments

Comments

@nicolascotton
Copy link

nicolascotton commented Feb 14, 2025

Bug Report

Plugin(s)

@capacitor/filesystem version 7.0.0

Capacitor Version

Capacitor Doctor   

Latest Dependencies:

  @capacitor/cli: 7.0.1
  @capacitor/core: 7.0.1
  @capacitor/android: 7.0.1
  @capacitor/ios: 7.0.1

Installed Dependencies:

  @capacitor/ios: not installed
  @capacitor/core: 7.0.1
  @capacitor/android: 7.0.1
  @capacitor/cli: 7.0.1

[success] Android looking great! 👌

Platform(s)

Android for sure. I'm unable to test for iOS.

Current Behavior

When a file is written, the size of the file is leaked in memory.

Expected Behavior

There should be no memory leaks when writing a file.

Code Reproduction

I can provide the following code to reproduce the issue.

import { Component } from '@angular/core';
import { Directory, Encoding, Filesystem } from '@capacitor/filesystem';
import { IonButton } from '@ionic/angular/standalone';


@Component({
  selector: 'app-root',
  template: `<ion-button (click)="repro()">Repro</ion-button>`,
  standalone: true,
  imports: [IonButton],
})
export class AppComponent {
  async repro() {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const randomIndex = Math.floor(Math.random() * chars.length);
    const char = chars[randomIndex]; // Note that the leak only occurs when the data differs.
    const data = char.repeat(1024 * 1024 * 10); // 10mb of data
    return Filesystem.writeFile({
      directory: Directory.External,
      path: 'test.txt',
      encoding: Encoding.UTF8, // Does not happen with base64 encoding.
      data
    })
  }
}

Open the dev tools and click on the repro button.
Notice the memory is climbing up every time even if we run a GC.
Image

Other Technical Details

I'm using Angular v18.
The leak only occurs when the data differs between writes & the encoding is not base64 (utf8).

Additional Context

None

@nicolascotton
Copy link
Author

It's not a capacitor issue.
Removing the data from the options after writeFile solves the issue:

import { Component } from '@angular/core';
import { Directory, Encoding, Filesystem } from '@capacitor/filesystem';
import { IonButton } from '@ionic/angular/standalone';


@Component({
  selector: 'app-root',
  template: `<ion-button (click)="repro()">Repro</ion-button>`,
  standalone: true,
  imports: [IonButton],
})
export class AppComponent {
  repro() {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const randomIndex = Math.floor(Math.random() * chars.length);
    const char = chars[randomIndex]; // Note that the leak only occurs when the data differs.
    const data = char.repeat(1024 * 1024 * 10); // 10mb of data
    const options = {
      directory: Directory.External,
      path: 'test.txt',
      data,
      encoding: Encoding.UTF8
    };
    return Filesystem.writeFile(options)
      .finally(() => (options.data as any) = null);
  }
}

Copy link

ionitron-bot bot commented Mar 6, 2025

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of the plugin, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Mar 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants