.NET(C#):判断32位程序能使用的内存值

2017-08-05 20:22:52  访问(3450) 赞(0) 踩(0)


相关下载:SlowX.ExamMemory(执行程序x86)  SlowX.ExamMemory(源码x86)  SlowX.ExamMemory(x64位执行程序)     

32位程序,内存值:1747724K = 1706.761M = 1.66676G




64位程序(6G多程序还是没有宕)



SlowXItem

using System;
using System.Collections.Generic;
using System.Text;

namespace SlowX.ExamMemory.Classes
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class SlowXItem
    {
        /// <summary>
        /// 
        /// </summary>
        public SlowXItem()
        {
            

            sList = new List<string>();
            string str = null;

            for (int i = 0; i < 99999; ++i)
            {
                str = GetStr();

                sList.Add(str);
            }
        }

        protected string GetStr()
        {
            return @"
drop table dbo.UTB_WEB_CODE_MAINSORT;
 
-- 版面分类 --
create table dbo.UTB_WEB_CODE_MAINSORT
(
	ID bigint  not null  primary key , 			-- 主分类 -- 
	TheName nvarchar(255)  not null,			-- 名称 --
	TheCode	nvarchar(255)  not null,			-- 代号 --
	ShowSeq	int not null,						-- 显示顺序 --
	CreateTime datetime default (getdate()) not null,	-- 创建时间 --
	UpdateTime datetime default (getdate()) not null,	-- 修改时间 --
);

--+ [表]UTB_WEB_CODE_MAINSORT:主分类 |-- 
EXECUTE sp_addextendedproperty N'MS_Description', N'主分类', N'user', N'dbo', N'table', N'UTB_WEB_CODE_MAINSORT', NULL, NULL;

--+ ID:主分类 |-- 
EXECUTE sp_addextendedproperty N'MS_Description', N'主分类', N'user', N'dbo', N'table', N'UTB_WEB_CODE_MAINSORT', 'column', 'ID';

--+ TheName:名称 |-- 
EXECUTE sp_addextendedproperty N'MS_Description', N'名称', N'user', N'dbo', N'table', N'UTB_WEB_CODE_MAINSORT', 'column', 'TheName';

--+ TheCode:代号 |-- 
EXECUTE sp_addextendedproperty N'MS_Description', N'代号', N'user', N'dbo', N'table', N'UTB_WEB_CODE_MAINSORT', 'column', 'TheCode';

--+ ShowSeq:显示顺序 |-- 
EXECUTE sp_addextendedproperty N'MS_Description', N'显示顺序', N'user', N'dbo', N'table', N'UTB_WEB_CODE_MAINSORT', 'column', 'ShowSeq';

--+ CreateTime:创建时间 |-- 
EXECUTE sp_addextendedproperty N'MS_Description', N'创建时间', N'user', N'dbo', N'table', N'UTB_WEB_CODE_MAINSORT', 'column', 'CreateTime';

--+ UpdateTime:修改时间 |-- 
EXECUTE sp_addextendedproperty N'MS_Description', N'修改时间', N'user', N'dbo', N'table', N'UTB_WEB_CODE_MAINSORT', 'column', 'UpdateTime';



-- 创建[表格:UTB_WEB_CODE_MAINSORT] seq_web_code_mainsort -- 
insert into utb_sys_dual (SequenceName,ID,CreateTime,UpdateTime,TableName) values 
    (
        'seq_web_code_mainsort',1,getdate(),getdate(),'UTB_WEB_CODE_MAINSORT'
    );

 
1、ID:关键字

2、TheName:名称(默认主显名称)

3、TheCode:代号

4、PID:树父节点(0代表根)

5、Path:树路径(如:,0,1,21, 代表 根 -> ID为1的父节点 -> 当前自身Id)

6、ShowSeq:显示顺序

7、CreateTime:创建时间

8、UpdateTime:修改时间

1、ID:关键字
见过有些系统是用StudentId,TeacherId等做关键字。也是一种标准,但不建议。因为一则不如ID直观知道是关键字;二则用通过表名的缩写等才能知道关键字段,在SQL管理器里面写维护代码时候,有反查的效率问题;三则如果外键也要命名,命名规则就冲突了。

2、TheName:主显名称,相当于Name
之前系统开发,遇到过oracle8i下面非数据库保留关键字,oracle9i下成数据库关键字的情况。为了避免这类问题的发生,本人开发习惯是用The当前缀。因此Name常用TheName做命名。同时也是主显名字,就是下拉框默认绑定时候,需要显示的名字。

3、Em(名称)Value:代表数据关系来源是枚举,表达式为(枚举名+Value),如EmUserRoleValue。代表EmUserRole对应的枚举存值。可以通过.net智能感知察觉出对应的值。

4、名称+Id:代表数据关系来自数据表。如MenuId,代表关联缩写为Menu表的字段。

1、BASIC:基础字典表:UTB_(项目名或模块名)_BASIC_(名称)

2、USER:用户权限表 (建议 UTB_SLOWX_USER_ITEM -- 用户表;UTB_SLOWX_USER_GROUP -- 用户组表;UTB_SLOWX_USER_POWER -- 用户权限)

3、LOG:日志表 (建议 UTB_SLOWX_LOG_VISIT -- 访问日志。日志做为一个独立的模块)

举个例子
UTB_SLOWX_BASIC_CITY(基础信息-城市表)

UTB(代表这个object是数据表,之前一个同事跟我说过,如果用TB开头命名,在SqlServer会优先查找master的表,影响效率,建议用UTB打头)

SLOWX(代表SlowX网站项目)

BASIC(代表基础字典表模块)

CITY(基础信息-城市表)

又比如
UTB_SLOWX_USER_POWER(用户权限表)
UTB_SLOWX_USER_ITEM(用户表)
UTB_SLOWX_USER_GROUP(用户组)

同样,对应的序列号命名为
SEQ_SLOWX_BASIC_CITY

开发人员无需反查文档,就能直接知道序列号是SEQ_SLOWX_BASIC_CITY

(补充说明:建议控制表名长度在25位内)

不过不一定要严格按照4段结构进行命名,有些简单的可以用3段,比如考试表
UTB_EXAM_CLASS(班级)
UTB_EXAM_COURSE(课程)

由于Oracle数据库只支持30位的字节长,SqlServer不受限制。为了保证系统能在多种数据库进行部署,建议数据库中的表名、字段名和object名等都控制在25个字以内(不是30,因为有时候你做连表操作,两张表都用同样的Name字段,肯定有一个字段需要别名成(比如)I__Name。如果本身字段名已经30长,在填充别名,会发生字段过长的错误)

正所谓没有规矩不成方圆

良好统一的命名规则,有助于团队代码风格统一,团队成员之间相互协作,更重要的是对系统有一种可持续的维护和升级。

数据库命名规则主要有:
1、数据表和序列等的命名
2、字段的命名
(不断更新中……)
";
          
        }

        public List<string> sList = null;
    }
}


