dart_frog icon indicating copy to clipboard operation
dart_frog copied to clipboard

fix: Using too much memory

Open tbm98 opened this issue 2 years ago • 3 comments

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 avatar Oct 08 '23 07:10 tbm98

Hello @tbm98 ,

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

renancaraujo avatar Oct 10 '23 10:10 renancaraujo

@renancaraujo I just run by dart_frog dev

tbm98 avatar Oct 10 '23 10:10 tbm98

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

fit-jose avatar Apr 19 '24 13:04 fit-jose