Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Core / CSharp / System / Windows / Media / SafeMILHandleMemoryPressure.cs / 1407647 / SafeMILHandleMemoryPressure.cs
//---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // // Description: // Tracks the amount of native memory used by SafeMILHandle objects. //--------------------------------------------------------------------------- using System; using System.Security; using MS.Internal; using System.Threading; namespace System.Windows.Media { internal class SafeMILHandleMemoryPressure { ////// Critical: This code calls into AddMemoryPressure which has a link demand. /// [SecurityCritical] internal SafeMILHandleMemoryPressure(long gcPressure) { _gcPressure = gcPressure; _refCount = 0; if (_gcPressure > SMALL_BITMAP_MEM) { MemoryPressure.Add(_gcPressure); } else { GC.AddMemoryPressure(_gcPressure); } } internal void AddRef() { Interlocked.Increment(ref _refCount); } ////// Critical: This code calls into RemoveMemoryPressure which has a link demand. /// [SecurityCritical] internal void Release() { if (Interlocked.Decrement(ref _refCount) == 0) { if (_gcPressure > SMALL_BITMAP_MEM) { MemoryPressure.Remove(_gcPressure); } else { GC.RemoveMemoryPressure(_gcPressure); } _gcPressure = 0; } } // Estimated size in bytes of the unmanaged memory private long _gcPressure; // // SafeMILHandleMemoryPressure does its own ref counting in managed code, because the // associated memory pressure should be removed when there are no more managed // references to the unmanaged resource. There can still be references to it from // unmanaged code elsewhere, but that should not prevent the memory pressure from being // released. // private int _refCount; // // The memory usage for a "small" bitmap // // Small bitmaps will be handled by GC.AddMemoryPressure() rather than WPF's own // MemoryPressure algorithm. GC's algorithm is less aggressive than WPF's, and will // result in fewer expensive GC.Collect(2) operations. // // For now, a small bitmap is 32x32 or less. It's at 4 bytes per pixel, and the *2 is // to account for the doubled estimate in // BitmapSourceMILSafeHandle.ComputeEstimatedSize(). // private const long SMALL_BITMAP_MEM = 32 * 32 * 4 * 2; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // // Description: // Tracks the amount of native memory used by SafeMILHandle objects. //--------------------------------------------------------------------------- using System; using System.Security; using MS.Internal; using System.Threading; namespace System.Windows.Media { internal class SafeMILHandleMemoryPressure { ////// Critical: This code calls into AddMemoryPressure which has a link demand. /// [SecurityCritical] internal SafeMILHandleMemoryPressure(long gcPressure) { _gcPressure = gcPressure; _refCount = 0; if (_gcPressure > SMALL_BITMAP_MEM) { MemoryPressure.Add(_gcPressure); } else { GC.AddMemoryPressure(_gcPressure); } } internal void AddRef() { Interlocked.Increment(ref _refCount); } ////// Critical: This code calls into RemoveMemoryPressure which has a link demand. /// [SecurityCritical] internal void Release() { if (Interlocked.Decrement(ref _refCount) == 0) { if (_gcPressure > SMALL_BITMAP_MEM) { MemoryPressure.Remove(_gcPressure); } else { GC.RemoveMemoryPressure(_gcPressure); } _gcPressure = 0; } } // Estimated size in bytes of the unmanaged memory private long _gcPressure; // // SafeMILHandleMemoryPressure does its own ref counting in managed code, because the // associated memory pressure should be removed when there are no more managed // references to the unmanaged resource. There can still be references to it from // unmanaged code elsewhere, but that should not prevent the memory pressure from being // released. // private int _refCount; // // The memory usage for a "small" bitmap // // Small bitmaps will be handled by GC.AddMemoryPressure() rather than WPF's own // MemoryPressure algorithm. GC's algorithm is less aggressive than WPF's, and will // result in fewer expensive GC.Collect(2) operations. // // For now, a small bitmap is 32x32 or less. It's at 4 bytes per pixel, and the *2 is // to account for the doubled estimate in // BitmapSourceMILSafeHandle.ComputeEstimatedSize(). // private const long SMALL_BITMAP_MEM = 32 * 32 * 4 * 2; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- LineSegment.cs
- EntityTypeBase.cs
- InfoCardProofToken.cs
- Matrix.cs
- XslVisitor.cs
- DateTimeOffset.cs
- SelectionUIHandler.cs
- Activity.cs
- XmlCharCheckingReader.cs
- DesignTimeXamlWriter.cs
- DynamicValueConverter.cs
- DeobfuscatingStream.cs
- PathGeometry.cs
- XsdCachingReader.cs
- ServicesUtilities.cs
- CharUnicodeInfo.cs
- NonParentingControl.cs
- ConnectionStringsExpressionBuilder.cs
- ContextCorrelationInitializer.cs
- CachingParameterInspector.cs
- FacetDescription.cs
- AlgoModule.cs
- QueueNameHelper.cs
- PageAdapter.cs
- SizeKeyFrameCollection.cs
- DiscoveryClient.cs
- NeutralResourcesLanguageAttribute.cs
- OdbcConnectionHandle.cs
- StreamingContext.cs
- XmlBinaryReaderSession.cs
- Matrix.cs
- WaitHandle.cs
- UnmanagedMemoryAccessor.cs
- ValueType.cs
- MethodImplAttribute.cs
- X509Certificate.cs
- Misc.cs
- UnicastIPAddressInformationCollection.cs
- CodeSubDirectoriesCollection.cs
- NodeLabelEditEvent.cs
- UpdateRecord.cs
- InstanceDataCollectionCollection.cs
- WindowsToolbar.cs
- Pointer.cs
- FrameworkContextData.cs
- DataSourceGroupCollection.cs
- SystemIcons.cs
- SqlTopReducer.cs
- WindowsNonControl.cs
- MenuItemStyle.cs
- XMLSyntaxException.cs
- DurationConverter.cs
- InstanceContextManager.cs
- ZipPackagePart.cs
- ControlBuilder.cs
- InvokePatternIdentifiers.cs
- AppLevelCompilationSectionCache.cs
- BaseCodePageEncoding.cs
- HtmlTableCellCollection.cs
- Config.cs
- relpropertyhelper.cs
- PropertyDescriptorCollection.cs
- SessionIDManager.cs
- OletxVolatileEnlistment.cs
- ResourceContainer.cs
- SqlDataSourceQueryEditor.cs
- MessageEventSubscriptionService.cs
- DrawingGroup.cs
- WebPartsSection.cs
- DoubleConverter.cs
- ImportStoreException.cs
- ClusterSafeNativeMethods.cs
- ColumnResizeUndoUnit.cs
- MethodAccessException.cs
- OuterGlowBitmapEffect.cs
- MultiPageTextView.cs
- XmlSubtreeReader.cs
- BitmapInitialize.cs
- Drawing.cs
- Form.cs
- ProcessRequestArgs.cs
- TablePatternIdentifiers.cs
- CodeParameterDeclarationExpression.cs
- DriveInfo.cs
- AddInAdapter.cs
- FieldAccessException.cs
- RtfToken.cs
- DataTablePropertyDescriptor.cs
- configsystem.cs
- DataSourceXmlTextReader.cs
- TextBoxBase.cs
- ColorAnimationBase.cs
- OleTxTransaction.cs
- WorkflowShape.cs
- X509Certificate.cs
- WebPartTransformer.cs
- EntityDataSourceStatementEditor.cs
- PersonalizationAdministration.cs
- DataSourceSelectArguments.cs
- DbException.cs