
Ask HN: Is there a difference between malloc/free and a gc - fctorial
It looks like when you&#x27;re using malloc&#x2F;free, you&#x27;re relying on the gc provided by the operating system. How do we know that this gc is better than the ones provided by runtimes(lower level?) that does gc only when the programmer asks it to?<p>Although that might expose the programs to some exploits (give input that delays gc until OOM) .
======
greenyoda
When you're using malloc, it's up to you to call free() when you want to
release memory that you know is no longer needed by your program. The only
time the operating system will free memory without being asked is when your
process exits (which, in the case of a long-running program like a web server,
could be never).

In contrast, a garbage collector periodically detects that memory is no longer
being used (e.g., an object that no existing object has pointers to), and
automatically frees it. The programmer is not required to do anything to make
this happen.

------
jki275
If you're using malloc/free, you're in a world with no gc. You are responsible
for memory management.

If you're talking about runtimes, and lower level work -- in the malloc/free
world of C/C++ there isn't anything. If you allocate memory, that memory is
allocated until you free it or your code exits. The OS isn't going to free
your memory while your process is running, at least not that I'm aware of. You
have to do it manually.

Then again, in some code it simply doesn't matter.

[https://devblogs.microsoft.com/oldnewthing/20180228-00/?p=98...](https://devblogs.microsoft.com/oldnewthing/20180228-00/?p=98125)

