首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

【一百分结贴】这段代码没法释放内存吗

2013-04-05 
【一百分结贴】这段代码无法释放内存吗?asp.net程序里增加了这段代码后,服务器内存不断增加,直至程序池内存

【一百分结贴】这段代码无法释放内存吗?
asp.net程序里增加了这段代码后,服务器内存不断增加,直至程序池内存超限回收!我不太懂程序,查了很久也不见效,请高手指教(替换前后的全部程序代码在这里):


WebBrowser m_WebBrowser = null; 
    private void _GenerateHtmlToImgImage()
    {
         m_WebBrowser = new WebBrowser();
         System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
         t.Enabled = false;
         t.Interval = 10000;
         t.Tick += t_Tick;
           m_WebBrowser.ScrollBarsEnabled = false;
         t.Enabled = true;
         m_WebBrowser.Navigate(m_Url);
         m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
                   while (m_WebBrowser != null && m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
             Application.DoEvents();
         if (m_WebBrowser != null)
             m_WebBrowser.Dispose();
                 m_WebBrowser = null;
     }
       void t_Tick(object sender, EventArgs e)
     {
         ((System.Windows.Forms.Timer)sender).Stop();
           WebBrowser tmp = m_WebBrowser;
         if (tmp != null)
         {
             m_WebBrowser = null;
             if (tmp.ReadyState != WebBrowserReadyState.Complete)
             {
                 tmp.Stop();
                 tmp.Dispose();
             }
         }


[解决办法]
webbrowser相当于嵌入一个ie啦,本身如果javascript如果编写的不是很完善的话,ie会有内存泄露的。这个几乎无解。除非你能针对特定的问题做hack,相当于对造成内存泄露的脚本做patch。对于ie来说,不正确地循环引用变量,闭包变量被扩大了变量的生命周期等等很多情况下都会造成泄露。
[解决办法]
webbrowser嵌入ie问题
[解决办法]
你这个_GenerateHtmlToImgImage函数是不是频繁调用,或者就是在定时器时间周期内调用?看起来是要实现超时,有点晕乎。
不过这段代码看着不舒服:
WebBrowser tmp = m_WebBrowser;
         if (tmp != null)


         {
             m_WebBrowser = null;
             if (tmp.ReadyState != WebBrowserReadyState.Complete)
             {
                 tmp.Stop();
                 tmp.Dispose();
             }
         }


改一下:

         if (m_WebBrowser != null)
         {
             if (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
             {
                 m_WebBrowser.Stop();
             }
             m_WebBrowser.Dispose();
             m_WebBrowser = null;
         }

[解决办法]
今天抽空又看了下,我说的那个控件也是用webbroswer实现的。效率差不多。
又在codeplex,上看了一个。效果也差不多,你试试看,如果这效率好些就用这个吧。
http://webshooter.codeplex.com/
[解决办法]
asp.net里边使用 System.Windows.Forms.Timer,这个是不想玩儿asp.net啦。

web服务器主要是给很多匿名用户并发访问网页的,它的设计都是与桌面窗体或者业务服务器不同的。

有些软件开发不是asp.net程序员所应该想的。就好像是在大厦门口巡逻的保安不应该以为自己的派出所民警、更不应该以为自己是刑警一样。
[解决办法]
    public Bitmap GenerateHtmlToImgImage()
    {
        Thread m_thread = new Thread(new ThreadStart(_GenerateHtmlToImgImage));
        m_thread.SetApartmentState(ApartmentState.STA);
        m_thread.Start();
        bool success = m_thread.Join(15000);//考虑到处理图片也要时间,增加5秒
        if(success)
            return m_Bitmap;
        else
        {
            m_thread.Abort();//終止线程
            throw new Exception("操作超时");//这里返回的异常,由外部调用者捕获并做其它处理。
        }
        return m_Bitmap;
    }

调用:
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            using(Bitmap bmp = HtmlToImg.GetHtmlToImg("http://www.baidu.com/", 800, 600, 800, 600))


            {
                bmp.Save(Server.MapPath(DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
        catch(Exception ex)
        {
            HttpResponse response = HttpContext.Current.Response;
            response.Write(ex.Message);
            response.Close();
        }
    }


备注,最后3行代码未测试,凭感觉写的。
[解决办法]
前段时间也研究过,也找到了处理的方法,只是当页面太大时,无法获取截图(全白),小型的页面还是可以的

    public class GHtmlToImage
    {
        /// <summary>
        /// 地址
        /// </summary>
        private string _url = "about:blank";
        /// <summary>
        /// 宽
        /// </summary>
        private int _width = 800;
        /// <summary>
        /// 高
        /// </summary>
        private int _height = 600;
        /// <summary>
        /// 缩略图宽
        /// </summary>
        private int _swidth = 320;
        /// <summary>
        /// 缩略图高
        /// </summary>
        private int _sheight = 250;

        private Bitmap _image;
        private Bitmap _thumbnailimage;
        private object _lock = new object();
        private object _lock2 = new object();
        public string Url
        {
            set { _url = value; }
        }
        public int Width
        {
            set { _width = value; }
        }
        public int Height


        {
            set { _height = value; }
        }
        public int SWidth
        {
            set { _swidth = value; }
        }
        public int SHeight
        {
            set { _sheight = value; }
        }
        public Bitmap Image
        {
            get { return _image; }
        }
        public Bitmap ThumbnailImage
        {
            get { return _thumbnailimage; }
        }
        public string DocText
        {
            get;
            set;
        }
        public int runtime
        {
            get;
            set;
        }
        public GHtmlToImage()
        {
            Thread.CurrentThread.CurrentUICulture =  Thread.CurrentThread.CurrentCulture;
        }
        /*
        [STAThreadAttribute]
        static void Main()
        {
            
        }
         */ 
        public void Init()
        {
            
            lock (_lock)
            {
                
                Thread _thread = new Thread(new ThreadStart(_GenerateHtmlToImgImage));
                _thread.Name = "Galsun.Fcx.htmlToImage";
                _thread.SetApartmentState(ApartmentState.STA);


                _thread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
                _thread.Start();
                _thread.Priority = ThreadPriority.Highest;
                TimeSpan ts = new TimeSpan(0, 0, 0, 30);
                _thread.Join();
               
                GC.Collect();
                GC.Collect();
            }
        }
        [STAThread]
        private void _GenerateHtmlToImgImage()
        {
            
            Monitor.Enter(this);
            try
            {
                #region 普通
                WebBrowser _WebBrowser = new WebBrowser();
                _WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
                //_WebBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(WebBrowser_ProgressChanged);
                _WebBrowser.ScrollBarsEnabled = false;
                _WebBrowser.Navigate(_url);
                System.Windows.Forms.Timer _tm = new System.Windows.Forms.Timer();
                _tm.Interval=1000;
                _tm.Tick += new EventHandler(Time_Tick);
                _tm.Enabled = true;
                //Application.SetCompatibleTextRenderingDefault(false);
                Application.Run();
                //Application.EnableVisualStyles();
                
                
                _WebBrowser.Dispose();


                
                #endregion

                #region 新模式
                /*
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                MainForm _form = new MainForm();
                _form._url = _url;
                _form._width = _width;
                _form._height = _height;
                _form._swidth = _swidth;
                _form._sheight = _sheight;
                _form.Width = _width;
                _form.Height = _height;
                Application.Run(_form);
                _image = _form._image;
                _thumbnailimage = _form._thumbnailimage;
                 */ 
                #endregion
            }
            finally
            {
                Monitor.Exit(this);

            }
        }
        private void Time_Tick(object sender, EventArgs e)
        {
            if (runtime > 0)
            {
                Thread.Sleep(100);
                Application.ExitThread();
            }
        }
        private void WebBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            WebBrowser _webBrowser = (WebBrowser)sender;


            if (e.CurrentProgress == e.MaximumProgress && _webBrowser.ReadyState == WebBrowserReadyState.Complete)
            {
                Application.ExitThread();
            }
        }
        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            Monitor.Enter(this);
            try
            {
                
                WebBrowser _webBrowser = (WebBrowser)sender;
                if (_webBrowser.ReadyState == WebBrowserReadyState.Complete)
                {
                    
                    
                    DocText = _webBrowser.DocumentText;
                    if(_webBrowser.Document.Body.ScrollRectangle.Height> _height)
                    {
                        _height=_webBrowser.Document.Body.ScrollRectangle.Height;
                    }
                    _webBrowser.ClientSize = new Size(_width, _height);
                    //_webBrowser.ScrollBarsEnabled = false;
                    //Application.DoEvents();
                    _image = new Bitmap(_webBrowser.Bounds.Width, _webBrowser.Bounds.Height);
                    _webBrowser.BringToFront();
                    _webBrowser.DrawToBitmap(_image, _webBrowser.Bounds);
                    _thumbnailimage = (Bitmap)_image.GetThumbnailImage(_swidth, _sheight, null, IntPtr.Zero);
                    runtime++;


                }
                
            }
            finally
            {
                Monitor.Exit(this);
            }
        }
         
    }

热点排行