----------------------------------c#如何释放内存?----------------------------------

by_封爱 版主 2015-07-09 01:41:07
我来描述下背景.可能我所谓的释放跟你们想象的不太一样.

你们可能觉得Dispose下或者using下甚至GC.Collect就可以释放内存..

但是我要说的不是一回事..

一个win程序加载的时候

我们假设有100个静态的List<T> =>t,窗体中有一个全局的timers.timer=>t以及List<int>=>lx
代码也简单

t.Elapsed += (o, e) =>
{
//可能通过一些逻辑判断从静态的list里取值并且给UI的文本框赋值
var list=static.t;
textbox1.BeginInvoke(new action(()=>{
textbox1.text=list.find(d=>d.name="小明").age;
lx.add(1);//这句我认为可能导致内存增加..所以有了下面一句
if (lx.Count >= 2000)
{
lx.RemoveRange(0, lx.Count - 2000);
}
}));
}

看起来似乎好像大概也没什么问题..静态变量加载的时候就有值了 lx集合也判断了.

但是这个程序运行很久之后 从任务管理器中看到这个exe的内存很高 并且持续++++

甚至运行2个月之后导致内存溢出或者其他无法捕获的错误(多内存错误)

所以我如何才能让我的exe得到及时的释放?
...全文
849 25 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
猪猪猪爱的 2015-07-13
  • 打赏
  • 举报
回复
可能是两个匿名lambda表达式的递归产生的,有可能死循环了 换一个写法 直接声明委托的方式 然后手动释放委托 别用表达式套表达式
於黾 2015-07-13
  • 打赏
  • 举报
回复
我写了一个测试 就是新建一个winform工程,默认有个主窗体 什么代码都不加 在我的笔记本上运行(直接运行exe而不是F5调试),它的内存就会不断的增加 而放到台式机上就完全不存在内存增长的问题
dylike 2015-07-13
  • 打赏
  • 举报
回复
引用DSAPI1.0.0.7 timer每N分钟或1分钟调用一次: DSAPI.内存相关.清理一下
程序员鼓励师 2015-07-12
  • 打赏
  • 举报
回复
fly4free 2015-07-11
  • 打赏
  • 举报
回复
dispose用于释放native代码创建的资源,从托管c++角度来讲,就会调用对象的!打头的析构函数 你给的代码跟这个没关系啊
sinat_29725645 2015-07-11
  • 打赏
  • 举报
回复
我感觉C语言就够学好久好久了
老李家的小二 2015-07-10
  • 打赏
  • 举报
回复
出现错误时,没有提示是第几行吗?
老李家的小二 2015-07-10
  • 打赏
  • 举报
回复
整错了
老李家的小二 2015-07-10
  • 打赏
  • 举报
回复
if (lx.Count >= 2000) { lx.RemoveRange(0, lx.Count - 2000); } lx.RemoveRange(0, lx.Count - 2000) -> lx.RemoveRange(0, 2000 - 2000) -> lx.RemoveRange(0,0) 是这样吗?
showjim 2015-07-09
  • 打赏
  • 举报
回复
问题应该不在这段代码上
showjim 2015-07-09
  • 打赏
  • 举报
回复
引用 14 楼 sbwwkmyd 的回复:
lx = lx.RemoveRange(0, lx.Count - 2000);
不好意思,搞错对象了,这里应该没有问题。
showjim 2015-07-09
  • 打赏
  • 举报
回复
lx = lx.RemoveRange(0, lx.Count - 2000);
Poopaye 2015-07-09
  • 打赏
  • 举报
回复
或者直接分配一个新的List,这样原List就能释放了
Poopaye 2015-07-09
  • 打赏
  • 举报
回复
.Net自带的List一旦分配了内存,无论你是Remove还是Clear都不会释放任何空间,如果有内存问题请自己实现一个容器
minidick 2015-07-09
  • 打赏
  • 举报
回复
试着把list.Add(model)注释掉,内存还是在缓慢增长。 问题出在不停地产生新的string,并赋值给model。及时的GC.Collect(),内存就不增长了。
失落的神庙 2015-07-09
  • 打赏
  • 举报
回复
BeginInvoke 这种情况用Invoke 同步委托吧。
by_封爱 版主 2015-07-09
  • 打赏
  • 举报