SlowXContainer

using System;
using System.Collections.Generic;
using System.Text;

namespace SlowX.ExamMemory.Classes
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class SlowXContainer
    {
        /// <summary>
        /// 
        /// </summary>
        public SlowXContainer()
        {

        }

        protected List<SlowXItem> TheList = new List<SlowXItem>();

        /// <summary>
        /// 
        /// </summary>
        protected int AddCount = 0;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public void AddItem(SlowXItem item)
        {
            ++AddCount;
            TheList.Add(item);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool IsAdd()
        {
            if (AddCount < 99999)
                return true;

            return false;
        }
    }
}


主程序

using System;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using SlowX.ExamMemory.Classes;

namespace SlowX.ExamMemory
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton_获得内存信息_Click(object sender, EventArgs e)
        {
            List<SlowXContainer> scList = new List<SlowXContainer>();

            SlowXContainer sc = new SlowXContainer();
            scList.Add(sc);
            bool isBreak = false;

            SlowXItem si = null;

            for (uint i = 0; i < int.MaxValue; ++i)
            {
                try
                {
                    si = new SlowXItem();
                    if (!sc.IsAdd())
                    {
                        sc = new SlowXContainer();
                        scList.Add(sc);
                    }

                    sc.AddItem(si);
                }
                catch (Exception err)
                {
                    richTextBox1.Text = "执行序号" + i.ToString() + System.Environment.NewLine
                        + "执行异常:" + err.Message;

                    isBreak = true;
                }

                if (isBreak)
                    break;
            }

            //try
            //{
            //    string str = GetM();
            //    MessageBox.Show(str);
            //}
            //catch (Exception err)
            //{
            //    richTextBox1.Text += System.Environment.NewLine +
            //        System.Environment.NewLine +
            //        err.Message;
            //}
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected string GetM()
        {
            try
            {

                StringBuilder theResult = new StringBuilder();

                GetWorkingSet8Process(theResult, Process.GetCurrentProcess());

                return theResult.ToString();
            }
            catch(Exception err)
            {
                return "获得内存值异常:" + err.Message;
            }
        }

        public void GetWorkingSet8Process(StringBuilder theResult, Process processV)
        {


            using (var p1 = new PerformanceCounter("Process", "Working Set - Private", processV.ProcessName))
            {
                using (var p2 = new PerformanceCounter("Process", "Working Set", processV.ProcessName))
                {
                    theResult.AppendLine(processV.Id.ToString());

                    //注意除以CPU数量
                    theResult.AppendLine(string.Format("{0}{1:N} KB", "工作集(进程类)", processV.WorkingSet64 / 1024));
                    theResult.AppendLine(string.Format("{0}{1:N} KB", "工作集 ", processV.WorkingSet64 / 1024));
                    theResult.AppendLine(string.Format("{0}{1:N} KB", "私有工作集 ", p1.NextValue() / 1024));
                    theResult.AppendLine(string.Format("{0};内存(专用工作集){1:N};PID:{2};程序名:{3}", DateTime.Now, p1.NextValue() / 1024, processV.Id.ToString(), processV.ProcessName));

                }
            }
        }

        private void toolStripButton_获得当前线程ID_Click(object sender, EventArgs e)
        {
            try
            {
                richTextBox1.Text = Process.GetCurrentProcess().Id.ToString();
            }
            catch (Exception err)
            {
                richTextBox1.Text = err.Message;
            }
        }
        

    } 
}




上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)