Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 635733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:26:12+00:00 2026-05-13T20:26:12+00:00

Memory leak is, when there is unused memory in application and GC can collect

  • 0

Memory leak is, when there is unused memory in application and GC can collect it, normally it occurs if some where in application we keep unwanted strong reference of an object, and GC will able to find the path(Direct and indirect) so it can free this object, but this all are true about reference type that mean in Heap memory allocation.

But what about stack And as far i know GC will not responsible to clean up the stack it will automaticaly clean when function will return.

So my question is that Is there any chance that memory leak will occurs in stack also?, if yes then at what scenario and what are the best practices to avoid this kind of leak.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:26:13+00:00Added an answer on May 13, 2026 at 8:26 pm

    If you are writing recursive functions that keep stack-local references to large data that is not needed in sub-recursive calls, then this is a type of space leak, but it is very unusual to have this be a problem in practice.

    More generally, if you have something like

    Main() {
        var s = ReadInAGiganticString();   // say 10 Megs long
        Server(s.Substring(0,5));          // but I only care about first 5 chars
    }
    Server(s) {
        while(true) { ... }                // but 10M is on stack forever
    }
    

    then this is a kind of stack-space-leak, but again, it is unlikely in practice. The fix is easy:

    Main() {
        var s = ReadInAGiganticString();
        var t = s.Substring(0,5);
        s = null;                         // the fix
        Server(t);
    }
    Server(s) {
        while(true) { ... }
    }
    

    In general, if you ever have a giant variable on the stack right before a call that will last ‘a long time’ and the variable is no longer used, you can null it out to ensure it can be GC’d before going into the ‘long’ call.

    (It is possible that an optimizer may do this for you based on reachability analysis.)

    Note that the above answer is for references to heap-allocated objects that are rooted on the stack. If, on the other hand, you have stack-allocated memory (e.g. a gigantic struct or stackalloc thingie), then the fix is not as easy, but this is even rarer in practice (who ever creates giant structs?).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a memory leak in my application. The memory consumption shoots up after
Is there a fix or a workaround for the memory leak in getpwnam?
In .NET perspective: What is a memory leak ? How can you determine whether
I have a problem with a memory leak in a .NET CF application. Using
It looks like there is a possible memory leak with one of our web
Is there a way/tool to check memory leak of Cocoa Touch project? or I
In the following code, there is a memory leak if Info::addPart1() is called multiple
I do not understand why Instruments/Leaks tells me that there is a memory leak
There are two ways (that I know of) to cause an unintentional memory leak
I've been trying to diagnose a memory leak in an Android application I'm writing.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.