回复
以上程序中 内存会一直涨.....涨到一个点之后就不涨了..具体不清楚 跟什么有关系. (我猜是因为RemoveRange但是没有具体测试) 并且在过程中执行了list.clear的操作.内存还是一样没有变化...
引用 5 楼 duanzi_peng 的回复:
var list,,,,,, -》 这句不应该 搞到外部么? 每次计时使用 都应该clear吧
所以这个clear在哪也没什么用.
by_封爱 版主 2015-07-09
  • 打赏
  • 举报
回复
引用 6 楼 xuzuning 的回复:
虽然我说不出原因,但我知道只要使用了 timers,那么就无一例外的造成内存占用慢慢的增加 timers.timer = 50 通常会在两小时内耗尽所有可用内存
那我还想2s一采集 那我用什么................就一个定时器 难道我还调度不成? 感觉小题大做啊... 或者还有其他的说明方案? 据说timer.timer是线程池管理的啊 没道理耗尽吧
by_封爱 版主 2015-07-09
  • 打赏
  • 举报
回复
为了更好的说明问题.我写了一个DEMO做测试..

public class common
    {
        public static List<int> a = new List<int>();
        public static List<int> b = new List<int>();
        public static List<int> c = new List<int>();
        public static List<int> d = new List<int>();
    }
   public class model
   {
       public string a { get; set; }
       public string b { get; set; }
       public string c { get; set; }
       public string d { get; set; }
   }
UI上拖4个文本框就够了. 代码如下

        public List<model> list { get; set; }
        private void Form1_Load(object sender, EventArgs e)
        {
            list = new List<model>();
            common.a = Enumerable.Range(1, 100).ToList();
            common.b = Enumerable.Range(1, 100).ToList();
            common.c = Enumerable.Range(1, 100).ToList();
            common.d = Enumerable.Range(1, 100).ToList();
            var timer = new System.Timers.Timer(666);
            timer.Enabled = true;
            timer.Elapsed += (a, b) =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    var model = new model();
                    model.a = common.a.OrderBy(d => Guid.NewGuid()).FirstOrDefault().ToString();
                    model.b = common.b.OrderBy(d => Guid.NewGuid()).FirstOrDefault().ToString();
                    model.c = common.c.OrderBy(d => Guid.NewGuid()).FirstOrDefault().ToString();
                    model.d = common.d.OrderBy(d => Guid.NewGuid()).FirstOrDefault().ToString();

                    textBox1.Text = model.a;
                    textBox2.Text = model.b;
                    textBox3.Text = model.c;
                    textBox4.Text = model.d;

                    list.Add(model);
                    if (list.Count >= 2000)
                    {
                        list.RemoveRange(0, list.Count - 2000);
                    }
                }));
            };
        }
xuzuning 2015-07-09
  • 打赏
  • 举报
回复
虽然我说不出原因,但我知道只要使用了 timers,那么就无一例外的造成内存占用慢慢的增加 timers.timer = 50 通常会在两小时内耗尽所有可用内存
加载更多回复(5)
-------------------------------------------------------------------------------- 飞鸽传书 (IP Messenger) Ver2.06 Copyright (C) 1996-2004 SHIROUZU Hiroaki All Rights Reserved. -------------------------------------------------------------------------------- 目录: 1. 软件简介 2. 许可协议 3. 系统要求 4. 使用说明 5. 相关信息 6. 广域网设置(广播设置) 7. 补充 8. 支持 9. 更新历史 10. 感谢 ================================================================================ 重要说明: comctl32.dll(公共控件) 要求 4.71 或以上版本 更多信息请参见 "系统要求" ================================================================================ -------------------------------------------------------------------------------- 1. 软件简介 - IPMsg 是一款局域网内即时通信软件, 基于 TCP/IP(UDP). 可运行于多种操作平台(Win/Mac/UNIX/Java), 并实现跨平台信息交流. - 不需要服务器支持. - 支持文件/文件夹的传送 (2.00版以上) - 通讯数据采用 RSA/Blofish 加密 (2.00版以上) - 十分小巧, 简单易用, 而且你可以完全免费使用它 - 目前已有的版本包括: Win32, Win16, MacOS, MacOSX, X11, GTK, GNOME, Java 等, 并且公开源代码. 请查看以下地址以获得相关信息: http://www.ipmsg.org/ -------------------------------------------------------------------------------- 2. 许可协议 (BSD License) Copyright (c) 1996-2004 SHIROUZU Hiroaki All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the SHIROUZU Hiroaki nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRI

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