DEVSENSE / Phalanger

PHP 5.4 compiler for .NET/Mono frameworks. Predecessor to the opensource PeachPie project (www.peachpie.io).
http://v4.php-compiler.net/
Apache License 2.0
382 stars 94 forks source link

100,000 different script method, program memory is very large, can add unload temp assembly public method?? #58

Closed taotao2014 closed 8 years ago

taotao2014 commented 8 years ago

100,000 different script method, program process memory is very large, can add unload temp assembly public method?? , have a better solution?????

Thank you very much.

Assembly beisen_AppFramework_TempFile_0CF8D27731D05A79C556E0E242E4E4D8~8#35863539#08d2e17a0584efaf, Version 1.0.0.0 Assembly beisen_AppFramework_TempFile_000B2394F04EC39A533F92CC2F63FFD0~8#35863539#08d2e11ae09dcbd0, Version 1.0.0.0 ..........

http://dfiles.tita.com/portal/110006/19c825bb1ccb4f87b628f23a9102408d.jpg

http://dfiles.tita.com/portal/110006/8c45ebaa82b64a3fb781b1dc1ced893d.jpg

http://dfiles.tita.com/portal/110006/b5ea45c093934cae90b451d466bf8cd6.jpg

http://dfiles.tita.com/portal/110006/0c3f793f05aa4620b6b589b12db73656.jpg

http://dfiles.tita.com/portal/110006/1e57a9c492bb431ca125900bd8cbc34c.jpg

http://dfiles.tita.com/portal/110006/7652d9f427fb4bae9808d10c5d8cb4aa.jpg

my c# code:

  private static ScriptContext _scriptContext;

  static PhpDynamicScript()
    {
        _scriptFileHashDict = new Dictionary<string, string>();
        _scriptContext = new ScriptContext(ApplicationContext.Default);
        _scriptContext.GlobalVariables["_logger"] =
           _scriptContext.NewObject(@"Beisen\Logging\LogWrapper"); ;
        Tools.CreateDirectory(DynamicScriptEngineSettings.Instance.ScriptTempFile);
    }

  private bool ExecuteDynamicScript(string code, out string errorMsg)
    {
        code = "<? " + code + " ?>";
        errorMsg = string.Empty;
        try
        {
            string scriptHash = Tools.GetScriptHash(code);
            string scriptFileFullName =      Path.Combine(DynamicScriptEngineSettings.Instance.ScriptTempFile, scriptHash);
            string scriptFile = GenerateScriptFile(scriptFileFullName, code);
            _scriptContext.Include(scriptFile, true);
            _compiled = true;
            return true;
        }
        catch (Exception ex)
        {
            _logger.Error(new CompileErrorException("compile error",ex));
            errorMsg = "compile error";
            return false;
        }
    }

   public T CallFunction<T>(string functionName, params object[] parameters)
    {
        ArgumentHelper.AssertNotEmpty(functionName, "functionName");
        if (_scriptContext != null)
        {
            var result = InternalCallFunction(functionName, parameters);
            if (result == null)
                return default(T);
            if (result is ClrObject)
            {
                var clrObject = ((ClrObject) result); 
                dynamic realObj = (T)clrObject.RealObject;
                if (clrObject.RuntimeFields != null)
                    foreach (var field in clrObject.RuntimeFields)
                        realObj[field.Key.String] = field.Value;
                return realObj;
            }
            return (T)result;
        }
        return default(T);
    }

  private object InternalCallFunction(string functionName, params object[] parameters)
    { 
        if (!_scriptContext.DeclaredFunctions.ContainsKey(functionName))
        {
            _logger.Error(
                new ScriptFunctionNotFoundException(String.Format("TenantId:{0},FunctionName:{1},Language:{2}",
                    _tenantId, functionName, Language)));
            return null;
        }
        var result = _scriptContext.Call(functionName, parameters);
        if (result != null && result.Value != null)
            return result.Value;
        return null;
    }

Thank you very much, have a better solution?????

jakubmisek commented 8 years ago

That's how .NET works. You are creating script files and Phalanger is compiling them - loads them into memory and call them. There is no way of unloading assemblies from the process memory.