The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Make a subtle memory leak

Last post 03-11-2013 12:10 AM by ekolis. 20 replies.
Page 1 of 1 (21 items)
Sort Posts: Previous Next
  • 10-12-2012 11:59 PM

    • Ben L.
    • Top 25 Contributor
    • Joined on 12-21-2010
    • HELP I'M TRAPPED IN A COMMUNITY SERVER FACTORY
    • Posts 1,386

    Make a subtle memory leak

    Your choice of language. Go.
  • 10-15-2012 3:05 PM In reply to

    Re: Make a subtle memory leak

    List<object> myList = new List<object>();
    for (int i = 0; i < int.MaxValue; i++)
        myList.Add(new object());
    This is a signature. It's not a good one, but it's still a signature.
  • 10-15-2012 4:20 PM In reply to

    Re: Make a subtle memory leak

     var exportBody = "";

    //you can't leak in a managed language, but you can fill that fucker right the hell up
    for (var i=0; i < fullDataRecords.length; i++) {
       var _record = fullDataRecords[i];
       for (var key in _record) {
          exportBody += _record[key] +' | ';
       }
    }

    boomzilla: I think the obvious answer is for everyone to just stop programming.

  • 10-15-2012 8:25 PM In reply to

    • Ben L.
    • Top 25 Contributor
    • Joined on 12-21-2010
    • HELP I'M TRAPPED IN A COMMUNITY SERVER FACTORY
    • Posts 1,386

    Re: Make a subtle memory leak

    mott555:
    List<object> myList = new List<object>();
    for (int i = 0; i < int.MaxValue; i++)
        myList.Add(new object());
    In an ideal language, object would be a zero-width struct, which would make the last line of that increment length and do nothing else.
  • 10-18-2012 2:52 PM In reply to

    Re: Make a subtle memory leak

    void TrimLeft(char *s)
    {
        char *n = new char[strlen(s)];
        char *p = n;
        do {
            if (*s != ' ')
                *p++ = *s;
            ++s;
        } while (*s);
        strcpy(s,n);
        delete[ n;
    }
    
    void foo(vector input)
    {
        for (vector::iterator i = input.begin(), end = input.end(); i != end; ++i)
        {
            try {
                TrimLeft(*i);
            } catch (...) {} /* BUGID 17839 */
        }
    }
    
  • 10-21-2012 3:50 PM In reply to

    Re: Make a subtle memory leak

    Oldie but goldie (in JS) :
    
    
     // init my shiny ajax page
     function initPage() {
    
      // setup event handlers
      $("#foobuttontop").click(function() { doFoo("top"); });
      $("#foobuttonbottom").click(function() { doFoo("bottom"); });
      // ...
    
      // all handlers are set up - and we're do- oh, wait, we'll still have to do some preprocessing involving this one really huge object...
      var hugeObject = createHugeObject();
      hugeObject.doPreprocessing();
    
      // alright. NOW we're really done. Good thing we just stored the huge object in a local variable so it can be GCed now...
     }
    
    
  • 10-22-2012 5:33 AM In reply to

    Re: Make a subtle memory leak

    PSWorx:
    Oldie but goldie (in JS) :
     

    Explain to the incompetent dummies how this is a memory leak.

    Because I don't see it.


    boomzilla: I think the obvious answer is for everyone to just stop programming.

  • 10-22-2012 9:44 AM In reply to

    • ammoQ
    • Top 10 Contributor
    • Joined on 04-13-2005
    • Vienna.Austria.Europe.Earth
    • Posts 3,530

    Re: Make a subtle memory leak

    I guess some JS implementations reference the huge object in the closures of the event handlers.

    dhromed:

    PSWorx:
    Oldie but goldie (in JS) :
     

    Explain to the incompetent dummies how this is a memory leak.

    Because I don't see it.

     

     

    beanbag girl 4ever ... or maybe Astah girl?
  • 10-22-2012 9:55 AM In reply to

    Re: Make a subtle memory leak

    ammoQ:

    I guess some JS implementations reference the huge object in the closures of the event handlers.

    dhromed:

    PSWorx:
    Oldie but goldie (in JS) :
     

    Explain to the incompetent dummies how this is a memory leak.

    Because I don't see it.

     

     

    According to ECMAScript spec, all conforming implementations have to. This is so you can do stuff like
    
     function makeClosure() {
      var a = 5;
      return function(x) {
       eval(x); // no explicit reference to 'a' here ...
      };
     }
    
     var closure = makeClosure();
     closure("console.info(a)"); // ... still prints 5.
    


    ... that's not saying people haven't found some clever heuristics to work around that problem by today.
  • 10-22-2012 2:11 PM In reply to

    Re: Make a subtle memory leak

    The How-smart-is-my-garbage-collector test:

    class WidgetContainer;

    class Widget
    {
      private:
        int id;
        WidgetContainer *owner;

      public:
        Widget(int n, WidgetContainer *wc)
        {
          id = n;
          owner = wc
        }
    }

    class WidgetContainer
    {

      private:

        List<Widget> widgets;



      public:

        void add(Widget w)
        {
          widgets.add(w);
        }
    }



    void test()
    {
      WidgetContainer wc = new WidgetContainer();

      for (int i = 0; i < 1000000; i++) {
        wc.add(new Widget(i, &wc));
      }
    }

    I think most modern runtimes will figure this one out, but I'm not 100% positive on that.

  • 10-23-2012 8:57 AM In reply to

    Re: Make a subtle memory leak

    curtmack:
    I think most modern runtimes will figure this one out, but I'm not 100% positive on that.
    You mean the circular references? Isn't that the whole point of using garbage collectors and not reference counting?
  • 11-01-2012 4:49 PM In reply to

    Re: Make a subtle memory leak

    I choose VB.NET. Because it's on this machine.

    Public Class LeakEverything

    Public Event Generic()
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For i As Integer = 0 To 10000
    Dim that As New Thing()
    AddHandler Me.Generic, AddressOf that.HandleGenericEvent
    Next
    GC.Collect()'Just to make sure...
    End Sub
    End Class

    Public Class Thing

    Private m_junk As Byte()
    Public Sub New()
    ReDim m_junk(1024)
    End Sub
    Public Sub HandleGenericEvent()
    'Do Nothing
    End Sub
    End Class

    It's more subtle if the byte array is removed, of course.

  • 11-28-2012 11:39 PM In reply to

    Re: Make a subtle memory leak

    PSWorx:
    According to ECMAScript spec, all conforming implementations have to. This is so you can do stuff like...<snip&rt;
    That's awesome and terrifying and and I don't even need to read the rest of the thread to vote that up as "best subtle memory leak". #slowclap
  • 11-29-2012 4:25 AM In reply to

    • PJH
    • Top 10 Contributor
    • Joined on 02-14-2007
    • Newcastle, UK
    • Posts 3,131

    Re: Make a subtle memory leak

    savar:
    <snip&rt;
    What the fuck is that? "Right than"? There's a preview tab for a reason.
    3 logicians go into a bar.; the barman says ‘Would you all like a drink?’.
    The first says 'I’m not sure', the second says 'I’m not sure', and the third says 'Yes'.
  • 11-29-2012 5:48 AM In reply to

    Re: Make a subtle memory leak

    Ben L.:
    mott555:
    List<object> myList = new List<object>();
    for (int i = 0; i < int.MaxValue; i++)
        myList.Add(new object());
    In an ideal language, object would be a zero-width struct, which would make the last line of that increment length and do nothing else.

    Sorry, but you need to account for new object() != new object().

    The following lists are different:
    listA = new List();
    listA.Add(new object());
    listA.Add(new object());
    
    listB = new List();
    item = new object();
    listB.Add(item);
    listB.Add(item);
    
  • 12-10-2012 1:59 AM In reply to

    Re: Make a subtle memory leak

    savar:
    PSWorx:
    According to ECMAScript spec, all conforming implementations have to. This is so you can do stuff like...<snip&rt;
    That's awesome and terrifying and and I don't even need to read the rest of the thread to vote that up as "best subtle memory leak". #slowclap
    You don't have to use JS regularly, do you? :) Everyone that's ever written any large script in JS should know that all variable definitions are implicitly moved to the top of the enclosing function. If you don't, you don't know the basics of the language you're using and you've definitely never used JSLint.
  • 01-09-2013 4:56 PM In reply to

    Re: Make a subtle memory leak

    PSWorx:
    Oldie but goldie (in JS) :

     // init my shiny ajax page
     function initPage() {
    
      // setup event handlers
      $("#foobuttontop").click(function() { doFoo("top"); });
      $("#foobuttonbottom").click(function() { doFoo("bottom"); });
      // ...
    
      // all handlers are set up - and we're do- oh, wait, we'll still have to do some preprocessing involving this one really huge object...
      var hugeObject = createHugeObject();
      hugeObject.doPreprocessing();
    
      // alright. NOW we're really done. Good thing we just stored the huge object in a local variable so it can be GCed now...
     }
    
    I assume here the intention is that doFoo is a function local to initPage, otherwise this won't actually work in Firefox since if it can't find any uses of local variables in nested functions it won't bother saving the scope.

     

  • 03-01-2013 5:29 PM In reply to

    • ekolis
    • Top 75 Contributor
    • Joined on 01-09-2008
    • Cincinnati, OH, USA
    • Posts 591

    Re: Make a subtle memory leak

    for (i = int.MinValue; i <= int.MaxValue; i++)
    new byte[int.MaxValue];

    Nobody said I was good at subtlety.
    I'm Spark Mandrill, and I'll... hey... can I... what, it BOUNCES?... 'kay, I'm splodin' now.
    Filed under:
  • 03-01-2013 6:52 PM In reply to

    • Ben L.
    • Top 25 Contributor
    • Joined on 12-21-2010
    • HELP I'M TRAPPED IN A COMMUNITY SERVER FACTORY
    • Posts 1,386

    Re: Make a subtle memory leak

    ekolis:
    for (i = int.MinValue; i <= int.MaxValue; i++)
    new byte[int.MaxValue];

    Nobody said I was good at subtlety.
    That doesn't leak any memory. It just makes a lot of garbage for the GC to pick up.
  • 03-08-2013 4:38 PM In reply to

    Re: Make a subtle memory leak

    NeilRashbrook:
    PSWorx:

    Oldie but goldie (in JS):

     // init my shiny ajax page
     function initPage() {
    
      // setup event handlers
      $("#foobuttontop").click(function() { doFoo("top"); });
      $("#foobuttonbottom").click(function() { doFoo("bottom"); });
      // ...
    
      // all handlers are set up - and we're do- oh, wait, we'll still have to do some preprocessing involving this one really huge object...
      var hugeObject = createHugeObject();
      hugeObject.doPreprocessing();
    
      // alright. NOW we're really done. Good thing we just stored the huge object in a local variable so it can be GCed now...
     }
    

    I assume here the intention is that doFoo is a function local to initPage, otherwise this won't actually work in Firefox since if it can't find any uses of local variables in nested functions it won't bother saving the scope.

    Indeed. You'd need to have something that directly or indirectly equates to using eval in the scope of initPage for that scope to qualify to 'always' be retained, e.g., eval itself or the string based versions of setTimout and setInterval.

  • 03-11-2013 12:10 AM In reply to

    • ekolis
    • Top 75 Contributor
    • Joined on 01-09-2008
    • Cincinnati, OH, USA
    • Posts 591

    Re: Make a subtle memory leak

    English:

    (Enter a memory leak, stage right.)

    MEMORY LEAK: Ahem! Good sirs, I do beg your pardon, but I will be absconding with some of your memory.

    (MEMORY LEAK absconds with 4 GIGABYTES of RAM)

    CHORUS: O where is our RAM? O where is our RAM? O where O where O where O where O where... is our poor RAM?
    I'm Spark Mandrill, and I'll... hey... can I... what, it BOUNCES?... 'kay, I'm splodin' now.
Page 1 of 1 (21 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems