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

Memory Leak and Instance Management Issue with Dependency Injection Library and AWS S3 #22

Closed
jeanvcastro opened this issue Aug 17, 2023 · 0 comments

Comments

@jeanvcastro
Copy link

Summary:
I'm encountering a memory leak issue in my application while utilizing the dependency injection library in conjunction with AWS S3 services. My current implementation involves creating a singleton container that maintains a single instance of the dependency injection container (DIContainer) throughout the application's lifecycle. However, this approach has led to a continuous increase in memory consumption as file uploads to S3 are performed.

Current Scenario:
The implementation of my singleton container is as follows:

class ContainerSingleton {
  private static instance: ContainerSingleton;
  private readonly container: DIContainer;

  private constructor() {
    this.container = new DIContainer();

    this.container.add({
      ...controllers,
      ...repositories,
      ...services,
      ...useCases
    });
  }

  public static getInstance(): ContainerSingleton {
    if (!ContainerSingleton.instance) {
      ContainerSingleton.instance = new ContainerSingleton();
    }

    return ContainerSingleton.instance;
  }

  public getContainer(): IDIContainer {
    return this.container as IDIContainer;
  }
}

const container = ContainerSingleton.getInstance().getContainer();

export default container;

The issue I've been facing is that the container is instantiated only at the entrypoint of the application, causing the service responsible for communicating with S3 to persist in memory even after uploads have been completed. This has resulted in a continuous increase in memory consumption, subsequently affecting my application's performance.

For instance, during my tests, I noticed that after around 100 requests to upload files of approximately 10MB each (totaling around 1GB), the AWS S3 library began to experience failures.

Current Solution:
To work around this issue, I had to resort to a suboptimal solution involving re-creating the S3 library instance every time an upload is performed. While this solution resolved the memory leak and performance problems, I recognize that it's not the cleanest approach.

Seeking Help: I'm in search of a more elegant solution to this problem. I would like to know if it's possible to elegantly recreate the service instance every time the get() method of the DIContainer is invoked. My attempt to recreate the service instance was as follows:

container.add({
   FileDriver: object(S3FileDriver) // attempted using factory, func, etc...
});

container.get("FileDriver"); // the goal is to recreate the instance here

Questions:

  1. Is there a more effective way to manage S3 service instances within the context of dependency injection to prevent memory leaks and excessive consumption?
  2. Can I recreate the instance of a specific service every time the get() method of the DIContainer is called? If so, how can I implement this properly and cleanly?

I appreciate any guidance and suggestions in advance to efficiently and elegantly resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant