Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a block of memory be partially freed?

I'm writing a very memory intensive program that will have dozens of malloc'd arrays. When the app receives a low memory warning, I want to dump the lower half of each of these arrays. Is there any way to do this?

I need some way that I can preserve half of the memory in each array. Obviously, if the app has low memory, I can't allocate a smaller array, copy half of my data into it, and then free the old array. Is there any function that can free a block of memory starting at pointer A and ending at pointer B or something like that?

like image 793
Randall Avatar asked Dec 22 '25 05:12

Randall


1 Answers

Realloc() can return the trailing portion of the memory of a malloc back to the allocation pool, but can't return it to the OS.

Realloc() also won't help with memory fragmentation, which is likely a problem in a low-memory situation.

like image 124
antlersoft Avatar answered Dec 23 '25 20:12

antlersoft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!