Timeline for How can I create a memory leak in Java?
Current License: CC BY-SA 4.0
22 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
May 12, 2021 at 21:45 | history | edited | Peter Mortensen | CC BY-SA 4.0 |
Removed historical information (that is what the revision history is for) - the answer should be as if it was written right now, etc.
|
May 23, 2017 at 12:02 | history | edited | URL Rewriter Bot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
Feb 24, 2014 at 10:08 | comment | added | Evan Plaice | Can't you just allocate a bunch of memory on background threads and intentionally deadlock them by using a very poorly structured system of nested thread locks? It fits the profile of running for the life of the program and not being accessible by the application or recoverable by the GC. | |
Oct 4, 2013 at 4:33 | history | edited | Boann | CC BY-SA 3.0 |
added 1065 characters in body
|
Oct 4, 2013 at 4:30 | comment | added | Boann | @Spidey "I could accept that as a memory leak" Thank you. Earlier, you'd said something couldn't be a leak if it's still referenced and that you couldn't have a leak in Java because it has a garbage collector. >_< "Your answer suggests that any unused allocated object implies a leak". Well, the example is a scenario of an object that the garbage collector can't/won't collect. If it emerges in a program and happens repeatedly it becomes a leak. I'll try to improve my answer with that info. | |
Oct 3, 2013 at 22:02 | comment | added | Bill K | This is totally valid code, it's what it means to use threads. They are designed to keep their references. Also it's not only threads that do this, also GUI frames keep everything referenced by them even though there is no thread. There are a few other "tops" to the GC tree as well. If you have a thread that runs forever then, by design, it will keep everything it references forever, otherwise nothing will work. If this is not what you intended however, then you DO have a programming bug! | |
Oct 3, 2013 at 17:23 | comment | added | Spidey | I mean all the people who read your answer and our discussion, and still didn't care to up or down your post. But that's not a real argument anyway. See slowlyLeakingVector in Listing 2. I could accept that as a memory leak, but that's different from what you are doing. As I see, your answer suggests that any unused allocated object implies in a leak. I don't agree with that. | |
Oct 3, 2013 at 16:14 | comment | added | Boann | @Spidey "not much people think you are right".. Uh, about which? Which people? Where? Check the previous page of answers for the many older, popular answers that I'm defending here and which you are calling wrong. "Memory leaks in Java are not real leaks" Nowhere does the article say that. It gives examples of memory leaks and shows how they are a real loss of usable memory due to objects not being reclaimed. Those objects accumulate over time and become a real problem. That's what a memory leak is. "they are bad design choices or bugs in code" ... which result in memory leaks, yes. | |
Oct 2, 2013 at 23:19 | comment | added | Spidey | obviously not much people think you are right. See this: ibm.com/developerworks/rational/library/05/0816_GuptaPalanki Memory leaks in Java are not real leaks, they are bad design choices or bugs in code. | |
Oct 2, 2013 at 19:36 | comment | added | Boann | @Spidey It's the most important difference. At a lower level (program -> JVM -> OS -> RAM chips -> Universe) any memory leak from the higher level becomes unreal. The practical definition for a memory leak is that during execution, your memory space (at whatever level you define it) is accumulating things which are not wanted, reducing available space for things which are wanted. "In a C program, a leak isn't referenced anywhere" -- also wrong -- leaked memory could still be referenced. What matters is whether the memory can be re-used, which in C does not depend on whether it's referenced. | |
Oct 2, 2013 at 18:07 | comment | added | Spidey | There is no difference between "live references and obsolete ones" in the context of the question. They are still referenced somewhere. In a C program, a leak isn't referenced anywhere, even the OS can't touch it. It can release all memory pages used by a given process, but it can't reference each and every memory block allocated. | |
Oct 2, 2013 at 14:22 | comment | added | Boann | @Spidey Then try it for yourself and you'll see that it does leak. The garbage collector isn't (and cannot be) infallible because it can't tell the difference between live references and obsolete ones, which is why explicit nulling of obsolete references is necessary when the object or scope holding the obsolete reference is still in use. | |
Oct 2, 2013 at 14:06 | comment | added | Spidey | Since Java has a garbage collector, I don't think it can even leak. | |
Oct 1, 2013 at 18:17 | comment | added | Boann | @Spidey What is your answer? What do you consider a leak? | |
Sep 27, 2013 at 18:00 | comment | added | Boann | The practical definition of a leak is that it's memory which has been lost track of such that we don't know and thus can't perform the procedure necessary to reclaim only it; we would have to tear down and rebuild the entire memory space. A rogue thread like this could arise naturally through a deadlock or dodgy threadpool implementation. Objects referenced by such threads, even indirectly, are now prevented from ever being collected, so we have memory which will not be naturally reclaimed or reusable during the lifetime of the program. I'd call that a problem; specifically it's a memory leak. | |
Sep 27, 2013 at 18:00 | comment | added | Boann | @Spidey If you would count memory that the process knows about as not being leaked, then all the answers here are wrong, since the process always tracks which pages in its virtual address space are mapped. When the process terminates, the OS cleans up all the leaks by putting the pages back on the free page stack. To take that to the next extreme, one could beat to death any argued leak by pointing out that none of the physical bits in the RAM chips or in the swap space on disk have been physically misplaced or destroyed, so you can switch the computer off and on again to clean up any leak. | |
Sep 27, 2013 at 15:51 | comment | added | Spidey |
Yes I do. The process still references the thread, and the thread still references the o object. You may have misunderstood what a memory leak is.
|
|
Sep 27, 2013 at 1:39 | comment | added | Boann | @Spidey "You'll have a [thing] for the lifetime of your program. That doesn't count as a leak to me." Do you hear yourself? | |
Sep 27, 2013 at 1:24 | comment | added | Spidey | You'll have a thread "running" (or sleeping, whatever) for the lifetime of your program. That doesn't count as a leak to me. As well as a pool doesn't count as a leak, even if you don't use it entirely. | |
Sep 26, 2013 at 23:39 | comment | added | Boann | @Spidey Nothing is "stuck". The calling method returns promptly, and the passed object will never be reclaimed. That's precisely a leak. | |
S Aug 31, 2013 at 5:10 | history | answered | Boann | CC BY-SA 3.0 | |
S Aug 31, 2013 at 5:10 | history | made wiki | Post Made Community Wiki by Boann |