Is the memory allocated by kmalloc () ever freed automatically?

I am writing a device driver that, among other things, allocates a block of memory with kmalloc. This memory is freed when the user program closes the file. In one of my experiments, the user program crashed without closing the file.

Will anything free this memory?

In another experiment, I moved kfree()from function close()to function module_exit(). When I ran the user program twice, I called again kmallocwith the same pointer as before, without first freeing it. Thus, I have lost the pointer to this memory and cannot release it.

Is this memory lost for the system until I reboot, or is it freed when the driver unloads?

+5
source share
2 answers

The memory core is never freed automatically. This includes kmalloc.

All memory associated with the open file descriptor should be released when the file is closed.
When the process terminates, for some reason (including kill -9), all open file descriptors are closed and the driver close function is called. Therefore, if you are free there, nothing that the process can do will make the memory remain after the process dies.

+8
source

Please do not associate your space with kernel programming.

What I mean?

, , , .

, , kmalloc, , ​​ , , , , .

, : kmalloc kfree, , .

+3

All Articles