C how to free sub memory?












0















I allocate a big memory , char* test= malloc(10000000); , then I put value on this memory , and do some work for each value.



What I want is , each 1000 index, I want to release all the memory until it.



For ex.



for(long i=0; i<10000000;i++)
DoSomeWork(test[i]);
if(i%1000==0)
releaseMemory(i-1000,i);


How can I do it in c?



I know that free can only free all of my allocate, but I don't want to wait to the end of work the free all the memory.



I want each 1000 works free all the 1000 back



I must to allocate all the memory in the begining of program.










share|improve this question























  • If you're allocating memory once up front, what do you think it even means to "free" it inside the loop? What is happening inside your DoSomeWork() function that you think you need to undo periodically?

    – Lee Daniel Crocker
    Nov 13 '18 at 19:00











  • You need to write your own memory allocator for what you want. Work in reverse order and call brk() and friends when needed.

    – Leonardo Herrera
    Nov 13 '18 at 19:00






  • 2





    There are a couple of ways to do this. One is to continue allocating one large block at the system level, but use your own mechanism to control which of your objects gets to use which bit of your memory. For example, you might have an array of flags indicating which chunks of memory are in use at any given time. The other way is to break up your allocation into smaller pieces, freeing those (and potentially re-allocating them) when appropriate.

    – Tim Randall
    Nov 13 '18 at 19:28
















0















I allocate a big memory , char* test= malloc(10000000); , then I put value on this memory , and do some work for each value.



What I want is , each 1000 index, I want to release all the memory until it.



For ex.



for(long i=0; i<10000000;i++)
DoSomeWork(test[i]);
if(i%1000==0)
releaseMemory(i-1000,i);


How can I do it in c?



I know that free can only free all of my allocate, but I don't want to wait to the end of work the free all the memory.



I want each 1000 works free all the 1000 back



I must to allocate all the memory in the begining of program.










share|improve this question























  • If you're allocating memory once up front, what do you think it even means to "free" it inside the loop? What is happening inside your DoSomeWork() function that you think you need to undo periodically?

    – Lee Daniel Crocker
    Nov 13 '18 at 19:00











  • You need to write your own memory allocator for what you want. Work in reverse order and call brk() and friends when needed.

    – Leonardo Herrera
    Nov 13 '18 at 19:00






  • 2





    There are a couple of ways to do this. One is to continue allocating one large block at the system level, but use your own mechanism to control which of your objects gets to use which bit of your memory. For example, you might have an array of flags indicating which chunks of memory are in use at any given time. The other way is to break up your allocation into smaller pieces, freeing those (and potentially re-allocating them) when appropriate.

    – Tim Randall
    Nov 13 '18 at 19:28














0












0








0








I allocate a big memory , char* test= malloc(10000000); , then I put value on this memory , and do some work for each value.



What I want is , each 1000 index, I want to release all the memory until it.



For ex.



for(long i=0; i<10000000;i++)
DoSomeWork(test[i]);
if(i%1000==0)
releaseMemory(i-1000,i);


How can I do it in c?



I know that free can only free all of my allocate, but I don't want to wait to the end of work the free all the memory.



I want each 1000 works free all the 1000 back



I must to allocate all the memory in the begining of program.










share|improve this question














I allocate a big memory , char* test= malloc(10000000); , then I put value on this memory , and do some work for each value.



What I want is , each 1000 index, I want to release all the memory until it.



For ex.



for(long i=0; i<10000000;i++)
DoSomeWork(test[i]);
if(i%1000==0)
releaseMemory(i-1000,i);


How can I do it in c?



I know that free can only free all of my allocate, but I don't want to wait to the end of work the free all the memory.



I want each 1000 works free all the 1000 back



I must to allocate all the memory in the begining of program.







c memory malloc free






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 18:53









Voiilp2Voiilp2

11




