|
|
|
|
|
by bjourne
3037 days ago
|
|
It never gets as fast as stack allocating variables. Consider a loop: while(1) {
Integer n = new Integer(....) vs int n = ...
} A C compiler would allocate the n variable on the stack, making the "allocation" completely free. But in a GC:ed language, the n variable would be bump allocated once every loop. That wouldn't in itself be so costly, but every so often, a GC cycle would be needed as the garbage accumulates. Furthermore, in C the address of the n variable stays in place while in a GC:ed language it moves a bit for each loop. That is why escape analysis is a fruitful optimization. It takes heap allocated objects and attempts to stack allocate them, similar to how a C compiler would do it. |
|