Jitted Code Pitching Support
We propose to support releasing memory that is allocated for methods that have been executed and will never be executed again or can be recompiled. That is needed for devices with limited memory capacity.
Also when GDB JIT feature (#6278) is enabled there is additional memory consumption (~700 bytes on ARM32 per method with several lines) for ELF debugging information. Such memory can be released when jitted code for method itself released.
The existing execution model of coreclr without releasing memory for executed code and debugging info creates serious obstacles for applications running on devices with limited memory or even can make impossible their execution in certain cases.
@papaslavik @brucehoult @Dmitri-Botcharnikov @leemgs @lemmaa @wateret
The earlier versions of CLR had this mechanism. It was called code-pitching but it was removed because of it was very unreliable. The code for it was part of "Shared Source Common Language Infrastructure" that you can find around if you would like to take a look. The hard problem with code pitching is to guarantee that the method being discarder is not executing, and that nobody is holding pointer to it.
cc @noahfalk @dotnet/jit-contrib
Is it possible to define that the method is not executing by stopping the program execution and inspection of stack frames for all threads?
Yes, the original code pitching implementation have done something similar.
except for FCALLs? Because I read in comments // FCall is a high-performance alternative to ECall. Unlike ECall, FCall // methods do not necessarily create a frame. Jitted code calls directly // to the FCall entry point. It is possible to do operations that need // to have a frame within an FCall, you need to manually set up the frame // before you do such operations.
FCalls are a problem because they are implemented in C++ or hand-written assembly, and so they cannot be discarder.
needed for devices with limited memory capacity
It may be a good idea to do analysis on how much memory can be saved by doing this for the typical applications. The JITed code does not typically dominate the working set.
For data we have garbage collection... So it sort of has a control on the memory increase for data.
@jkotas I wonder if you mean the meta-data more than just data compared to the code here...
Also it's interesting if it's possible to write a test using serialization an de-serialization or something like that which causes an unbounded increase in the memory for the code of dynamic methods.
The hard problem with code pitching is to guarantee that the method being discarder is not executing, and that nobody is holding pointer to it.
Do you mean the backpatching thing here or arbitrary pointers to the code?
I also believe such a feature could be useful for the application of an aggressive inlining etc.
@jkotas From my impression on average the JITed code size of a small function on ARM is 3 times larger than the corresponding IL assembly of the method (not talking about too small functions).
Of course, the functions which have a lot of calls interfered with the local variables might have the factor of 8 or similar.
E.g. if we take a test JIT/SIMD/CircleInConvex_r/CircleInConvex_r.exe:
Method ClassLibrary.test:convex_hull(ref) has 476 IL size and 1426 emitted size
ClassLibrary.test:Check(ref):bool : 166 IL and 570 emitted
A small function ClassLibrary.test:abs(float):float has 19 IL vs 114 emitted
Also the method Main() for the test IT/Directed/cmov/Float_Xor_Op_cs_d/Float_Xor_Op_cs_d.exe has a bigger factor: 3763 IL vs 33234 emitted
So, I believe there might be a considerably big application which has a lot of executed code.
First, I do agree that having ability to drop JITed code would be really nice.
Based on my experience, I do not believe that it will move the needle significantly (e.g. more than 1.5x) in total RAM consumption of managed process, or in ability to run on very low end devices, for typical apps.
But if you had to have a Facebook and a Browser application on a low-end device, it would be a non-typical but a very resource consuming app.
Still Facebook presumably would have a lot of class description meta-data present in the RAM. So it would nevertheless be about 1.5x as you say, but the absolute number could be significant.
@jkotas By the way, is the class unloading feature present in coreclr?
Well, I don't think it would be, provided, no code unloading is supported.
Class unloading is present for Reflection.Emit (AssemblyBuilderAccess.RunAndCollect). It works with assembly granularity.
Method ClassLibrary.test:convex_hull(ref) has 476 IL size and 1426 emitted size
Out of interest, I quickly converted convex_hull(List
struct Point {float X; float Y;};
template <class T>
class List{
public:
List();
int Count;
void Add(T);
void RemoveAt(int);
void Clear();
T& operator[](int i);
T& Last();
typedef int cmp_fn(T&, T&);
void Sort(cmp_fn);
};
@sergign60 Is this still an active issue tracking work being done?