Memory tips

56 views
Skip to first unread message

rsuk

unread,
Oct 12, 2018, 11:59:14 AM10/12/18
to mono-cecil
Firstly, a huge thank you - mono.cecil has been hugely useful and clearly you have invested a massive amount of work on it over the years.

I have completed an initial set of development using mono.cecil; using it only for inspecting type information, not generating assemblies, reading .pdb etc. I'm wondering if you might be able to offer any tips on reducing the memory usage? I have been experimenting with different reader parameters and have currently settled on:

InMemory = false,
ReadWrite = false,
ReadSymbols = false

We only use a fairly small subset of the properties available on the TypeReference, TypeDefinition etc., so I'm wondering if you might have any tips on how we might be able to minimize memory usage once we have loaded all the required AssemblyDefinitions; i.e. I'm wondering if it might be possible to discard some data held behind the scenes that might not be necessary in an inspection only scenario. Not necessarily looking for anything built in, maybe something we could implement in our own code to "prune" the object graph somehow to reduce the overall memory footprint.

One thing we have observed is that a large number of byte arrays are held in memory - does mono.cecil hold the raw metadata in memory and query on demand, or could this be data that is being held in memory for some other purpose - i.e. manipulating and writing assemblies. I wonder if might be possible to somehow remove that once we have completed the initial population of the objects?

Thanks for any pointers or tips you might be able to offer - much appreciated.

Richard

Jb Evain

unread,
Oct 15, 2018, 12:42:38 PM10/15/18
to mono-...@googlegroups.com
Hey Richard,

Thanks for the kind words, always glad to read that Cecil is useful!

I don't think there's anything built-in that will help you more. Most metadata is lazy loaded on demand, so if you're using only a subset you should not consume too much memory. One useful pattern is to always use the HasN properties before accessing the N collection.

For instance:

if type.HasMethods
  foreach (var m in type.Methods)
    ...;

This prevents allocating empty collections if there's no methods in there.

If you want to dive into the internals, the parts of the binary that contains the metadata information (the heaps, https://github.com/jbevain/cecil/blob/master/Mono.Cecil.PE/ImageReader.cs#L367) will indeed be loaded in memory as a byte[] for fast access.

If you're very memory constrained you can change Cecil to always read on disk but you'll see a lot of seeking which is likely going to be slowish.

Jb



--
--
--
mono-cecil
---
You received this message because you are subscribed to the Google Groups "mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mono-cecil+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard Sloggett

unread,
Oct 15, 2018, 12:50:32 PM10/15/18
to mono-...@googlegroups.com

Hello,

 

Thanks for getting back to me, and for the tips on using the Has.. properties – I will certainly do that.

 

Is there a setting built in to switch from an in memory byte array to reading from disk, or is that something I would need to do implement in a custom build of cecil?

 

Richard

Jb Evain

unread,
Oct 15, 2018, 12:55:37 PM10/15/18
to mono-...@googlegroups.com
Afraid there's no switch :)
Reply all
Reply to author
Forward
0 new messages