Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Can Copy Collectors outperform malloc/free?

Name: Anonymous 2011-09-16 10:36

So I've heard a couple arguments for this and I'd like to see what you think.

The allocation procedure under copying collectors is most definitely faster than malloc, as malloc has to search through fragments of memory for a sufficiently large block to reserve, whereas allocation under a CC is as simple as incrementing the free heap pointer, much like allocating a stack frame.

When the garbage collector is triggered, it must search through the live set of objects and copy each one over to the new heap. This takes time, but it is limited to the objects in the lives set, so as long as that is reasonably small, this shouldn't take very long. Also, no operation at all is needed on the objects that are no longer referenced. Under malloc and free, free must be called on each and every single one of these objects, the moment they become dead to the program.

I could see garbage collection performing poorly in comparison to malloc and free when the live set of objects is very large and many objects stay live for a very long time. The long living objects would require minimal time for management using malloc and free, whereas the garbage collector may traverse them many times. I suppose this is were using a generational copying collector can come in handy, however. So /prog/, what do think?

Name: Anonymous 2011-09-16 10:45

I am no expert, but this doesn't sound like something for opinions, but rather benchmarks.

Name: Anonymous 2011-09-16 10:48

>>2

Yeah. I'm sure it varies for implementations, and even types of use as well.

Name: FrozenVoid 2011-09-16 10:55

i prefer static allocation. Dynamic is just wasteful and inelegant.



orbis terrarum delenda est

Name: Anonymous 2011-09-16 12:37

>>4

gbt asm noob


>>1

copy collectors have a tendency to thrash up the cache something awful, especially the longer it has been running.

Name: Anonymous 2011-09-16 13:31

>>5

copy collectors have a tendency to thrash up the cache something awful, especially the longer it has been running.

yeah, I could see its toll when doing the depth first search through the live set of objects in the heap, but probably gives the program better locality of reference otherwise due to the heap being so compact. If I recall correctly, malloc uses different sections of the heap for large objects and small ones, to reduce fragmentation, so it intentionally puts distance between the objects.

Name: Anonymous 2011-09-16 13:34

>>6

err nevermind, as the heap starts to fill with objects, the objects in the live set could get quite far from each other. And I'm sure moving every live object on the heap to a totally new region of memory isn't so great for the cache.

Name: Anonymous 2011-09-16 14:26

GC is shit.

Name: Anonymous 2011-09-16 16:02

>>5
copy collectors have a tendency to thrash up the cache something awful, especially the longer it has been running.

False. malloc allocates memory in essentially random places, copying GC at least ensures that temporal locality of allocations implies spatial locality of allocated memory.

yeah, I could see its toll when doing the depth first search through the live set of objects in the heap,

They don't do it. They use memory barriers (not the instruction reordering kind) to mark the older-generation objects that had their fields assigned. And a lot of clever tricks, I presume, to make the search for live objects over the generation being collected as linear as possible.

I am no expert, but this doesn't sound like something for opinions, but rather benchmarks.

Well, it's quite easy to make a benchmark that allocates and deallocates a lot of objects of varying sizes. I did that once, .NET code turned out to be about 50 times faster than C code using the default msvcrt malloc. Making a benchmark that is at least somewhat related to anything you might encounter in a real-world task is much harder, of course.

Name: Anonymous 2011-09-16 16:19

>>9
Well, it's quite easy to make a benchmark that allocates and deallocates a lot of objects of varying sizes. I did that once, .NET code turned out to be about 50 times faster than C code using the default msvcrt malloc.
When you need to do that, the right thing is to use a dynamic array. If implemented properly, that solution should ridiculize the GC.

Name: Anonymous 2011-09-16 16:44

The thing maybe some of you are overlooking is that many C and C++ programmers who are concerned about memory allocation performance and fragmentation will often use specialized allocators, and malloc to handle the edge-cases.

You can implement arena allocators which just increment a pointer on allocation, and then free everything at once at a predetermined time, like when unloading a module, file or dataset. Way faster than GC and it's not hard to do.

Name: Anonymous 2011-09-16 17:06

We've had this thread at least 5 times. Fuck you all.

Name: Anonymous 2011-09-16 17:56

>>12
GC proponents always come back trying to find situations where GC is useful. They don't seem to realize the obvious truth: GC is shit.

Name: Anonymous 2011-09-16 18:04

>>13
You are >>1 and IHBT.

Name: Me 2011-09-16 18:29

>>14
I am not >>1.

Name: Anonymous 2011-09-16 18:31

>>15
That's funny, because you two seem equal in terms of intelligence (or rather, idiocy).

Name: Me 2011-09-16 18:34

>>16
Are you You or The Sussman trolling us?

Name: Anonymous 2011-09-16 18:59

>>17
Neither. Now go back to your little trollwar with >>1 and leave me alone.

Name: Anonymous 2011-09-16 23:41

The problem with GC is also that it consumes more memory than needed, not only that it's slower.

Name: Anonymous 2011-09-17 1:37

>>9

They don't do it. They use memory barriers (not the instruction reordering kind) to mark the older-generation objects that had their fields assigned. And a lot of clever tricks, I presume, to make the search for live objects over the generation being collected as linear as possible.

I think that only works when every data type on the heap is immutable. That's a very interesting technique though.

Name: Anonymous 2011-09-17 1:48

>>11

You can implement arena allocators which just increment a pointer on allocation, and then free everything at once at a predetermined time, like when unloading a module, file or dataset. Way faster than GC and it's not hard to do.

That's true, although freeing individual slots in the array can get a little hairy, although it can be done efficiently there too. One solution would be to store an array stack of free indecies in that array, I believe.

Name: Anonymous 2011-09-17 3:20

Name: n3n7i 2011-09-17 10:44

... malloc-ing is easy though =)
... Once I malloc-ed so hard that I came =)

.... heeheeheehee ... =)

Don't change these.
Name: Email:
Entire Thread Thread List