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

fix: Using too much memory #1102

Open
tbm98 opened this issue Oct 8, 2023 · 3 comments
Open

fix: Using too much memory #1102

tbm98 opened this issue Oct 8, 2023 · 3 comments
Labels
bug Something isn't working as expected

Comments

@tbm98
Copy link

tbm98 commented Oct 8, 2023

Description

It is my code

import 'dart:convert';
import 'dart:io';

import 'package:dart_frog/dart_frog.dart';
import 'package:http_parser/http_parser.dart';
import 'package:path/path.dart' as p;
import 'package:http/http.dart' as http;
import 'package:uuid/uuid.dart';

import '../main.dart';

Future<Response> onRequest(RequestContext context) async {
  try {
    final formDataRequest = await context.request.formData();
    final fileUpload = formDataRequest.files.values.first.name;
    final timestamp = Uuid().v1();
    final pathUp = p.join(
        Directory.current.path, 'imagesUp', timestamp.toString() + '.jpg');
    final pathDown = p.join(
        Directory.current.path, 'imagesDown', timestamp.toString() + '.jpg');
    final fbytes = await formDataRequest.files.values.first.readAsBytes();

    await File(pathUp).writeAsBytes(
      fbytes,
      flush: true,
    );

    print('upfile-ok');

    return Response.bytes(body: []);
  } catch (e, s) {
    print('error- $e - $s');
    return Response(statusCode: 500, body: e.toString() + s.toString());
  }
}

it performs the action of saving the image to the hard drive.

With 100 consecutive requests, each request uploading a 2.5MB image, the server will very quickly reach a RAM usage of about 4GB.

If the number of requests is 500 consecutive requests, the RAM usage will be more than 10GB.

@tbm98 tbm98 added the bug Something isn't working as expected label Oct 8, 2023
@renancaraujo
Copy link
Contributor

Hello @tbm98 ,

Do you experience this on dev server or prod server? Or both?

@tbm98
Copy link
Author

tbm98 commented Oct 10, 2023

@renancaraujo I just run by dart_frog dev

@fit-jose
Copy link

@tbm98 what happens if you dont run the dev server?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working as expected
Projects
Status: Backlog
Development

No branches or pull requests

3 participants