vmorganp / Lazytainer

Docker container lazy loading
MIT License
576 stars 20 forks source link

Memory ussage issue #31

Closed heisenberg2980 closed 1 year ago

heisenberg2980 commented 1 year ago

After some time running Lazytainer the machine crashes due to no RAM memory available. After some analysis I realised Lazytainer is increasing the memory ussage overtime until it consumes all available memory. As an example, after being iddle for 10 hours it is using over 400Mb of memory:

image

No logs are generated.

I am running the latest version Lazytainer (v2.0.4) using docker-compose, this is the docker-compose.yaml:

version: "3"
services:
  lazytainer:
    container_name: lazytainer
    image: ghcr.io/vmorganp/lazytainer:master
    restart: unless-stopped
    environment:
      - VERBOSE=false # probably set this to false unless you're debugging or doing the initial demo
      - TZ=Europe/London
    ports:
      - 5800:5800
      - 4443:4443
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    labels:
      # Firefox
      - "lazytainer.group.firefox.pollRate=5"
      - "lazytainer.group.firefox.inactiveTimeout=300"
      - "lazytainer.group.firefox.ports=5800"
      - "lazytainer.group.firefox.sleepMethod=stop" # can be either "stop" or "pause", or left blank for stop
      - "lazytainer.group.firefox.minPacketThreshold=1"
      # Kasm
      - "lazytainer.group.kasm.pollRate=5"
      - "lazytainer.group.kasm.inactiveTimeout=300"
      - "lazytainer.group.kasm.ports=4443"
      - "lazytainer.group.kasm.sleepMethod=stop" # can be either "stop" or "pause", or left blank for stop
      - "lazytainer.group.kasm.minPacketThreshold=1"
d3lity commented 1 year ago

I'm sorry for the low-level code-talk..

I did some memory optimizing for code and noticed there might not need be so much creating new dockerClient on every function-call. I managed to get it work with one. I did same optimizing for other variables that I noticed got created again on different function calls. As a result CPU usage went down (I would guess the garbage collector doesn't have to do so much work) and I got memory to keep under 18MiB. I tested this with 2 additional containers (MariaDB and Portainer).

I'll keep testing code for some time to see if everything keeps working and I did not get overboard with optimizing. I also simplified and/or butchered MainLoop in groups. I guess I was scared about make-calls for calculating rxHistory. I don't know if that is necessary.

Thank you @vmorganp for authoring this great piece of work! I did previously same kind of stuff with systemd, but it seems somewhat messier way to do this from my point of view.

vmorganp commented 1 year ago

Nice @d3lity. I figured the new client thing was probably the issue but hadn't gotten around to actually doing it.

Any chance you want to create a PR with the changes you mentioned and we can get it merged in?

Thanks for the thanks!

vmorganp commented 1 year ago

Hey @Heisenberg2980 can you verify if this is still an issue after the latest fix by @d3lity?

heisenberg2980 commented 1 year ago

Memory is now stable around 15MB