Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Memory leak issue

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm gonna break this into four parts because its a little lengthy (lots of code, and an explanation as to what i'm doing).

Anyway part 1:

I'm designing a game and if I run it for a long while it'll get an OOM exception. I don't know why this is at all because when I trace through the code its not making any new objects each turn.

Anyway, the relevant bits


the first part of the constructor creates a warehouse object and a hydroponics array, and then creates those objects.
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
part 2:

The hydroponics object uses a superclass, factory.

This is the code for that
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
part 3:

And now, this is the hydroponics object.
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
part 4: Explanation of events

When I run the game, I made a runTurn() method in the StarPort class that only (so far), calls runProduction(), and that in turnn calls the produce method of all the hydroponics objects. I noticed as I did this that my memory started to increase a bit, so I put the thing in an infinite loop and got an OOM exception a few seconds later with no stack trace available (which helps things, a lot . . . )

Whereever I trace through where it SHOULD be going i'm not creating anything new. Food isn't an object, just an int, and that number stops increasing once the chemicals in the warehouse are gone (it "breaks" silently at this point, as in keeps looping through but doesn't change any numbers around), and I'm not making any new warehouse objects, AFAIK, either. The one I am using is recognizable enough and keeps track of everything up to the point i have programmed it in (That is to say right now its small, but i have big plans for it).

Right now I have about two options.

1. Find out what's going on while the program is small.

2. Worry about it later since it fairly works and i can continue making it bigger. Frankly the issue with this is I'd still have a memory leak and a bigger program to have to debug. So i'm gonna try choosing option 1.

I could likely just make everything one object, but this is too much a simplification for what I want in the long-run.

Anyone have any ideas?
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NM.

I just ran a debugger and saw the stacktrace from that and (along with something my teacher suggested) was able to fix my own problem.
 
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done.

For the benefit of other readers with similar problems, please tell us how you sorted it.
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure.

Well my first thought was that I was constructing something somewhere, and that it had a pointer i couldn't get to, although no indications pointed to that being the case.

My CS teacher said something about only using an object as long as you need it, and said to look up deconstructors (A cursory search into that provided some background on what the GC does and if calling it is useful or not, and some stuff about a finalize() method that does the actual deconstructing).

Since I knew where the program was going, I decided to target its path with a debugger, and execute each thing a step at a time. It was handy enough to provide a stacktrace of what was in memory at the time, and I noticed that the methods in hydroponics went away when it was done with the object, but the StarPort object methods (is that correct terminology?) we're still in memory even after it had run them (It wasn't clearing them out, even though it had exitted them and gone elsewhere in the object, where the bulk of the game was taking place).

I think by that point I had figured out I had to have a way to exit the object but keep a reference to the object itself, which I figured out by putting the object reference into the main method and calling methods within the class with dot notation. Ran it for a bit and memory wasn't increasing by a ton like it was before and the debugger was showing the stack being cleared once it was finished.

I wouldn't be surprised if more memory leaks in Java were caused by something like this. If the way I explained this didn't make sense I can clarify.
 
Campbell Ritchie
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't followed it too closely, but using object names and dots sounds like common or garden object-oriented programming! Well done.

But it is not normal practice in Java to use destructors (they don't actually exist in Java, but they are commonly used, I think, in C++). Even the finalize method is very rarely used. Do a search through JavaRanch and you can find out more.
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I just mean that when you execute everything from within the object (like, looping through methods without going back to say, main), the program will just keep s atack of hat it goes through without clearing it, until it runs out of memory.

But if you access stuff dot notation this isn't such an issue.
 
Everybody! Do the Funky Monkey! Like this tiny ad!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic