<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IBlog &#187; Compact Framework</title>
	<atom:link href="http://blog.objectpattern.com/category/compact-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.objectpattern.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sat, 17 Apr 2010 18:49:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Knowing memory usage from Compact Framework application</title>
		<link>http://blog.objectpattern.com/compact-framework/knowing-memory-usage-from-compact-framework-application/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=knowing-memory-usage-from-compact-framework-application</link>
		<comments>http://blog.objectpattern.com/compact-framework/knowing-memory-usage-from-compact-framework-application/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 18:50:38 +0000</pubDate>
		<dc:creator>Himanshu</dc:creator>
				<category><![CDATA[Compact Framework]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Windows Plateform]]></category>

		<guid isPermaLink="false">/post/Knowing-memory-usage-from-Compact-Framework-application.aspx</guid>
		<description><![CDATA[For any handheld device application, memory usage is an important element to observer. .NET Compact framework have very less code that gives information about physical or virtual memory. But using native libraries &#8211; by doing pInvoke, one can retrieve memory usage statistics.
There can be more than one level of report that can be captured. Today, [...]]]></description>
			<content:encoded><![CDATA[<p>For any handheld device application, memory usage is an important element to observer. .NET Compact framework have very less code that gives information about physical or virtual memory. But using native libraries &#8211; by doing pInvoke, one can retrieve memory usage statistics.</p>
<p>There can be more than one level of report that can be captured. Today, lets start with summary. GlobalMemoryStatus function in coredll can help identifying how much total physical memory available in the device and how much of it is free. Same way, how much of virtual memory available to process and how much of it is free.</p>
<p>Lets see how can we use GlobalMemoryStatus. We will have to define a class which will be passed to function and will be filled by GlobalMemoryStatus function. Layout of it is defined as structure by MS team, but its alright to use class here. By having it as class, we will be able to write default constructor unlike structure in C#.</p>
<pre class="csharpcode">    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    <span class="kwrd">internal</span> <span class="kwrd">class</span> MemoryStatus
    {
        <span class="kwrd">public</span> <span class="kwrd">uint</span> dwLength;
        <span class="kwrd">public</span> <span class="kwrd">uint</span> dwMemoryLoad;
        <span class="kwrd">public</span> <span class="kwrd">uint</span> ullTotalPhys;
        <span class="kwrd">public</span> <span class="kwrd">uint</span> ullAvailPhys;
        <span class="kwrd">public</span> <span class="kwrd">uint</span> ullTotalPageFile;
        <span class="kwrd">public</span> <span class="kwrd">uint</span> ullAvailPageFile;
        <span class="kwrd">public</span> <span class="kwrd">uint</span> ullTotalVirtual;
        <span class="kwrd">public</span> <span class="kwrd">uint</span> ullAvailVirtual;
        <span class="kwrd">public</span> <span class="kwrd">uint</span> ullAvailExtendedVirtual;
        <span class="kwrd">public</span> MemoryStatus()
        {
            <span class="kwrd">this</span>.dwLength = (<span class="kwrd">uint</span>)Marshal.SizeOf(<span class="kwrd">typeof</span>(MemoryStatus));
        }
    }
</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>Now, lets define extern method for GlobalMemoryStatus</p>
<pre class="csharpcode">        [<span class="kwrd">return</span>: MarshalAs(UnmanagedType.Bool)]
        [DllImport(<span class="str">"coredll.dll"</span>, CharSet = CharSet.Auto, SetLastError = <span class="kwrd">true</span>, EntryPoint = <span class="str">"GlobalMemoryStatus"</span>)]
        <span class="kwrd">static</span> <span class="kwrd">extern</span> <span class="kwrd">bool</span> GlobalMemoryStatusWinCE([In, Out] MemoryStatus lpBuffer);
</pre>
<p>And here is how we can call the method</p>
<pre class="csharpcode">    MemoryStatus internalReport = <span class="kwrd">new</span> MemoryStatus();
    <span class="kwrd">if</span> (GlobalMemoryStatusWinCE(internalReport))</pre>
<pre class="csharpcode">
]]></content:encoded>
			<wfw:commentRss>http://blog.objectpattern.com/compact-framework/knowing-memory-usage-from-compact-framework-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