11













  • If you're allocating memory once up front, what do you think it even means to "free" it inside the loop? What is happening inside your DoSomeWork() function that you think you need to undo periodically?

    – Lee Daniel Crocker
    Nov 13 '18 at 19:00











  • You need to write your own memory allocator for what you want. Work in reverse order and call brk() and friends when needed.

    – Leonardo Herrera
    Nov 13 '18 at 19:00






  • 2





    There are a couple of ways to do this. One is to continue allocating one large block at the system level, but use your own mechanism to control which of your objects gets to use which bit of your memory. For example, you might have an array of flags indicating which chunks of memory are in use at any given time. The other way is to break up your allocation into smaller pieces, freeing those (and potentially re-allocating them) when appropriate.

    – Tim Randall
    Nov 13 '18 at 19:28



















  • If you're allocating memory once up front, what do you think it even means to "free" it inside the loop? What is happening inside your DoSomeWork() function that you think you need to undo periodically?

    – Lee Daniel Crocker
    Nov 13 '18 at 19:00











  • You need to write your own memory allocator for what you want. Work in reverse order and call brk() and friends when needed.

    – Leonardo Herrera
    Nov 13 '18 at 19:00






  • 2





    There are a couple of ways to do this. One is to continue allocating one large block at the system level, but use your own mechanism to control which of your objects gets to use which bit of your memory. For example, you might have an array of flags indicating which chunks of memory are in use at any given time. The other way is to break up your allocation into smaller pieces, freeing those (and potentially re-allocating them) when appropriate.

    – Tim Randall
    Nov 13 '18 at 19:28

















If you're allocating memory once up front, what do you think it even means to "free" it inside the loop? What is happening inside your DoSomeWork() function that you think you need to undo periodically?

– Lee Daniel Crocker
Nov 13 '18 at 19:00





If you're allocating memory once up front, what do you think it even means to "free" it inside the loop? What is happening inside your DoSomeWork() function that you think you need to undo periodically?

– Lee Daniel Crocker
Nov 13 '18 at 19:00













You need to write your own memory allocator for what you want. Work in reverse order and call brk() and friends when needed.

– Leonardo Herrera
Nov 13 '18 at 19:00





You need to write your own memory allocator for what you want. Work in reverse order and call brk() and friends when needed.

– Leonardo Herrera
Nov 13 '18 at 19:00




2




2





There are a couple of ways to do this. One is to continue allocating one large block at the system level, but use your own mechanism to control which of your objects gets to use which bit of your memory. For example, you might have an array of flags indicating which chunks of memory are in use at any given time. The other way is to break up your allocation into smaller pieces, freeing those (and potentially re-allocating them) when appropriate.

– Tim Randall
Nov 13 '18 at 19:28





There are a couple of ways to do this. One is to continue allocating one large block at the system level, but use your own mechanism to control which of your objects gets to use which bit of your memory. For example, you might have an array of flags indicating which chunks of memory are in use at any given time. The other way is to break up your allocation into smaller pieces, freeing those (and potentially re-allocating them) when appropriate.

– Tim Randall
Nov 13 '18 at 19:28












1 Answer
1






active

oldest

votes


















2














What you want can be achieved by allocating the program in smaller chunks.



You have to adjust your algorithm to handle a bunch of small sub-arrays which you then can release after use.



In this case, it might be useful to allocate the chunks in reversed direction to give the libc the chance to release the freed memory to the underlying OS.



Let me enhance a bit here:



Assume you want an array with 10000000 (10 million) entries. Instead of allocating it as one chunk as depicted in the question, it could be possible to have



#define CHUNKSIZE 10000
#define ENTRYSIZE 8
#define NUM_CHUNKS 1000

void test(void)
{
void** outer_array = malloc(NUM_CHUNKS * sizeof(void*))
for (int i = 0; i < NUM_CHUNKS; i++) {
void * chunk = malloc(CHUNKSIZE * ENTRYSIZE);
outer_array[NUM_CHUNKS - 1 - i] = chunk;
// allocate them in reverse order
}

// now, set item #123456
size_t item_index = 123456;
// TODO check if the index is below the maximum
size_t chunk_index = item_index / CHUNKSIZE;
size_t index_into_chunk = item_index % CHUNKSIZE;
void * item_address = &outer_array[chunk_index][index_into_chunk * ENTRY_SIZE];

// after having processed one chunk, you can free it:
free(outer_array[0]);
outer_array[0] = NULL;
}


