protected override void InnerCollectGarbage()
 {
     lock (_executionSynchronizer)
     {
         _jsEngine.CollectGarbage(true);
     }
 }
Beispiel #2
0
        public void Clear()
        {
            _context.Execute("Handlebars.templates = []; " +
                             "Handlebars.partials = []; ");

            // a good opportunity to return ram to it's rightful owner
            _context.CollectGarbage(true);
        }
Beispiel #3
0
        private static void Cleanup(V8ScriptEngine engine)
        {
            var data    = engine.Script as DynamicObject;
            var cleanup = new StringBuilder();

            foreach (var item in data.GetDynamicMemberNames())
            {
                if (item != "EngineInternal")
                {
                    cleanup.Append("delete " + item + ";");
                }
            }

            engine.Execute(cleanup.ToString());
            engine.CollectGarbage(true);
        }
Beispiel #4
0
 void IDisposable.Dispose()
 {
     foreach (var s in services)
     {
         engine.AddHostObject(s, new object());
         engine.Execute(string.Format("delete {0};", s));
     }
     services.Clear();
     SetContext(null);
     sw.Stop();
     if (Pool.GarbageCollection != JsEnginePoolGarbageStrategy.Automatic)
     {
         engine.CollectGarbage(exhaustive: (Pool.GarbageCollection == JsEnginePoolGarbageStrategy.ExhaustiveAfterUse));
     }
     Console.WriteLine("Engine {0}#{1,-3} Thread {2,-3} used for {3}.", PoolName, SerialNumber, Thread.CurrentThread.ManagedThreadId, sw.Elapsed);
     Pool.ReturnToPool(this);
 }
        public void V8ScriptEngine_ResourceConstraints_Dual()
        {
            const int limit = 4 * 1024 * 1024;
            const string code = @"x = []; for (i = 0; i < 1024 * 1024; i++) { x.push(x); }";

            engine.Execute(code);
            engine.CollectGarbage(true);
            var usedHeapSize = engine.GetRuntimeHeapInfo().UsedHeapSize;

            var constraints = new V8RuntimeConstraints
            {
                MaxYoungSpaceSize = limit,
                MaxOldSpaceSize = limit,
                MaxExecutableSize = limit
            };

            engine.Dispose();
            engine = new V8ScriptEngine(constraints);

            TestUtil.AssertException<ScriptEngineException>(() =>
            {
                try
                {
                    engine.Execute(code);
                }
                catch (ScriptEngineException exception)
                {
                    Assert.IsTrue(exception.IsFatal);
                    throw;
                }
            });

            engine.CollectGarbage(true);
            Assert.IsTrue(usedHeapSize > engine.GetRuntimeHeapInfo().UsedHeapSize);
        }
        public void V8ScriptEngine_ResourceConstraints()
        {
            const int limit = 4 * 1024 * 1024;
            const string code = @"x = []; while (true) { x.push(x); }";

            var constraints = new V8RuntimeConstraints
            {
                MaxYoungSpaceSize = limit,
                MaxOldSpaceSize = limit,
                MaxExecutableSize = limit
            };

            engine.Dispose();
            engine = new V8ScriptEngine(constraints);

            TestUtil.AssertException<ScriptEngineException>(() =>
            {
                try
                {
                    engine.Execute(code);
                }
                catch (ScriptEngineException exception)
                {
                    Assert.IsTrue(exception.IsFatal);
                    throw;
                }
            });

            TestUtil.AssertException<ScriptEngineException>(() =>
            {
                try
                {
                    engine.CollectGarbage(true);
                    engine.Execute("x = 5");
                }
                catch (ScriptEngineException exception)
                {
                    Assert.IsTrue(exception.IsFatal);
                    throw;
                }
            });
        }
Beispiel #7
0
        // Handles all events
        public void EventLoop()
        {
            var lastGarbageCollect = DateTime.Now;

            while (true)
            {
                var events             = 0;
                var eventLimitExceeded = false;

                // Check if timeouts should trigger and call then
                for (var i = 0; i < _timeouts.Count; i++)
                {
                    if (events >= EventLimit)
                    {
                        eventLimitExceeded = true;
                        break;
                    }

                    var timeout = _timeouts[i];
                    if (timeout.ShouldTrigger(DateTime.Now))
                    {
                        if (!timeout.IsRepeating())
                        {
                            _timeouts.RemoveAt(i);
                            i--;
                        }
                        else
                        {
                            timeout.Reset();
                        }

                        timeout.Call();
                        events++;
                    }
                }

                events = 0;

                // Handle all queued callbacks
                while (_callbacks.TryDequeue(out Callback callback))
                {
                    if (events >= EventLimit)
                    {
                        eventLimitExceeded = true;
                        break;
                    }

                    callback.Call();
                    events++;
                }

                events = 0;

                // Handle all queued requests
                while (_requests.TryDequeue(out Request request))
                {
                    if (events >= EventLimit)
                    {
                        eventLimitExceeded = true;
                        break;
                    }

                    request.Call();
                    events++;
                }

                if ((DateTime.Now - lastGarbageCollect).TotalMilliseconds >= 1000)
                {
                    try {
                        _engine.CollectGarbage(true);
                    } catch { }

                    lastGarbageCollect = DateTime.Now;
                }

                if (!eventLimitExceeded)
                {
                    // Sleep to keep from using all CPU
                    Thread.Sleep(1);
                }
            }
        }
        private static void Cleanup(V8ScriptEngine engine)
        {
            var data = engine.Script as DynamicObject;
            var cleanup = new StringBuilder();

            foreach (var item in data.GetDynamicMemberNames())
            {
                if (item != "EngineInternal")
                {
                    cleanup.Append("delete " + item + ";");
                }
            }
            
            engine.Execute(cleanup.ToString());
            engine.CollectGarbage(true);
        }