I observed kldunload cannot release memory properly. I'm using amd64 FreeBSD 13.0.
Below is my sample code and make file, can refer to Skeleton Layout of a kernel module for more information.
skeleton.c
Makefile
The ko occupies a huge space (around 10MB) when loaded in memory due to its huge bss as shown in the following picture:
freebsd-memory.sh is used to check the memory usage. This file can be downloaded using
For every time load/unload skeleton.ko, can observe the memory consumed by kernel was increased by around 10 MB (4738MB - 4728MB = 10MB) as shown below.
The Wired memory is the Memory in use by the Kernel.
A sh file is used to repeatedly load/unload the ko for 10 times, can observe the memory consumed by kernel was increased by around 100MB (5160MB - 5062MB = 98MB) as shown below.
skeleton.sh
The memory cannot be released even after days.
Is there a workaround or config to solve this problem? Thanks in advance.
Below is my sample code and make file, can refer to Skeleton Layout of a kernel module for more information.
skeleton.c
Code:
/*
* KLD Skeleton
* Inspired by Andrew Reiter's Daemonnews article
*/
#include <sys/types.h>
#include <sys/module.h>
#include <sys/systm.h> /* uprintf */
#include <sys/errno.h>
#include <sys/param.h> /* defines used in kernel.h */
#include <sys/kernel.h> /* types used in module initialization */
#define BUFFER_SIZE 10*1000*1024 // 10 MB
static char gBuffer[BUFFER_SIZE];
static int demo_init (void)
{
for (int i=0; i<BUFFER_SIZE; i++)
{
gBuffer[i] = 'A';
}
return 0;
}
static void demo_exit (void)
{
for (int i=0; i<BUFFER_SIZE; i++)
{
gBuffer[i] += 1;
}
}
/*
* Load handler that deals with the loading and unloading of a KLD.
*/
static int
skel_loader(struct module *m, int what, void *arg)
{
int err = 0;
switch (what) {
case MOD_LOAD: /* kldload */
uprintf("Skeleton KLD loaded.\n");
demo_init();
break;
case MOD_UNLOAD:
uprintf("Skeleton KLD unloaded.\n");
demo_exit();
break;
default:
err = EOPNOTSUPP;
break;
}
return(err);
}
/* Declare this module to the rest of the kernel */
static moduledata_t skel_mod = {
"skel",
skel_loader,
NULL
};
DECLARE_MODULE(skeleton, skel_mod, SI_SUB_KLD, SI_ORDER_ANY);
Makefile
Code:
SRCS=skeleton.c
KMOD=skeleton
.include <bsd.kmod.mk>
The ko occupies a huge space (around 10MB) when loaded in memory due to its huge bss as shown in the following picture:
freebsd-memory.sh is used to check the memory usage. This file can be downloaded using
fetch https://raw.githubusercontent.com/ocochard/myscripts/master/FreeBSD/freebsd-memory.sh
.For every time load/unload skeleton.ko, can observe the memory consumed by kernel was increased by around 10 MB (4738MB - 4728MB = 10MB) as shown below.
Code:
root@freebsd-13-0-n01:~/test2 # kldload -v ./skeleton.ko
Skeleton KLD loaded.
Loaded ./skeleton.ko, id=5
root@freebsd-13-0-n01:~/test2 # kldunload -v ./skeleton.ko
Unloading skeleton.ko, id=5
Skeleton KLD unloaded.
root@freebsd-13-0-n01:~/test2 #
The Wired memory is the Memory in use by the Kernel.
A sh file is used to repeatedly load/unload the ko for 10 times, can observe the memory consumed by kernel was increased by around 100MB (5160MB - 5062MB = 98MB) as shown below.
skeleton.sh
Code:
#!/bin/sh
max=10
for i in `seq 1 $max`
do
echo "-------------- $i -------------- "
kldload -v ./skeleton.ko
sleep 3
kldunload -v ./skeleton.ko
sleep 3
done
The memory cannot be released even after days.
Is there a workaround or config to solve this problem? Thanks in advance.
Last edited: