[Mono-dev] increased memory usage when embedding mono under special environment

Bernhard Urban beurba at microsoft.com
Tue Jul 4 13:01:06 UTC 2017

How do you determine said memory usage?  Is your special program somehow messing with malloc?  Note that mono uses malloc for quite some stuff, so if your special program messes with the allocation pattern that could explain it. I suggest to look at it with some profiler.

From: Mono-devel-list <mono-devel-list-bounces at lists.dot.net> on behalf of Jovic, Vladimir <vladimir.jovic at ifm.com>
Sent: Tuesday, July 4, 2017 2:39:35 PM
To: mono-devel-list at lists.dot.net
Subject: [Mono-dev] increased memory usage when embedding mono under special    environment


I am crosscompiling mono 4.4.2 for PPC platform (couldn't compile newer version, as the configuration fails).
Also, I implemented a c++ library, which executes a C# function in DLL. This library is dynamically loaded at runtime (using dlopen) by a program with very special environment. I wouldn't be surprised to hear that this program experience both stack smash and memory overwrites at the same time, but I am not in a position to pick.

Now the problem I am having is: when I use my library in a standalone program, then it works fine. But when I use my library with the program with this special environment, than it uses 40 more megabytes to do some initializations. The parameters for both programs are the same (default, as I do not set any environment variables).

The code to set the mono library is next. As you see, it is a code that can be found in any example of how to execute a c# function from a DLL.

MonoMethod* csMethodEntryPoint = NULL;

void SetMethodEntryPoint( const std::string& ifmDllsPath )
    mono_config_parse( NULL );

    MonoDomain* monoDomain = mono_jit_init_version( "pdm_mono_sdk", "v4.0.30319" );

    const std::string assemblyName( ifmDllsPath + "/ifmApiLinuxWrapper.dll" );
    MonoAssembly* assembly = mono_domain_assembly_open( monoDomain, assemblyName.c_str() );
    if ( NULL == assembly )
        throw std::runtime_error( "assembly is NULL" );

    MonoImage* monoImage = mono_assembly_get_image( assembly );
    if ( NULL == monoImage )
        throw std::runtime_error( "monoImage is NULL" );

    MonoClass* entityClass = mono_class_from_name( monoImage, "ifmApiLinuxWrapper", "ifmApiLinuxWrapper" );
    if ( NULL ==  entityClass )
        throw std::runtime_error( "entityClass is NULL" );

    csMethodEntryPoint = mono_class_get_method_from_name( entityClass, "ApiCmdRequest", 2 );
    if ( NULL == csMethodEntryPoint )
        throw std::runtime_error( "entryPoint is NULL" );

The function that allocates extra memory is mono_jit_init_version(). In a standalone program, it allocates 1M, and with this special program 20M!
I said above that I lose 40M. So, 19M with mono_jit_init_version(), and extra 21M from a call to c# function.
I took a look into the mono source code for mono_jit_init_version() function, but the function is so huge, that I got lost quite quickly.

The question is how to debug this. Can I set some environment variable to help me track why extra memory is allocated?

Vladimir Jovic

Mono-devel-list mailing list
Mono-devel-list at lists.dot.net

More information about the Mono-devel-list mailing list