0
$\begingroup$

What I need is similar to garbage collection but a bit different, consider the following code:

int* large_arr = new int [1000000];
...
int b = large_arr[0];
int c = large_arr[1] * large_arr[2];
//after here large_arr is never used
int d = b + c;
...

So whether it's possible for compilers to detect after the comment line, the variable large_arr is not used in future and we can free those memories. For me it's an important practical issue. I want to know if this kind of optimization has been studied and what's the name of that.

$\endgroup$
5
  • $\begingroup$ What is the motivation of your question ? $\endgroup$
    – user16034
    Commented Aug 3, 2023 at 8:25
  • $\begingroup$ @YvesDaoust I'm expecting that this could reduce peak memory usage. $\endgroup$
    – Peter Wu
    Commented Aug 3, 2023 at 8:41
  • $\begingroup$ That is obvious, but why ? $\endgroup$
    – user16034
    Commented Aug 3, 2023 at 9:02
  • $\begingroup$ On one hand I have some code that they created some large array but free them only at end. I'm sure some of them can be free in middle but I'm looking for an 'one-click' solution to do this, like just add "-O3" in gcc. On the other hand I guess it's natural to consider free some memory when compilers know they are not used in future. $\endgroup$
    – Peter Wu
    Commented Aug 3, 2023 at 9:19
  • $\begingroup$ This is only useful if there are several large allocations. In a virtual memory system, unused allocated memory remains "dormant" and might be swapped out. $\endgroup$
    – user16034
    Commented Aug 3, 2023 at 9:23

1 Answer 1

1
$\begingroup$

The C/C++ languages mandate that you explicitly delete the heap-allocated memory, and a standard compiler or IDE will not insert the statement for you.

Determining if some variable is still used from a certain point is a basic task for a compiler, though in certain cases it is not possible to determine the "tightest" place.

This kind of considerations belongs to static analysis.


Consider this example:

int* large_arr = new int [1000000];
int n; cin >> n; n= 2 * n + 1;
if (n == sum_of_proper_divisors(n))
{
   process(large_arr);
}
delete [] large_arr;

What would a compiler do ? What would you do ?

$\endgroup$
2
  • $\begingroup$ Thanks for the reply, I see the problem is non-trivial. The same problem also appears for compilers to decide whether we can optimize out a certain branch but it is routine for modern compilers. So it is true that this is still possible in some practical case, but people do not do that because of low benefit? $\endgroup$
    – Peter Wu
    Commented Aug 3, 2023 at 8:09
  • $\begingroup$ @PeterWu: there is no tradition of compilers modifying the code. As far as I know, memory allocation optimization is not considered. On the opposite, garbage collection postpones the deallocations. $\endgroup$
    – user16034
    Commented Aug 3, 2023 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.