Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can RuntimeGeneratedFunctions.jl cause memory leak issue? #87

Open
vinhpb opened this issue Mar 28, 2024 · 2 comments
Open

Can RuntimeGeneratedFunctions.jl cause memory leak issue? #87

vinhpb opened this issue Mar 28, 2024 · 2 comments
Labels
question Further information is requested

Comments

@vinhpb
Copy link

vinhpb commented Mar 28, 2024

Hi,

I am writing a Genetic Programming-styled code to search for solutions to specific problems. The solutions are in the form of functions. Thus, I use RuntimeGeneratedFunctions.jl to generate functions in runtime in order to evaluate their fitnesses. As the code runs (on WSL) and time goes by, the amount of available RAM on my computer becomes less and less until the system forcibly closes the terminal. I suspect it is due to the generated functions. I wonder if it is a known problem and if there exists a solution. Thank you.

Here is the part of the code that involves RuntimeGeneratedFunctions.jl:

 function eval_solution(expr, data, eval_genfunc) 
      f = expr
      f1 = @RuntimeGeneratedFunction(f)
      fitness = evaluate_genfunc(f1, data)
      return fitness
 end

Here, expr is the Expr containing the content of the function to be generated, data is the data necessary to calculate the fitness of the generated function, eval_genfunc is a custom function to calculate the fitness of a generated function. eval_genfunc looks like this:

function eval_genfunc(f1, data) 
      parameters = f1(data)
      score = g(parameters)  % g performs a simulation with given parameters and extracts some information from there as the score
      return score
end

The function eval_solution( ) is used in multithreading mode in a main function:

function main(...)
 ...
 while iterate > 0
   Threads.@threads  for i in n_threads
      expr = ...  % Calling the function to generate an expr
      fitness = eval_solution(expr, data, eval_genfunc)
      ...
   end
   ...
   iterate -= 1
 end
 ...
end
@vinhpb vinhpb added the question Further information is requested label Mar 28, 2024
@ChrisRackauckas
Copy link
Member

The last word on this should be #63. @c42f is there still something that is retained, like the Julia compilation of the generated function?

@c42f
Copy link
Contributor

c42f commented Jul 17, 2024

IIUC, the answer is that yes, creating an unlimited number of functions at runtime will eventually cause your system to run out of memory.

Primarily I expect because all the old methods still exist in the Julia runtime: their internal CodeInfo objects, and the native code generated from that. I don't think there's any way to GC that stuff. Certainly it's not something we can control from RuntimeGeneratedFunctions - the best we can do here is to allow GC of the original Expr data structures (as pointed out - that's #63)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants