Cleaning up processes memory allocation: how do you do it?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Cleaning up processes memory allocation: how do you do it?

Post by Craze Frog »

When a process is stopped, its allocated pages must be returned to the list of free pages. The question is how to do this in an efficient manner. Obviously it would be best if the malloc would free everything, but we can't depend on that for sure, as some programs will inevitable be buggy.

Considerations:
Processes may allocate sparse pages (it's not only a stack and a heap).
Processes may share pages (in which case they should only be freed when both processes ends). Leaks here will be rare, so this need not be so efficient.

What are good ways of doing this?
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: Cleaning up processes memory allocation: how do you do it?

Post by yemista »

A process can have a list of memory pages. When the process exits, trap to the kernel and go through the memory pages. Each page should have a semaphore. If that count is zero, free the page. If not, another process is sharing it so let it be and decrement the semaphore. If a process asks for memory, have the function that gives it another page also throw that page into the process's memory list. I cant really think of more efficient ways to do it. This also backs you up incase a process crashes on you some way, because so long as the kernel doesnt kill it itself, you will have all the information you need on it to remove all aspects of it.
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Re: Cleaning up processes memory allocation: how do you do it?

Post by Craze Frog »

A process can have a list of memory pages.
Yes, but it would be inefficient. I was wondering whether a linear examination of the page tables would be just as good, or if it would be absolutely disasterous. Remember, the x86 uses two-level page tables, which may give a huge advantage when exploited properly.
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Cleaning up processes memory allocation: how do you do it?

Post by JohnnyTheDon »

Scanning the page tables would be the easiest and IMHO the best because they're there already, and it prevents any memory leaks. When you're dealing with shared pages and swapfiles it gets more complex, but how you do this depends on how you've set it up.
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Re: Cleaning up processes memory allocation: how do you do it?

Post by Craze Frog »

Thanks for your opinion. Dealing with shared pages can be done easily with the extra bits of the page table entries.
xyzzy
Member
Member
Posts: 391
Joined: Wed Jul 25, 2007 8:45 am
Libera.chat IRC: aejsmith
Location: London, UK
Contact:

Re: Cleaning up processes memory allocation: how do you do it?

Post by xyzzy »

yemista wrote:A process can have a list of memory pages. When the process exits, trap to the kernel and go through the memory pages. Each page should have a semaphore. If that count is zero, free the page. If not, another process is sharing it so let it be and decrement the semaphore. If a process asks for memory, have the function that gives it another page also throw that page into the process's memory list. I cant really think of more efficient ways to do it. This also backs you up incase a process crashes on you some way, because so long as the kernel doesnt kill it itself, you will have all the information you need on it to remove all aspects of it.
This is similar to what I do. My virtual memory manager maintains a linked list of memory regions. Upon deletion of a process, it goes through each region in the process' address space, and if the region is marked as allocated, it will free any pages allocated to it (each region has a list of allocated pages). You mentioned using a semaphore here - an atomic reference count would suffice for this job: I just have a reference count in the page structure (each memory page is represented by a structure), and if it reaches 0 when the PMM's free function is called on it, it can be returned to the free page lists. This method is fairly efficient: it only looks at allocated memory regions, and freeing allocated pages for a region can be done by removing an entry from the linked list and calling the PMM's free function on it (which handles reference counting), until the page list is empty.
Post Reply