• Post Reply Bookmark Topic Watch Topic
  • New Topic

Memory and recusion  RSS feed

 
Gary Wolgast
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a class instantiates an object like a string or StringBuffer in a method and that method then instantiates a new copy of itself and calls that same method (recursively calling itself) is the memory in each object stay in scope during the entire time the original (first) object of that type exists? Will the garbage collector leave them alone or will it be collected?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know the exact answer to your question but I have a question for you. Why would you ever need/want a method to instantiate it's own class? And on top of that recursively?
 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that the garbage collector would leave them alone, like in normal recursion. It is low-priority, so it shouldn't touch them if you're using the methods repeatedly like in a normal recursion, because it should see that it needs to return to them eventually (you said you're calling them recursively, so it would eventually have to return back to the first call of the method).
 
Gary Wolgast
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not my code. Someone else wrote but have to support it. I've counted this class calling itself 14 levels of recursion. Is seems the higher level of recursion it reaches, the more memory that gets used. It's a poor design from what I can tell.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!