V: Memory allocated for a local array is never freed

Created on 21 Jan 2020  路  3Comments  路  Source: vlang/v

V version: 0.1.24
OS: Windows 7

What did you do?
Run the following program:

fn main()
  {
  for i := 1; i < 100000000; i++
    {
    a := [1, 2, 3]
    }
  }

What did you expect to see?
A small and limited amount of RAM is allocated for a local array

What did you see instead?
Allocated RAM grows infinitely until the whole system hangs.
The C listing shows that no attempt is made to free memory allocated at each loop iteration:

 void main__main () {
 for (
int i= 1  ;  i < 100000000  ;  i ++ ) { 

array_int a=new_array_from_c_array(3, 3, sizeof(int), EMPTY_ARRAY_OF_ELEMS( int, 3 ) {  1 ,  2 ,  3  }) ;
 }
 ;
 }
Bug Memory Management

Most helpful comment

What memory management approach are you going to adopt? I see three possible ways, but none of them seems to me feasible:

  • Free heap memory when leaving the scope. But this implies two system API calls per each loop iteration, so that the performance cannot be "within 3% of C", as it is claimed now.
  • Run garbage collector. But I think it contradicts the philosophy of V and also leads to performance issues.
  • Allocate local arrays on the stack instead of the heap. But I wonder how to append new elements to such arrays.

All 3 comments

Memory management isn't ready yet unfortunately.

What memory management approach are you going to adopt? I see three possible ways, but none of them seems to me feasible:

  • Free heap memory when leaving the scope. But this implies two system API calls per each loop iteration, so that the performance cannot be "within 3% of C", as it is claimed now.
  • Run garbage collector. But I think it contradicts the philosophy of V and also leads to performance issues.
  • Allocate local arrays on the stack instead of the heap. But I wonder how to append new elements to such arrays.

I've read from V Discord channel that it's going to be similar to this https://aardappel.github.io/lobster/memory_management.html

Was this page helpful?
0 / 5 - 0 ratings

Related issues

markgraydev picture markgraydev  路  3Comments

radare picture radare  路  3Comments

XVilka picture XVilka  路  3Comments

fidergo-stephane-gourichon picture fidergo-stephane-gourichon  路  3Comments

cjmxp picture cjmxp  路  3Comments