How can I tell how much memory an lxc container is using?

Using htop or free inside a container gives the information for host machine resources. However I want the utilization status specific to the container. lxc info on a container id gives me the following result:-

Resources:
  Processes: 153
  Memory usage:
    Memory (current): 1.44GB
    Memory (peak): 1.52GB
  Network usage:
    eth0:
      Bytes received: 1.39MB
      Bytes sent: 7.71MB
      Packets received: 6967
      Packets sent: 7041
    lo:
      Bytes received: 1.73MB
      Bytes sent: 1.73MB
      Packets received: 3989
      Packets sent: 3989
    lxdbr0:
      Bytes received: 0B
      Bytes sent: 470B
      Packets received: 0
      Packets sent: 5

But this information is not consistent with htop on the host machine. htop tells me that each container is using 98M of memory. Even if I account for slight difference in measurement 98 MB and 1.44GB are two very different results. What is the best way to find the memory usage of a container?

Thanks,
Akshat

htop is unlikely to get the whole picture when it comes to container memory consumption. You need to look at all the processes that are running inside the container AND also account for any tmpfs filesystems or similar kernel resource usage.

You probably should consider installing “lxcfs” on your system which would then have “free” and other commands report the right values inside the container.

Thank you @stgraber I already have lxcfs installed. Do I need to do anything else to make it work? I did take a look at the reference but I’m not sure how to use it…

Is the output of lxc info (like the one I posted in the description above) reliable? Can I accept the memory stats it gives me back?

Odd, lxcfs being installed should cause /proc/meminfo and /proc/cpuinfo in the container to report container values.

lxc info shows the values reported by the kernel memory cgroup, so those should be reliable, yes.

@stgraber I’m experiencing a similar issue:

host# free -h
              total        used        free      shared  buff/cache   available
Mem:            62G        5.7G        6.4G        330M         50G         56G
Swap:           17G        139M         17G

host# lxc-info -n c1
...
Memory use:     54.21 GiB
KMem use:       2.28 GiB

host# apt list --installed lxcfs
Listing... Done
lxcfs/stable,now 2.0.7-1+deb9u1 amd64 [installed,automatic]
guest# free -h
              total        used        free      shared  buff/cache   available
Mem:            62G         54G        8.5G        330M        6.7M        8.5G
Swap:           17G        139M         17G

guest# ps -e -o pmem= | paste -sd+ | bc -l
14.0

Why does lxc-info on the host say 54.21GB, but free 5.7G? Is there a way to get accurate values inside a container even if I have to write a script, or access /proc directly? Is there anything I can check?

I have an assumption that 54GB includes buff/cache, but how do I then separate those values inside a container?

P.S. Debian Stretch, lxc-2.0.7.