skip to main |
skip to sidebar
RSS Feeds
ASP.NET, JavaScript, Oracle and SQL articles and code.
ASP.NET, JavaScript, Oracle and SQL articles and code.
6:37 AM
Posted by Michael Heliso
As you all know any program is using resources. These resources can be files, data base resources, network connections, memory buffers, objects, etc. The usage of any resources requires memory to be allocated. This is achieved using the following steps:
1.Allocate memory for the type that represents the resource by calling newobj intermediate language instruction. This instruction is emitted when you use the new operator.
2.Initialize the memory to make the resource available. The initial state of the resource is created by the constructor of the type.
3.Use the resource by accessing type members.
4.Clean up the resource state.
5.Free the memory occupied by the resource. This task is performed by garbage collector.
The previous steps were generating two major bugs. First, often programmers forgotten to free memory when this was any longer needed. Second, programmers tried to access memory after this was freed. These two cases represent the worst bugs that can reside in an application because the behavior of the application in unpredictable and it's also very difficult to track them.
Resource management it's a difficult task and distracts you, the programmer from the main problems that you have to solve. So this is the reason why garbage collector was created. It will take all the burden of memory handling from your shoulders. You must keep in mind that garbage collector does not knows about a resource that is represented by types in memory. So, this means that it can not clean up the resource in proper manner. This steps must be performed by you, by writing the corresponding code which will clean up the resource. There are two methods that you can make use of to perform the resource cleaning, Finalize and Dispose.
Most types existent in .NET framework do not require resource clean up, types such as Int32, String, ArrayList . But there are also types which wrap some unmanaged resource like a network connection, a database connection, an icon, etc.
The Common Language Runtime also known as CLR requires that all resources to be allocated from the managed heap. This managed heap resembles with the heap from C runtime heap with one major difference, you never free objects from the managed heap, object will be automatically freed when the application doesn't needs them. Now let's see what happens when a process get's initialized. The CLR will reserve a contiguous zone of address space. This address zone represents the managed heap. The managed heap maintains pointer to this address space, we'll call it NextObjectPtr. This pointer indicates where in the managed heap the next object will be allocated. Initially NextObjectPtr points to the base address from the reserved address space. As your intuition tells you, newobj instruction creates a new object. As I have mentioned earlier, this instruction is emitted when you make use of new key word. The newobj instruction will determine the CLR to perform the next steps:
1.Calculate the number of bytes required by the type for which memory will be allocated and also for all it's based types.
2.Add the bytes required for an object overhead. Each object has two overhead fields, a method table pointer and a SyncBLockIndex. If you are using an 32 bit system, each field requires 32 bits, this will add 8 bytes to each object. In the case of a 64 bit system, each field requires 64 bits which will add 16 bytes to each object.
3.CLR then checks if the bytes required to allocate the object are available in the reserved address space (managed heap), if the will fit, then it is allocated at the address pointed by NextObjectPtr, the constructor is called passing NextObjectPtr for this parameter and the new operator will return the address of the object. The NextObjectPtr is moved after the currently allocated object and indicates the address where the next object will be allocated at in the managed heap.
When an application calls the new operator to create a new object there might not be enough space for it. The managed heap checks this by adding the required bytes of the objet to the address in NextObjectPtr. If the value exceeds the address space the managed heap is full and garbage collection takes action.
Garbage collector checks if there are any unused objects in the managed heap. If this kind of objects do exists, then the memory used by them can be reclaimed. If there is no more memory in the heap the new operator will throw an OutOfMemoryException.
An application has a set of roots. A root can be considered to be a memory storage location which contains a pointer to a reference type. This pointer can refer to an object or is set to null if the object doesn't exists. All global or static reference type variables are considered roots also any local variable reference type or parameter variable on a thread stack are considered as being roots. When garbage collector starts running (garbage collector starts when generation 0 of object is full, the garbage collector generation mechanism is used for performance improving, I'm not going to discuss this matter in this article), it will assume that all roots from the managed heap do not refer to any object. The garbage collector starts to iterate thru all roots and creates a graph with all objects that can be reached. In the image objects A and B are directly referenced by the roots so they will be added to the graph. When garbage collector will reach object C it will observer that this objects references another object from the managed heap, object D. So, object will be also added to the garbage collector graph. The entire iteration is performed recursively.
After the graph is completed, this will contain all objects reachable from your application. All other objects which are not a part of this graph are considered garbage. The garbage collector will iterate the heap linearly searching for free large continuous blocks of memory where new object could be allocated. Also garbage collector will shift non garbage objects in memory using memcpy function to compact the memory heap. This operation will make all pointers to objects invalid. So the garbage collector will correct all the invalid pointers. After the managed heap memory is compacted the NextObjectPtr will point exactly after the last non garbage object.
Now that you have a general overview of how garbage collector works you can design you applications properly.
In the next article I will talk with more details about object generations and also about Finalize and Dispose methods and how they should be used.
12:35 PM
Posted by Michael Heliso
Logical query processing step numbers
(8) SELECT (9) DISTINCT (11)
(1) FROM
(3)
(2) ON
(4) WHERE
(5) GROUP BY
(6) WITH {CUBE | ROLLUP}
(7) HAVING
(10) ORDER BY
The first noticeable aspect of SQL that is different than other programming languages is the order in which the code is processed. In most programming languages, the code is processed in the order in which it is written. In SQL, the first clause that is processed is the FROM clause, while the SELECT clause, which appears first, is processed almost last.
Each step generates a virtual table that is used as the input to the following step. These virtual tables are not available to the caller (client application or outer query). Only the table generated by the final step is returned to the caller. If a certain clause is not specified in a query, the corresponding step is simply skipped. Following is a brief description of the different logical steps applied in both SQL Server 2000 and SQL Server 2005.
Brief Description of Logical Query Processing Phases
1. FROM: A Cartesian product (cross join) is performed between the first two tables in the FROM clause, and as a result, virtual table VT1 is generated.
2. ON: The ON filter is applied to VT1. Only rows for which the
3. OUTER (join): If an OUTER JOIN is specified (as opposed to a CROSS JOIN or an INNER JOIN), rows from the preserved table or tables for which a match was not found are added to the rows from VT2 as outer rows, generating VT3. If more than two tables appear in the FROM clause, steps 1 through 3 are applied repeatedly between the result of the last join and the next table in the FROM clause until all tables are processed.
4. WHERE: The WHERE filter is applied to VT3. Only rows for which the
5. GROUP BY: The rows from VT4 are arranged in groups based on the column list specified in the GROUP BY clause. VT5 is generated.
6. CUBE | ROLLUP: Supergroups (groups of groups) are added to the rows from VT5, generating VT6.
7. HAVING: The HAVING filter is applied to VT6. Only groups for which the
8. SELECT: The SELECT list is processed, generating VT8.
9. DISTINCT: Duplicate rows are removed from VT8. VT9 is generated.
10. ORDER BY: The rows from VT9 are sorted according to the column list specified in the ORDER BY clause. A cursor is generated (VC10).
11. TOP: The specified number or percentage of rows is selected from the beginning of VC10. Table VT11 is generated and returned to the caller.
Itzik Ben-Gan and Lubor Kollar - Inside Microsoft® SQL Server™ 2005 T-SQL Querying
3:58 AM
Posted by Michael Heliso
Customer: <%# custID %>Where "Customer" is a label and "custID" is a public property.
namespace MyControls { public enum DataTypes { Default, String, Integer, DateTime, Double } }
namespace MyControls { public interface IDataBound { DataTypes DataType { get; set; } string BoundColumn { get; set; } object BoundValue { get; set; } bool SingleBind { get; set; } } }
namespace MyControls { public interface IDataBoundInfo : IDataBound { string TableName { get; set; } } }
using System; using System.Web.UI.WebControls; /// <summary> /// Summary description for BindingTextBox /// </summary> namespace MyControls { public class BindingTextBox : TextBox, IDataBoundInfo { /// <summary> /// IDataBound members. /// </summary> private DataTypes _datatype = DataTypes.Default; private string _boundcolumn; private bool _singlebind; /// <summary> /// IDataBoundInfo members. /// </summary> private string _tablename; public DataTypes DataType { get { return _datatype; } set { _datatype = value; } } public string BoundColumn { get { return _boundcolumn; } set { _boundcolumn = value; } } public virtual object BoundValue { get { return ControlHelper.ConvertValue (_datatype, this.Text); } set { if (value is DBNull) this.Text = ""; else this.Text = value.ToString(); } } public bool SingleBind { get { return _singlebind; } set { _singlebind = value; } } public string TableName { get { return _tablename; } set { _tablename = value; } } } }
using System; /// <summary> /// Summary description for ControlHelper /// </summary> namespace MyControls { public class ControlHelper { public static object ConvertValue(DataTypes toType, object value) { try { switch (toType) { case DataTypes.String: return Convert.ToString(value); case DataTypes.Integer: return Convert.ToInt32(value); case DataTypes.DateTime: return Convert.ToDateTime(value); case DataTypes.Double: return Convert.ToDouble(value); case DataTypes.Default: return value; } } catch { return null; } return null; } } }
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Web.UI; using System.Web.UI.WebControls; /// <summary> /// Summary description for BindingPanel /// </summary> namespace MyControls { public class BindingPanel : Panel { private string _data_member; private object _datasource; #region public string DataMember [Browsable(false)] public string DataMember { get { return _data_member; } set { _data_member = value; } } #endregion #region public object DataSource [Browsable(false)] public object DataSource { get { return _datasource; } set { if ((value == null) || (value is IListSource) || (value is IEnumerable)) { _datasource = value; } else throw new ArgumentException(@"Invalid object. Object must implement IListSource or IEnumerable", "DataSource"); } } #endregion #region private void UpdateFromControlsRecursive (Control control, object row) private void updateFromControlsRecursive (Control control, object row) { foreach (Control ctrl in control.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; object _old_value = null; if (boundField.Length > 0) { if (row is DataRow) _old_value = ((DataRow)row)[boundField]; if (_old_value != idbc.BoundValue) { if (row is DataRow) { if (idbc.BoundValue != null) ((DataRow)row)[boundField] = idbc.BoundValue; else ((DataRow)row)[boundField] = DBNull.Value; } } } } } } #endregion #region private void BindControlsRecursive(Control control, object row) private void bindControlsRecursive(Control control, object row) { foreach (Control ctrl in control.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; if (boundField != null && boundField.Length > 0) { if (row is DataRow) idbc.BoundValue = ((DataRow)row)[boundField]; } } } } #endregion #region private void clearControlsRecursive(Control control) private void clearControlsRecursive(Control control) { foreach (Control ctrl in control.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; if (boundField != null && boundField.Length > 0) idbc.BoundValue = DBNull.Value; } } } #endregion #region private PropertyDescriptor[] GetColumnPropertyDescriptors(object dataItem) private PropertyDescriptor[] GetColumnPropertyDescriptors(object dataItem) { ArrayList props = new ArrayList(); PropertyDescriptorCollection propDescps = TypeDescriptor.GetProperties(dataItem); foreach (PropertyDescriptor pd in propDescps) { Type propType = pd.PropertyType; TypeConverter converter = TypeDescriptor.GetConverter(propType); if ((converter != null) && converter.CanConvertTo(typeof(string))) props.Add(pd); } props.Sort(new PropertyDescriptorComparer()); PropertyDescriptor[] columns = new PropertyDescriptor[props.Count]; props.CopyTo(columns, 0); return columns; } #endregion #region protected virtual IEnumerable GetDataSource() protected virtual IEnumerable GetDataSource() { if (_datasource == null) return null; IEnumerable resolvedDataSource = _datasource as IEnumerable; if (resolvedDataSource != null) return resolvedDataSource; IListSource listDataSource = _datasource as IListSource; if (listDataSource != null) { IList listMember = listDataSource.GetList(); if (listDataSource.ContainsListCollection == false) return (IEnumerable)listMember; ITypedList typedListMember = listMember as ITypedList; if (typedListMember != null) { PropertyDescriptorCollection propDescps = typedListMember.GetItemProperties(null); PropertyDescriptor propertyMember = null; if ((propDescps != null) && (propDescps.Count != 0)) { string dataMember = DataMember; if (dataMember != null) { if (dataMember.Length == 0) propertyMember = propDescps[0]; else propertyMember = propDescps.Find(dataMember, true); if (propertyMember != null) { object listRow = listMember[0]; object list = propertyMember.GetValue(listRow); if (list is IEnumerable) return (IEnumerable)list; } } throw new Exception("A list that coresponds to the selected DataMember cannot be found."); } throw new Exception("The DataSource does not contain any data members to bind to."); } } return null; } #endregion #region public void BindControls(DataRow row) public void BindControls(DataRow row) { bindControlsRecursive(this, row); } #endregion #region public void BindControls(object datasource) public void BindControls(object datasource) { bindControlsRecursive(this, datasource); } #endregion #region public void ClearControls() public void ClearControls() { clearControlsRecursive(this); } #endregion #region public void UpdateFromControls(DataRow row) public void UpdateFromControls(DataRow row) { updateFromControlsRecursive(this, row); } #endregion #region public void UpdateFromControls(object datasource) public void UpdateFromControls(object datasource) { updateFromControlsRecursive(this, datasource); } #endregion #region public override void DataBind() public override void DataBind() { IEnumerable dataSource = null; base.OnDataBinding(EventArgs.Empty); dataSource = GetDataSource(); if (dataSource != null) { PropertyDescriptor[] properties = null; foreach (Control ctrl in this.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; if (boundField.Length > 0) { foreach (object dataItem in dataSource) { properties = GetColumnPropertyDescriptors(dataItem); for (int i = 0; i < properties.Length; i++) { PropertyDescriptor pd = properties[i]; if (boundField.CompareTo(pd.Name) == 0) { object ctlValue = pd.GetValue(dataItem); idbc.BoundValue = pd.Converter.ConvertTo(ctlValue, typeof(string)); } } if (idbc.SingleBind) break; } } } } } } #endregion #region NESTED CLASSES #region private sealed class PropertyDescriptorComparer : IComparer private sealed class PropertyDescriptorComparer : IComparer { public int Compare(object objectA, object objectB) { PropertyDescriptor pd1 = (PropertyDescriptor)objectA; PropertyDescriptor pd2 = (PropertyDescriptor)objectB; return String.Compare(pd1.Name, pd2.Name); } } #endregion #endregion } }
if (_datasource == null) return null; IEnumerable resolvedDataSource = _datasource as IEnumerable; if (resolvedDataSource != null) return resolvedDataSource; IListSource listDataSource = _datasource as IListSource; if (listDataSource != null) {.....}
IList listMember = listDataSource.GetList(); if (listDataSource.ContainsListCollection == false) return (IEnumerable)listMember;
ITypedList typedListMember = listMember as ITypedList; if (typedListMember != null) { PropertyDescriptorCollection propDescps = typedListMember.GetItemProperties(null) if ((propDescps != null) && (propDescps.Count != 0)) { string dataMember = DataMember; if (dataMember != null) { if (dataMember.Length == 0) propertyMember = propDescps[0]; else propertyMember = propDescps.Find(dataMember, true); if (propertyMember != null) { object listRow = listMember[0]; object list = propertyMember.GetValue(listRow); if (list is IEnumerable) return (IEnumerable)list; } } throw new Exception("A list that coresponds to the selected DataMember can not be found."); } throw new Exception("The DataSource does not contains any data members to bind to."); }
private PropertyDescriptor[] GetColumnPropertyDescriptors(object dataItem) { ArrayList props = new ArrayList(); PropertyDescriptorCollection propDescps = TypeDescriptor.GetProperties(dataItem); foreach (PropertyDescriptor pd in propDescps) { Type propType = pd.PropertyType; TypeConverter converter = TypeDescriptor.GetConverter(propType); if ((converter != null) && converter.CanConvertTo(typeof(string))) props.Add(pd); } props.Sort(new PropertyDescriptorComparer()); PropertyDescriptor[] columns = new PropertyDescriptor[props.Count]; props.CopyTo(columns, 0); return columns; }
public override void DataBind() { IEnumerable dataSource = null; base.OnDataBinding(EventArgs.Empty); dataSource = GetDataSource(); if (dataSource != null) { PropertyDescriptor[] properties = null; foreach (Control ctrl in this.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; if (boundField.Length > 0) { foreach (object dataItem in dataSource) { properties = GetColumnPropertyDescriptors(dataItem); for (int i = 0; i < properties.Length; i++) { PropertyDescriptor pd = properties[i]; if (boundField.CompareTo(pd.Name) == 0) { object ctlValue = pd.GetValue(dataItem); idbc.BoundValue = pd.Converter.ConvertTo(ctlValue, typeof(string)); } } if (idbc.SingleBind) break; } } } } } }
dataSource = GetDataSource()
foreach (Control ctrl in this.Controls)
if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; }
foreach (object dataItem in dataSource) { properties = GetColumnPropertyDescriptors(dataItem); }
for (int i = 0; i < properties.Length; i++) { PropertyDescriptor pd = properties[i]; if (boundField.CompareTo(pd.Name) == 0) { object ctlValue = pd.GetValue(dataItem); idbc.BoundValue = pd.Converter.ConvertTo(ctlValue, typeof(string)); } }
private void bindControlsRecursive(Control control, object row) { foreach (Control ctrl in control.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; if (boundField != null && boundField.Length > 0) { if (row is DataRow) idbc.BoundValue = ((DataRow)row)[boundField]; } } } }
private void updateFromControlsRecursive(Control control, object row) { foreach (Control ctrl in control.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; object _old_value = null; if (boundField.Length > 0) { if (row is DataRow) _old_value = ((DataRow)row)[boundField]; if (_old_value != idbc.BoundValue) { if (row is DataRow) { if (idbc.BoundValue != null) ((DataRow)row)[boundField] = idbc.BoundValue; else ((DataRow)row)[boundField] = DBNull.Value; } } } } } }
private void clearControlsRecursive(Control control) { foreach (Control ctrl in control.Controls) { if (ctrl is IDataBound) { IDataBound idbc = (IDataBound)ctrl; string boundField = idbc.BoundColumn; if (boundField != null && boundField.Length > 0) idbc.BoundValue = DBNull.Value; } } }