Memory leak happens when using thread inside another thread

Below sample code has memory leak. If I comment out the two lines inside RefreshTimer_Elapsed, then the memory leak is gone. Does anybody know what's wrong? Thanks for help.

static void RefreshTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Thread innerThread = new Thread(delegate() { }); innerThread.Start(); } static void Main(string[] args) { System.Timers.Timer RefreshTimer = new System.Timers.Timer(); RefreshTimer.Interval = 5000; RefreshTimer.Elapsed += new System.Timers.ElapsedEventHandler(RefreshTimer_Elapsed); RefreshTimer.Start(); for (; ; ) { } }

-------------Problems Reply------------

Are you sure theres a memory leak? Or do you notice that your memory just grows?

Until the garbage collector cleans up all the threads you create, memory will grow, but its not leaking, the garbage collector knows that this is dead memory.

The only way memory "leaks" in a managed enviroment like .NET or java is when you have objects being referenced that are never used or needed. Thats not the case here. You're just creating a bunch of threads and forget about them immediately. As soon as they're no longer referenced by RefreshTimer_Elapsed and the thread stops running then there are no more references and they are free to be cleaned.

You won't see the memory drop until the garbage collector is ready to do a cleanup. You can try and force this but its not generally recommended for performance reasons.

What you see might be just resources not yet reclaimed by the Garbage collector because there is no memory pressure.

Also you have a busy for loop in your Main routine, you probably want a Thread.Sleep statement there for testing, unless this is somehow part of this test...

To force a garbage collection just for your testing only you could replace your for loop with:

while(true)
{
Thread.Sleep(5000);
GC.Collect();
GC.WaitForPendingFinalizers();
}

In general when examining 'memory leaks' or resource problems in managed code I would recommend using a profiler (i.e. Redgate ANTS) and take a closer look at the allocations over time.

I think it's because you keep creating new threads.

The Timer object needs to be disposed!

It appears you are creating new items as there is a recursive call of the code and there may be some kind of loop developing at runtime causing untidy filling of memory with multiple copies of objects as every called item does not fully complete.

RefreshTimer_Elapsed makes a new thread every interval. What kind of work is the anonymous method doing? Is it completing? Every thread you make will get 1MB of virtual memory allocated via Windows.

If you threads never finish, then every interval, you will consume another 1MB of memory.

Category:c# Views:0 Time:2010-12-02

Related post

  • Is spawning threads inside of threads standard or bad practice? 2011-07-20

    In short: Is spawning threads inside of threads a standard procedure or should this be avoided if possible? An example: Think of a parallel spider retrieving and parsing multiple documents at once. In my reasoning the spider in question would need tw

  • What needs to be considered when invoke threads inside a thread in Java or It should be avoided? 2011-11-23

    I am trying to use multithread, a thread pool in fact, for a large group of concurrent tasks. Unfortunately, the tool need to be used by the thread is also implemented by using thread and it is provided by third party. Is it safe to use thread inside

  • Android Memory Leak: Multiple instances of activity in multiple threads 2011-02-26

    I need a "Loading" progressdialog to appear at the start of an activity, and then display some stuff after loading is finished. In my onresume, I have code similar to this: loadThread = true; Thread showDetailsThread = new Thread() { @Override public

  • Internet Explorer 8 memory leak when videos are played inside Iframe using windows media player Plugin 2014-08-18

    We are using IE8 and when we dynamically launch different videos inside the page using Iframe and widows media player we are noticing memory leaks. Step to create: 1) Create sample html file with iframe and a button to launch the video and a button t

  • can a thread which runs indefinatley cause memory leak? 2011-08-17

    I have a j2ee application and inside which i have a class 1 which starts multiple threads T1 ,T2 ,T3.Now i also have a webserrvice.Now I want that that my threads T1,T2,T3,should run indevinitely till the webserver is stopped or my application is und

  • C# interop with COM has memory leak in multithread mode while single thread does not 2012-02-17

    I wrote a C# console application to call a unmanaged COM interop. If I use single thread mode there is no memory leak. But when I use multi thread do interop with COM concurrently, there is huge memory leak. Both way have the same intereop count. I'm

  • Making sense of the 'leaks' command line utility for finding memory leaks 2009-04-17

    I'm working on tracking down some difficult to find memory leaks in my iPhone program. I'm running a quick test on an app which leaks an NSString object with the following intentionally -incorrect- code: -(void)applicationDidFinishLaunching:(NSNotifi

  • PosgresSQL memory leak 2009-08-14

    I am running a system written in c++ that is continuously inserting large amounts of data into my database and at the same time querying the database for updated results. My problem is that the postgres threads started in this process continuously us

  • Detached NSThread memory leak 2011-04-23

    With instruments i got a memory leak in this method of a detached thread : -(void)goToThisUrl:(id) targetUrl { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; if (someCondition) { // Doing some stuff here } // Instruments show memory leak

  • jQuery Mobile and Google Maps API v3 memory leak 2012-01-13

    I have a mobile web app using jQuery Mobile 1 and Google Maps API v3. It appears there is a major memory leak when loading Google Maps inside jQuery Mobile. The memory taken by the Google Maps does not get released when the user leaves the map page.

  • How to prevent memory leaks while cancelling an operation in a worker thread? 2009-01-08

    Currently i am working on a desktop application which consists mathematical analysiss.I am using qt for GUI and project written in c++. When user starts an analysis, i open a worker thread and start a progress bar.Everything is ok up to now, problem

  • Memory leaks in a multi-threaded application using COM & C# 2009-03-13

    I have written a multi thread unmanaged application which uses COM objects in its worker threads. Everything went fine until I started using .NET objects exported as COM to do the work. Playing around with the code and commenting out parts of the .NE

  • .NET Windows Service, threads and garbage collection (possible memory leaks) 2010-04-09

    I am developing a .NET Windows service that is creating a couple of threads and then uses these threads to send print jobs to printers (there is a thread for each printer). I have some issues which sometimes can be fixed by restarting the service. So

  • Memory Leak Looping cfmodule inside cffunction 2011-01-07

    Googlers if you've got a heap dump with a root of coldfusion.runtime.CFDummyComponent read on. Update 2/22/2011 Marc Esher of MXUnit fame found the exact same bug in a different context. His solution involves a large loop over a query solved by going

  • Android Surfaceview Threads and memory leaks 2011-08-29

    Im creating a game in android, and i noticed that the game has a memory leak. Iv managed to isolate the memory leak into a smaller application so that i can see well try and work out, how to fix it. The application uses a surfaceview for its view and

  • Java Threads: Memory Leak 2012-01-23

    I implemented simple server-client chat in Java. Here the source for the server: public class Server { final private static int PORT = 50000; private static class Read extends Thread { private static Socket socket; private static String address; publ

  • Allocation and Deallocation of objects on different threads causing memory leaks. 2012-04-14

    I am using TWRequest to call out to Twitter each time i make a request i get a memory leak inside instruments like so. on [request performRequestWithHandler:] I am tracing out the allocation and deallocation of the class that contains the request. It

  • Memory leak while using Threads 2009-01-27

    I appear to have a memory leak in this piece of code. It is a console app, which creates a couple of classes (WorkerThread), each of which writes to the console at specified intervals. The Threading.Timer is used to do this, hence writing to the cons

  • Component Art WebChart causing Asp.net worker thread memory leak 2009-09-22

    We are using the Component Art WebChart for an online dynamic report and after 1 or 2 page loads of the report it throws the memory into a tail spin. The WP3 worker thread just goes to about 400mb and climbing and finally low memory and the system ei

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.144 (s). 11 q(s)