There are (roughly) two possibilities how a program can enhance the heap in order to allocate memory:




  1. It can obtain a completely new memory block from the OS, indepedent from the "main address space". Then it can use it for allocation and return it to the OS as soon as it is free()d. This happens in some allocators if the allocation size is above a certain threshold.

  2. It can enhance the program address space. Then, the new memory is added at the end. After free()ing the last memory block, the program address space can be reduced again. This happens in some allocators if the allocation size is below a certain threshold.


This way, your program's memory footprint decreases over time.






share|improve this answer


























  • one can also use realloc with a smaller size. Not sure it frees a lot, though

    – Jean-François Fabre
    Nov 13 '18 at 18:58






  • 1





    Also keep in mind that just because you free memory doesn't necessarily mean it will become available to the OS right away (or even soon).

    – dbush
    Nov 13 '18 at 18:58






  • 1





    @Jean-FrançoisFabre I thought that too, but problem with realloc is that it frees from end, not start. And what he wants is from start.

    – Afshin
    Nov 13 '18 at 18:58








  • 1





    you could compute the array in reverse by 1000 chunks. it's ugly, though, lots of adds & subs & index magic. eeek

    – Jean-François Fabre
    Nov 13 '18 at 19:01













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287741%2fc-how-to-free-sub-memory%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














What you want can be achieved by allocating the program in smaller chunks.



You have to adjust your algorithm to handle a bunch of small sub-arrays which you then can release after use.



In this case, it might be useful to allocate the chunks in reversed direction to give the libc the chance to release the freed memory to the underlying OS.



Let me enhance a bit here:



Assume you want an array with 10000000 (10 million) entries. Instead of allocating it as one chunk as depicted in the question, it could be possible to have



#define CHUNKSIZE 10000
#define ENTRYSIZE 8
#define NUM_CHUNKS 1000

void test(void)
{
void** outer_array = malloc(NUM_CHUNKS * sizeof(void*))
for (int i = 0; i < NUM_CHUNKS; i++) {
void * chunk = malloc(CHUNKSIZE * ENTRYSIZE);
outer_array[NUM_CHUNKS - 1 - i] = chunk;
// allocate them in reverse order
}

// now, set item #123456
size_t item_index = 123456;
// TODO check if the index is below the maximum
size_t chunk_index = item_index / CHUNKSIZE;
size_t index_into_chunk = item_index % CHUNKSIZE;
void * item_address = &outer_array[chunk_index][index_into_chunk * ENTRY_SIZE];

// after having processed one chunk, you can free it:
free(outer_array[0]);
outer_array[0] = NULL;
}


There are (roughly) two possibilities how a program can enhance the heap in order to allocate memory:




  1. It can obtain a completely new memory block from the OS, indepedent from the "main address space". Then it can use it for allocation and return it to the OS as soon as it is free()d. This happens in some allocators if the allocation size is above a certain threshold.

  2. It can enhance the program address space. Then, the new memory is added at the end. After free()ing the last memory block, the program address space can be reduced again. This happens in some allocators if the allocation size is below a certain threshold.


This way, your program's memory footprint decreases over time.






share|improve this answer


























  • one can also use realloc with a smaller size. Not sure it frees a lot, though

    – Jean-François Fabre
    Nov 13 '18 at 18:58






  • 1





    Also keep in mind that just because you free memory doesn't necessarily mean it will become available to the OS right away (or even soon).

    – dbush
    Nov 13 '18 at 18:58






  • 1





    @Jean-FrançoisFabre I thought that too, but problem with realloc is that it frees from end, not start. And what he wants is from start.

    – Afshin
    Nov 13 '18 at 18:58








  • 1





    you could compute the array in reverse by 1000 chunks. it's ugly, though, lots of adds & subs & index magic. eeek

    – Jean-François Fabre
    Nov 13 '18 at 19:01


















2














What you want can be achieved by allocating the program in smaller chunks.



You have to adjust your algorithm to handle a bunch of small sub-arrays which you then can release after use.



In this case, it might be useful to allocate the chunks in reversed direction to give the libc the chance to release the freed memory to the underlying OS.



Let me enhance a bit here:



Assume you want an array with 10000000 (10 million) entries. Instead of allocating it as one chunk as depicted in the question, it could be possible to have



#define CHUNKSIZE 10000
#define ENTRYSIZE 8
#define NUM_CHUNKS 1000

