|
Make a subtle memory leak
Last post 06-07-2013 5:31 AM by dhromed. 27 replies.
-
-
-
mott555


- Joined on 08-10-2009
- Missouri
- Posts 581
|
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.
|
|
-
-
-
Ben L.


- Joined on 12-22-2010
- HELP I'M TRAPPED IN A COMMUNITY SERVER FACTORY
- Posts 1,875
|
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.
|
|
-
-
-
PSWorx


- Joined on 04-28-2006
- Posts 1,305
|
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...
}
|
|
-
-
dhromed


- Joined on 04-13-2005
- Dutchland
- Posts 10,305
|
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.
|
|
-
-
ammoQ


- 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?
|
|
-
-
PSWorx


- Joined on 04-28-2006
- Posts 1,305
|
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.
|
|
-
-
curtmack


- Joined on 10-25-2007
- Posts 148
|
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.
|
|
-
-
PSWorx


- Joined on 04-28-2006
- Posts 1,305
|
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?
|
|
-
-
Evilweasil


- Joined on 05-30-2012
- Posts 38
|
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.
|
|
-
-
savar


- Joined on 08-02-2006
- Posts 394
|
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
|
|
-
-
PJH


- Joined on 02-14-2007
- Newcastle, UK
- Posts 3,197
|
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'.
|
|
-
-
PleegWat


- Joined on 06-20-2009
- Posts 20
|
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);
|
|
-
-
anonymous_guy


- Joined on 12-09-2012
- Posts 41
|
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.
|
|
-
-
NeilRashbrook


- Joined on 10-07-2012
- Posts 5
|
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.
|
|
-
-
ekolis


- Joined on 01-09-2008
- Cincinnati, OH, USA
- Posts 597
|
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.
|
|
-
-
Ben L.


- Joined on 12-22-2010
- HELP I'M TRAPPED IN A COMMUNITY SERVER FACTORY
- Posts 1,875
|
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.
|
|
-
-
Ragnax


- Joined on 02-01-2007
- Posts 231
|
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.
|
|
-
-
ekolis


- Joined on 01-09-2008
- Cincinnati, OH, USA
- Posts 597
|
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.
|
|
-
-
dkf


- Joined on 04-24-2008
- Manchester, UK
- Posts 222
|
Re: Make a subtle memory leak
Nothing quite beats the power of memory leaks driven by string interning deep inside a library.
|
|
-
-
Medezark


- Joined on 12-11-2009
- Posts 403
|
Re: Make a subtle memory leak
ekolis:English:
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?
Rammy, Rammy, Rammy Ram -- and it went, wherever I, Did Go....
|
|
-
-
Faxmachinen


- Joined on 03-19-2007
- Posts 383
|
Re: Make a subtle memory leak
Ben L.:Your choice of language. Go. That's not much of a choice.
Mr. "I got 90% on meta-critic"
|
|
-
-
dhromed


- Joined on 04-13-2005
- Dutchland
- Posts 10,305
|
Re: Make a subtle memory leak
dkf:Nothing quite beats the power of memory leaks driven by string interning deep inside a library. That is so hot.
 boomzilla: I think the obvious answer is for everyone to just stop programming.
|
|
-
-
joe.edwards


- Joined on 08-14-2006
- Dallas, TX
- Posts 1,315
|
Re: Make a subtle memory leak
dhromed: dkf:Nothing quite beats the power of memory leaks driven by string interning deep inside a library. That is so hot.
What could be dirty about interning your string deep inside a library until it leaks?
I spend most of my life pressing buttons to make the pattern of lights change however I want.
|
|
-
-
Page 1 of 1 (28 items)
|
|
|