This is the section of the forum for FreeBSD internal development. I presume you are asking about how to develop a kernel module? In that case, start with the man page for kldload and kldunload. There are guides for how to develop device drivers in the handbook, and lots of online tutorials for how to develop kernel modules.
If this is for a userspace program, the mechanism is called DLL; there must be lots of information on the web, as this is a fairly standard problem. And in that case, your question is in the wrong section of the forum, but that's easy to fix.
Before you go down the path of implementing DLLs, think about easier ways to solve the problem. If you have swap space, then unused memory will simply be pushed to swap; in that case, unloading the DLL is not even necessary. And instead of dynamically linking into your program, consider splitting the work into two programs which then communicate via some IPC (inter-process communication) mechanism, which could be as easy as RPCs. Matter-of-fact, using RPCs has the advantage that in the future, you can move part of the program to another node (another computer), allowing you to use more memory, and scaling better.