void test(void)
{
void** outer_array = malloc(NUM_CHUNKS * sizeof(void*))
for (int i = 0; i < NUM_CHUNKS; i++) {
void * chunk = malloc(CHUNKSIZE * ENTRYSIZE);
outer_array[NUM_CHUNKS - 1 - i] = chunk;
// allocate them in reverse order
}

// now, set item #123456
size_t item_index = 123456;
// TODO check if the index is below the maximum
size_t chunk_index = item_index / CHUNKSIZE;
size_t index_into_chunk = item_index % CHUNKSIZE;
void * item_address = &outer_array[chunk_index][index_into_chunk * ENTRY_SIZE];

// after having processed one chunk, you can free it:
free(outer_array[0]);
outer_array[0] = NULL;
}


There are (roughly) two possibilities how a program can enhance the heap in order to allocate memory:




  1. It can obtain a completely new memory block from the OS, indepedent from the "main address space". Then it can use it for allocation and return it to the OS as soon as it is free()d. This happens in some allocators if the allocation size is above a certain threshold.

  2. It can enhance the program address space. Then, the new memory is added at the end. After free()ing the last memory block, the program address space can be reduced again. This happens in some allocators if the allocation size is below a certain threshold.


This way, your program's memory footprint decreases over time.






share|improve this answer


























  • one can also use realloc with a smaller size. Not sure it frees a lot, though

    – Jean-François Fabre
    Nov 13 '18 at 18:58






  • 1





    Also keep in mind that just because you free memory doesn't necessarily mean it will become available to the OS right away (or even soon).

    – dbush
    Nov 13 '18 at 18:58






  • 1





    @Jean-FrançoisFabre I thought that too, but problem with realloc is that it frees from end, not start. And what he wants is from start.

    – Afshin
    Nov 13 '18 at 18:58








  • 1





    you could compute the array in reverse by 1000 chunks. it's ugly, though, lots of adds & subs & index magic. eeek

    – Jean-François Fabre
    Nov 13 '18 at 19:01
















2












2








2







What you want can be achieved by allocating the program in smaller chunks.



You have to adjust your algorithm to handle a bunch of small sub-arrays which you then can release after use.



In this case, it might be useful to allocate the chunks in reversed direction to give the libc the chance to release the freed memory to the underlying OS.



Let me enhance a bit here:



Assume you want an array with 10000000 (10 million) entries. Instead of allocating it as one chunk as depicted in the question, it could be possible to have



#define CHUNKSIZE 10000
#define ENTRYSIZE 8
#define NUM_CHUNKS 1000

void test(void)
{
void** outer_array = malloc(NUM_CHUNKS * sizeof(void*))
for (int i = 0; i < NUM_CHUNKS; i++) {
void * chunk = malloc(CHUNKSIZE * ENTRYSIZE);
outer_array[NUM_CHUNKS - 1 - i] = chunk;
// allocate them in reverse order
}

// now, set item #123456
size_t item_index = 123456;
// TODO check if the index is below the maximum
size_t chunk_index = item_index / CHUNKSIZE;
size_t index_into_chunk = item_index % CHUNKSIZE;
void * item_address = &outer_array[chunk_index][index_into_chunk * ENTRY_SIZE];

// after having processed one chunk, you can free it:
free(outer_array[0]);
outer_array[0] = NULL;
}


There are (roughly) two possibilities how a program can enhance the heap in order to allocate memory:




  1. It can obtain a completely new memory block from the OS, indepedent from the "main address space". Then it can use it for allocation and return it to the OS as soon as it is free()d. This happens in some allocators if the allocation size is above a certain threshold.

  2. It can enhance the program address space. Then, the new memory is added at the end. After free()ing the last memory block, the program address space can be reduced again. This happens in some allocators if the allocation size is below a certain threshold.


This way, your program's memory footprint decreases over time.






share|improve this answer















What you want can be achieved by allocating the program in smaller chunks.



You have to adjust your algorithm to handle a bunch of small sub-arrays which you then can release after use.



In this case, it might be useful to allocate the chunks in reversed direction to give the libc the chance to release the freed memory to the underlying OS.



Let me enhance a bit here:



Assume you want an array with 10000000 (10 million) entries. Instead of allocating it as one chunk as depicted in the question, it could be possible to have



#define CHUNKSIZE 10000
#define ENTRYSIZE 8
#define NUM_CHUNKS 1000

