• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I release memory?  RSS feed

 
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code plays a single tone every 5 minutes.
When I run this code, I get an error after about 2-1/2 hours (~30 iterations of the loop). I figure it's because I'm not releasing the memory for the Receivers and Sequencers, but I don't know how to do it.
I thought that when I assign new object references in each iteration, the objects from the old iteration would get gc'd, because nothing is referring to them anymore. Apparently, that's not the case. Any help on how to manage the memory would be gratefully appreciated. Here is the error, and the code is below.

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably need to close() your Sequencer and Receiver in a finally block. Or, since both implement AutoClosable you could use the try-with-resources construct (Java 7 and above).
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marie Day wrote:I thought that when I assign new object references in each iteration, the objects from the old iteration would get gc'd, because nothing is referring to them anymore. Apparently, that's not the case.


Yes, I would agree with this assessment of the problem. I think what happens that if you have an object which uses some system resources, then perhaps something you can't see is keeping a reference to that object (and therefore, as you say, not allowing it to be garbage-collected). And that invisible something wont let go of the reference until you allow the object to release those system resources.

I'm not familiar with those MIDI-playing classes, but for example you call "open" on a Sequencer object but you don't call "close" on it. I don't know if there is a close method, but I'm guessing there is. Perhaps there's also a Receiver method which you have to call to free it up -- check the API documentation for Sequencer and Receiver and change your code to call those methods if you find them.
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the API specs for the open method here which says you must then call close after calling open.

Also consider using a Timer or a ScheduledExecutor
 
Marie Day
Greenhorn
Posts: 29
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It turns out that closing the objects was all I needed. I shortened the sleep times so I could test it faster. It does 100 iterations without crashing or slowing down (which it did with the previous code before it crashed):

So, I changed this section to the following, and it works fine.



To do anything more complex, I do probably need to look into a Timer or ScheduledExecutor.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
close methods are better called in a finally block so they get called even if an exception occurs.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage has already told you , timers or scheduled executors are better than Thread#sleep.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marie Day wrote:It turns out that closing the objects was all I needed.

It should perhaps be pointed out that Closeable (or AutoCloseable) is one of the very rare cases in Java where you have to do something explicit in order to free memory. Normally, the JVM will release objects automatically as soon as they go out of scope (or the last variable that references it gets set to null).

So, possibly worth noting for future reference.

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!