Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rakudo Memory/Garbage collecting techniques

I understand that this question verges into implementation specific domains, but at this point, Rakudo/MoarVM specific answers would help me too.

I am working on some NativeCall modules, and wondering how to debug memory leaks. Some memory is handled in the C library, and I have a good handle over there. I know that domain is my responsibility and there is nothing that MoarVM can do over there. What can I do in the MoarVM domain? what is the best way to check for dangling objects, circular references, etc.?

Is there a way at the end of a series of operations, where I think all of my Perl objects are out of scope to say "Run Garbage Collection and tell me about anything left"?

Is there some Rakudo/NQP/MoarVM specific code I can run to help me? This isn't to release in production, just for testing/diagnostics while I am developing.

Garbage Collection in MoarVM gives a tantalizing overview, but not enough information for me to do anything with it.

like image 892
Curt Tilmes Avatar asked Feb 09 '18 14:02

Curt Tilmes


People also ask

What is garbage collection in memory?

Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java. A GC-enabled programming language includes one or more garbage collectors (GC engines) that automatically free up memory space that has been allocated to objects no longer needed by the program.

How does generational garbage collection work?

The Generational Garbage Collection ProcessBoth survivor spaces start out empty. When the eden space fills up, a minor garbage collection is triggered. Referenced objects are moved to the first survivor space. Unreferenced objects are deleted when the eden space is cleared.

Does garbage collector clean stack memory?

GC sweeps heap memory only. Usually, stack memory is collected automatically when the execution path reaches the end of the scope. It's right time to invest in Cryptocurrencies Dogecoin !


Video Answer


1 Answers

Firstly, while leaked memory on the C-side isn't your problem in this case, it's worth knowing that Rakudo installs a perl6-valgrind-m that runs the program under valgrind. I've used this a number of times to figure out segfaults and leaks when writing native library bindings.

For looking into objects managed by MoarVM, it's possible to get the VM to dump heap snapshots. They are taken after each GC run, and an extra GC run is forced and a final snapshot taken at the end of the program. To record snapshots, run with --profile=heap. The output file can then be fed to moar-ha, which can be installed using zef install App::MoarVM::HeapAnalyzer (it's implemented in Perl 6, which may be worth knowing should you wish to extend it in some way to help you solve you problems).

If you have any idea of what kind of objects might be leaking, then it can be useful to search for objects of that type with the find command. There is then a path command that shows how that object is being kept alive. It can also be useful to look at counts of objects between different heap snapshots, to see what is growing in use. Unfortunately there's not yet a snapshot diff feature.

One thing to note is that the snapshots include everything that runs atop of the VM. That means the Perl 6 compiler will be in memory, as well as a bunch of objects for things from the language built-ins. (The tool was developed to help track down managed leaks in the compiler and built-ins, so this is considered a feature. :-) Some kind of filtering may be feasible in the future, however.)

Finally, you mentioned circular references. These are not a problem in Perl 6, since GC is done through tracing, not reference counting.

like image 114
Jonathan Worthington Avatar answered Nov 12 '22 03:11

Jonathan Worthington