逆风飞扬  
顺风也好,逆风也罢,都是驾驭风力而已,君子性非异也,善假与物也。逆风飞扬,也能飞得更高,更远!

在手机编程中,有时候内存空间不足无法正常启动程序,或者存储空间不足的时候还要去存储一些东西,这个时候程序就可能出现异常问题。为了保证程序的稳定性,我们就需要在程序中主动的判断当前的内存使用情况,避免异常的出现。我们通常使用如下两个api来得到当前内存的使用情况。
1.GetDiskFreeSpaceEx(),用来得到存储空间的使用情况。
2.GlobalMemoryStatus(),用来得到RAM的使用情况。
具体的使用情况参考下面的CheckMemory类。这个类是根据opennetcf的源码整理而成,仅供学习参考。

 

    /// <summary>
    
/// CheckMemory 的摘要说明,以下代码是根据opennetcf的源码整理而成,仅供学习参考。
    
/// </summary>

    public class CheckMemory {

        [DllImport(
"coredll", EntryPoint = "GlobalMemoryStatus", SetLastError = false)]
        
internal static extern void GlobalMemoryStatusCE(out MemoryStatus msce);
        [DllImport(
"coredll")]
        
private static extern bool GetDiskFreeSpaceEx(string directoryName, ref long freeBytesAvailable, ref long totalBytes, ref long totalFreeBytes);

        
public struct MemoryStatus {
            
internal uint dwLength;
            
/// <summary>
            
/// Specifies a number between 0 and 100 that gives a general idea of current memory utilization, in which 0 indicates no memory use and 100 indicates full memory use.
            
/// </summary>

            public int MemoryLoad;
            
/// <summary>
            
/// Indicates the total number of bytes of physical memory.
            
/// </summary>

            public int TotalPhysical;
            
/// <summary>
            
/// Indicates the number of bytes of physical memory available.
            
/// </summary>

            public int AvailablePhysical;
            
/// <summary>
            
/// Indicates the total number of bytes that can be stored in the paging file. Note that this number does not represent the actual physical size of the paging file on disk.
            
/// </summary>

            public int TotalPageFile;
            
/// <summary>
            
/// Indicates the number of bytes available in the paging file.
            
/// </summary>

            public int AvailablePageFile;
            
/// <summary>
            
/// Indicates the total number of bytes that can be described in the user mode portion of the virtual address space of the calling process.
            
/// </summary>

            public int TotalVirtual;
            
/// <summary>
            
/// Indicates the number of bytes of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process.
            
/// </summary>

            public int AvailableVirtual;
        }


        
public struct DiskFreeSpace {
            
/// <summary>
            
/// The total number of free bytes on the disk that are available to the user associated with the calling thread.
            
/// </summary>

            public long FreeBytesAvailable;
            
/// <summary>
            
/// The total number of bytes on the disk that are available to the user associated with the calling thread.
            
/// </summary>

            public long TotalBytes;
            
/// <summary>
            
/// The total number of free bytes on the disk.
            
/// </summary>

            public long TotalFreeBytes;
        }


        
public CheckMemory() {
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
public void CheckMemoryState() {
            
string directoryName = "\\Storage"//mobile 2003 for smartphone 的系统。
            DiskFreeSpace result = new DiskFreeSpace();
            
//根据得到的内存存储空间使用情况,再做进一步的处理了,
            bool flag = GetDiskFreeSpaceEx(directoryName, ref result.FreeBytesAvailable, ref result.TotalBytes, ref result.TotalFreeBytes));

            
//RAM内存使用情况
            MemoryStatus ms = new MemoryStatus();            
            GlobalMemoryStatusCE( 
out ms );
            
        }

    }

 

posted on 2007-10-30 17:17  逆风飞扬  阅读(1002)  评论(1编辑  收藏  举报