void test(void)
{
void** outer_array = malloc(NUM_CHUNKS * sizeof(void*))
for (int i = 0; i < NUM_CHUNKS; i++) {
void * chunk = malloc(CHUNKSIZE * ENTRYSIZE);
outer_array[NUM_CHUNKS - 1 - i] = chunk;
// allocate them in reverse order
}

// now, set item #123456
size_t item_index = 123456;
// TODO check if the index is below the maximum
size_t chunk_index = item_index / CHUNKSIZE;
size_t index_into_chunk = item_index % CHUNKSIZE;
void * item_address = &outer_array[chunk_index][index_into_chunk * ENTRY_SIZE];

// after having processed one chunk, you can free it:
free(outer_array[0]);
outer_array[0] = NULL;
}


There are (roughly) two possibilities how a program can enhance the heap in order to allocate memory:




  1. It can obtain a completely new memory block from the OS, indepedent from the "main address space". Then it can use it for allocation and return it to the OS as soon as it is free()d. This happens in some allocators if the allocation size is above a certain threshold.

  2. It can enhance the program address space. Then, the new memory is added at the end. After free()ing the last memory block, the program address space can be reduced again. This happens in some allocators if the allocation size is below a certain threshold.


This way, your program's memory footprint decreases over time.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 9:12

























answered Nov 13 '18 at 18:56









glglglglglgl

66.3k790164




66.3k790164













  • one can also use realloc with a smaller size. Not sure it frees a lot, though

    – Jean-François Fabre
    Nov 13 '18 at 18:58






  • 1





    Also keep in mind that just because you free memory doesn't necessarily mean it will become available to the OS right away (or even soon).

    – dbush
    Nov 13 '18 at 18:58






  • 1





    @Jean-FrançoisFabre I thought that too, but problem with realloc is that it frees from end, not start. And what he wants is from start.

    – Afshin
    Nov 13 '18 at 18:58








  • 1





    you could compute the array in reverse by 1000 chunks. it's ugly, though, lots of adds & subs & index magic. eeek

    – Jean-François Fabre
    Nov 13 '18 at 19:01





















  • one can also use realloc with a smaller size. Not sure it frees a lot, though

    – Jean-François Fabre
    Nov 13 '18 at 18:58






  • 1





    Also keep in mind that just because you free memory doesn't necessarily mean it will become available to the OS right away (or even soon).

    – dbush
    Nov 13 '18 at 18:58






  • 1





    @Jean-FrançoisFabre I thought that too, but problem with realloc is that it frees from end, not start. And what he wants is from start.

    – Afshin
    Nov 13 '18 at 18:58








  • 1





    you could compute the array in reverse by 1000 chunks. it's ugly, though, lots of adds & subs & index magic. eeek

    – Jean-François Fabre
    Nov 13 '18 at 19:01



















one can also use realloc with a smaller size. Not sure it frees a lot, though

– Jean-François Fabre
Nov 13 '18 at 18:58





one can also use realloc with a smaller size. Not sure it frees a lot, though

– Jean-François Fabre
Nov 13 '18 at 18:58




1




1





Also keep in mind that just because you free memory doesn't necessarily mean it will become available to the OS right away (or even soon).

– dbush
Nov 13 '18 at 18:58





Also keep in mind that just because you free memory doesn't necessarily mean it will become available to the OS right away (or even soon).

– dbush
Nov 13 '18 at 18:58




1




1





@Jean-FrançoisFabre I thought that too, but problem with realloc is that it frees from end, not start. And what he wants is from start.

– Afshin
Nov 13 '18 at 18:58







@Jean-FrançoisFabre I thought that too, but problem with realloc is that it frees from end, not start. And what he wants is from start.

– Afshin
Nov 13 '18 at 18:58






1




1





you could compute the array in reverse by 1000 chunks. it's ugly, though, lots of adds & subs & index magic. eeek

– Jean-François Fabre
Nov 13 '18 at 19:01







you could compute the array in reverse by 1000 chunks. it's ugly, though, lots of adds & subs & index magic. eeek

– Jean-François Fabre
Nov 13 '18 at 19:01




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287741%2fc-how-to-free-sub-memory%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Jo Brand

Thiostrepton