[SOLVED] RAM Usage in Pure LXC Container

By “Pure LXC,” I meant LXC that uses the “lxc-*” commands and not “lxc *” as used by LXD. LXC and LXD gets used interchangeable and if I search in Google or Kagi for anything related to LXC, I get search results that talk about LXD, even though I never mentioned LXD in the list of keywords. With that out of the way…

Here is the basic configuration for my LXC container:

root@vmserver:~# cat /data/vm/lxc/vyos/config
# Template used to create this container: /usr/share/lxc/templates/lxc-download
# Parameters passed to the template:
# Template script checksum (SHA-1): 47a9fc1ce184c958ecafc88dfd5f21c06f728288
# For additional config options, please look at lxc.container.conf(5)

# Uncomment the following line to support nesting containers:
#lxc.include = /usr/share/lxc/config/nesting.conf
# (Be aware this has security implications)


# Distribution configuration
lxc.include = /usr/share/lxc/config/common.conf
lxc.arch = linux64

# Container specific configuration
lxc.rootfs.path = dir:/var/lib/lxc/vyos/rootfs
lxc.uts.name = vyos

lxc.start.auto = 1

lxc.autodev = 1
lxc.hook.autodev = sh -c "mknod -m 0666 ${LXC_ROOTFS_MOUNT}/dev/fuse c 10 229"

# Network configuration
lxc.net.0.type = veth
lxc.net.0.link = brVMNet
lxc.net.0.flags = up
lxc.net.0.hwaddr = 02:16:3e:08:75:40

lxc.net.1.type = veth
lxc.net.1.link = brVLabNet1
lxc.net.1.flags = up
lxc.net.1.hwaddr = 02:09:3f:04:35:70

Once I execute lxc-attach vyos and run the free -m command, I get this:

vyos@vyos:~$ free -m
               total        used        free      shared  buff/cache   available
Mem:           46068        4222         762           8       41659       41846
Swap:           8191           0        8191

Note that the used column shows the entire host memory usage and not the container memory usage. Is there a way to show only the memory usage for the container?

Does “ lxc-info -n vyos” work?

Output:

Name:           vyos
State:          RUNNING
PID:            1626873
IP:             172.20.100.1
IP:             172.20.30.64
Link:           veths4ZDbn
 TX bytes:      916 bytes
 RX bytes:      451.27 KiB
 Total bytes:   452.16 KiB
Link:           vethfkuYyg
 TX bytes:      916 bytes
 RX bytes:      2.35 KiB
 Total bytes:   3.24 KiB

Only IP addresses and network stats.

https://man7.org/linux/man-pages/man1/lxc-info.1.html

I looked at the list of switches but I did not look at the man page.

performance reasons the kernel does not account kernel memory use unless a kernel memory limit is set.

So it looks like I need to set a memory limit before I can see how much my container uses. Well, guess I’ll have to do a lot more searching on my own.

Thanks.

I got it! lxcfs is needed to emulate a couple of files in /proc which will allow me to see available memory!

I installed lxcfs, started the service (I’m in Fedora Server), and in my container, what I get is this:

root@vmserver:~# lxc-attach vyos
root@vyos:~# free -m
               total        used        free      shared  buff/cache   available
Mem:           46068         140       45925           0           1       45927
Swap:              0           0           0

So now I only see how much memory that VyOS is using and that is awesome! :grinning:

With that, my problem is solved!

Source:

1 Like

Out of curiosity, does the cgroup top command work with lxc? On the host run; systemd-cgtop

It’s been years since I used lxc and I just can’t remember if this works. Might be another way to find this info.

systemd-cgtop is for showing slices and services in real-time. I had to stop the “animation” by pressing Ctrl+C because it’s preventing me from reading what’s going one due to my visual impairment.

Anyway, the problem is now solved thanks to installing and starting lxcfs.

You can run it once and specify the machine. For my unifi container I run; systemd-cgtop -1 -M wifi

If you just want to quickly check the container resources usage this requires far less work. But to each his own, glad you solved this problem.