public IHttpActionResult GetProducts(GenObj obj) { try { var wait = Helper.Delay(2000); //var sp = StoreItem.GetItemStoreProcedure(); //var items = StoreItem.GetItemsLazyLoading(obj.Skip, obj.Take); List <StoreItem> items = new List <StoreItem>(); for (int i = 0; i < 3; i++) { StoreItem sti = new StoreItem(); sti.Color = 1; sti.Description = i + " Description Description Description"; sti.Id = i; sti.Name = "Name " + i; sti.Price = 90 * i; sti.Pic = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRLto1NKBd8N--_2SRzNup0qNI6Z3if070KCbj_zmrm16IWJota"; items.Add(sti); } return(Ok(new { Error = "", data = items })); } catch (Exception exs) { return(InternalServerError(new Exception(exs.Message + "getproducts"))); } }
public ContentStore( IPublishedSnapshotAccessor publishedSnapshotAccessor, IVariationContextAccessor variationContextAccessor, IUmbracoContextAccessor umbracoContextAccessor, ILogger logger, BPlusTree <int, ContentNodeKit> localDb = null) { _publishedSnapshotAccessor = publishedSnapshotAccessor; _variationContextAccessor = variationContextAccessor; _umbracoContextAccessor = umbracoContextAccessor; _logger = logger; _localDb = localDb; _contentNodes = new ConcurrentDictionary <int, LinkedNode <ContentNode> >(); _contentRootNodes = new ConcurrentDictionary <int, LinkedNode <object> >(); _contentTypesById = new ConcurrentDictionary <int, LinkedNode <PublishedContentType> >(); _contentTypesByAlias = new ConcurrentDictionary <string, LinkedNode <PublishedContentType> >(StringComparer.InvariantCultureIgnoreCase); _xmap = new ConcurrentDictionary <Guid, int>(); _genObjs = new ConcurrentQueue <GenObj>(); _genObj = null; // no initial gen exists _liveGen = _floorGen = 0; _nextGen = false; // first time, must create a snapshot _collectAuto = true; // collect automatically by default }
private static void Main() { WriteLine($"Maximum generations: {GC.MaxGeneration}"); // Создание нового объекта в куче object o = new GenObj(); // Поскольку этот объект создан недавно, он помещается в поколение 0 WriteLine($"Gen: {GC.GetGeneration(0)}"); // Сборка мусора переводит объект в следующее поколение GC.Collect(); WriteLine($"Gen: {GC.GetGeneration(o)}"); // 1 GC.Collect(); WriteLine($"Gen: {GC.GetGeneration(o)}"); // 2 GC.Collect(); WriteLine($"Gen: {GC.GetGeneration(o)}"); // 2: максимальное значение o = null; // Уничтожаем жесткую ссылку на объект WriteLine("Collecting Gen 0"); GC.Collect(0); // Сборка мусора в поколении 0 GC.WaitForPendingFinalizers(); // Методы финализации НЕ вызываются WriteLine("Collecting Gen 0 and 1"); GC.Collect(1); // Сборка мусора в поколенияз 0 и 1 GC.WaitForPendingFinalizers(); // Методы финализации НЕ вызываются WriteLine("Collecting Gen 0, 1 and 2"); GC.Collect(2); // Тотальная сборка мусора GC.WaitForPendingFinalizers(); // Вызываются методы финализации }
private void Lock(WriteLockInfo lockInfo, bool forceGen = false) { if (Monitor.IsEntered(_wlocko)) { throw new InvalidOperationException("Recursive locks not allowed"); } Monitor.Enter(_wlocko, ref lockInfo.Taken); lock (_rlocko) { // assume everything in finally runs atomically // http://stackoverflow.com/questions/18501678/can-this-unexpected-behavior-of-prepareconstrainedregions-and-thread-abort-be-ex // http://joeduffyblog.com/2005/03/18/atomicity-and-asynchronous-exception-failures/ // http://joeduffyblog.com/2007/02/07/introducing-the-new-readerwriterlockslim-in-orcas/ // http://chabster.blogspot.fr/2013/12/readerwriterlockslim-fails-on-dual.html //RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { if (_nextGen == false || (forceGen)) { // because we are changing things, a new generation // is created, which will trigger a new snapshot if (_nextGen) { _genObjs.Enqueue(_genObj = new GenObj(_liveGen)); } _liveGen += 1; _nextGen = true; // this is the ONLY place where _nextGen becomes true } } } }
public Snapshot CreateSnapshot() { lock (_rlocko) { // if no next generation is required, and we already have a gen object, // use it to create a new snapshot if (_nextGen == false && _genObj != null) { return(new Snapshot(this, _genObj.GetGenRef())); } // else we need to try to create a new gen object // whether we are wlocked or not, noone can rlock while we do, // so _liveGen and _nextGen are safe if (Monitor.IsEntered(_wlocko)) { // write-locked, cannot use latest gen (at least 1) so use previous var snapGen = _nextGen ? _liveGen - 1 : _liveGen; // create a new gen object if we don't already have one // (happens the first time a snapshot is created) if (_genObj == null) { _genObjs.Enqueue(_genObj = new GenObj(snapGen)); } // if we have one already, ensure it's consistent else if (_genObj.Gen != snapGen) { throw new PanicException($"The generation {_genObj.Gen} does not equal the snapshot generation {snapGen}"); } } else { // not write-locked, can use latest gen (_liveGen), create a corresponding new gen object _genObjs.Enqueue(_genObj = new GenObj(_liveGen)); _nextGen = false; // this is the ONLY thing that triggers a _liveGen++ } // so... // the genObj has a weak ref to the genRef, and is queued // the snapshot has a ref to the genRef, which has a ref to the genObj // when the snapshot is disposed, it decreases genObj counter // so after a while, one of these conditions is going to be true: // - genObj.Count is zero because all snapshots have properly been disposed // - genObj.WeakGenRef is dead because all snapshots have been collected // in both cases, we will dequeue and collect var snapshot = new Snapshot(this, _genObj.GetGenRef()); // reading _floorGen is safe if _collectTask is null if (_collectTask == null && _collectAuto && _liveGen - _floorGen > CollectMinGenDelta) { CollectAsyncLocked(); } return(snapshot); } }
public SnapDictionary() { _items = new ConcurrentDictionary <TKey, LinkedNode <TValue> >(); _genObjs = new ConcurrentQueue <GenObj>(); _genObj = null; // no initial gen exists _liveGen = _floorGen = 0; _nextGen = false; // first time, must create a snapshot _collectAuto = true; // collect automatically by default }
/// <summary> /// Gets the value of a field or property of an object, given its name. /// </summary> /// <param name="FieldName">Name of field or property.</param> /// <param name="Object">Object.</param> /// <returns>Corresponding field or property value, if found, or null otherwise.</returns> public override async Task <object> TryGetFieldValue(string FieldName, object Object) { if (Object is GenericObject GenObj) { if (GenObj.TryGetFieldValue(FieldName, out object Value)) { return(Value); } else { return(null); } } else if (!(Object is null) && this.returnTypedObjects) { IObjectSerializer Serializer2 = await this.Context.GetObjectSerializer(Object.GetType()); return(await Serializer2.TryGetFieldValue(FieldName, Object)); }
// This method demonstrates how objects are promoted between generations. // Applications could take advantage of this info to improve performance // but most applications will ignore this information. private static void GenerationDemo() { Display(0, "\n\nDemo start: Understanding Generations.", +1); // Let's see how many generations the managed heap supports (we know it's 2) Display("Maximum GC generations: " + GC.MaxGeneration); // Create a new BaseObj in the heap GenObj obj = new GenObj("Generation"); // Since this object is newly created, it should be in generation 0 obj.DisplayGeneration(); // Displays 0 // Performing a GC promotes the object's generation Collect(); obj.DisplayGeneration(); // Displays 1 Collect(); obj.DisplayGeneration(); // Displays 2 Collect(); obj.DisplayGeneration(); // Displays 2 (max generation) obj = null; // Destroy the strong reference to this object Collect(0); // Collect objects in generation 0 WaitForFinalizers(); // We should see nothing Collect(1); // Collect objects in generation 1 WaitForFinalizers(); // We should see nothing Collect(2); // Same as Collect() WaitForFinalizers(); // Now, we should see the Finalize method run Display(-1, "Demo stop: Understanding Generations.", 0); }
private void Lock(WriteLockInfo lockInfo, bool forceGen = false) { Monitor.Enter(_wlocko, ref lockInfo.Taken); var rtaken = false; try { Monitor.Enter(_rlocko, ref rtaken); // see SnapDictionary try { } finally { _wlocked++; lockInfo.Count = true; if (_nextGen == false || (forceGen && _wlocked == 1)) { // because we are changing things, a new generation // is created, which will trigger a new snapshot if (_nextGen) { _genObjs.Enqueue(_genObj = new GenObj(_liveGen)); } _liveGen += 1; _nextGen = true; } } } finally { if (rtaken) { Monitor.Exit(_rlocko); } } }
public GenRef(GenObj genObj) { GenObj = genObj; }
public Snapshot CreateSnapshot() { var lockInfo = new ReadLockInfo(); try { Lock(lockInfo); // if no next generation is required, and we already have one, // use it and create a new snapshot if (_nextGen == false && _genObj != null) { return(new Snapshot(this, _genObj.GetGenRef() #if DEBUG , _logger #endif )); } // else we need to try to create a new gen ref // whether we are wlocked or not, noone can rlock while we do, // so _liveGen and _nextGen are safe if (_wlocked > 0) // volatile, cannot ++ but could -- { // write-locked, cannot use latest gen (at least 1) so use previous var snapGen = _nextGen ? _liveGen - 1 : _liveGen; // create a new gen ref unless we already have it if (_genObj == null) { _genObjs.Enqueue(_genObj = new GenObj(snapGen)); } else if (_genObj.Gen != snapGen) { throw new Exception("panic"); } } else { // not write-locked, can use latest gen, create a new gen ref _genObjs.Enqueue(_genObj = new GenObj(_liveGen)); _nextGen = false; // this is the ONLY thing that triggers a _liveGen++ } // so... // the genRefRef has a weak ref to the genRef, and is queued // the snapshot has a ref to the genRef, which has a ref to the genRefRef // when the snapshot is disposed, it decreases genRefRef counter // so after a while, one of these conditions is going to be true: // - the genRefRef counter is zero because all snapshots have properly been disposed // - the genRefRef weak ref is dead because all snapshots have been collected // in both cases, we will dequeue and collect var snapshot = new Snapshot(this, _genObj.GetGenRef() #if DEBUG , _logger #endif ); // reading _floorGen is safe if _collectTask is null if (_collectTask == null && _collectAuto && _liveGen - _floorGen > CollectMinGenDelta) { CollectAsyncLocked(); } return(snapshot); } finally { Release(lockInfo); } }
public GenRef(GenObj genObj) => GenObj = genObj;