0

I noticed today that my program slowly chews memory. I checked with Java VisualIVM to try and learn more. I am very new at this. I am coding in Java8 with Swing to take care of the game.

Note that nothing is supposed to happen except rendering of objects.

No new instances or anything.

The Game Loop is looking something along the lines of

while (running)
    try render all drawables at optimal fps
    try update all entities at optimal rate
    sleep if there is time over

From what I could find during a 20 minute period the following happened.

  • Sleeping is working, it is yielding most of its time.
  • 5 classes were loaded some time into the run time.
  • The game uses about 70 MB when first launched. (At this point everything is loaded as far as I can tell.)
  • 15 MB of RAM were taken pretty rapidly after the initial 70 MB. Followed by a slowly increase. Now 100 MB is taken in total.
  • CPU Usage seems sane, about 7% on my i-2500k.
  • The Heap size has been increased once. Used heap has never exceeded 50%.

If I comment everything in the Game Loop except for the while (running {} part I get fewer leaks, they still occur however.

Is this normal or should I dig deeper? If I need to dig deeper can someone point me in the direction of what to look after.

Now after 25 minutes it is up to 102 MB of ram. Meaning the leaks are smaller and fewer.

A reminder, I not very good at this. First time I try to debug my project this way. Please bear that in my mind.

Update

After roughly 40 minutes it settles at 101 to 102 MB of RAM usage. It hasn't exceeded that for 15 minutes now. It goes a bit up and down.

The Heap size is getting smaller and smaller. CPU Usage is steady.

Emz
  • 1,280
  • 1
  • 14
  • 29
  • 1
    I think you will need to post some code. – khelwood Dec 11 '14 at 13:47
  • @khelwood Be my guest, it is there now. As mentioned above, even if I uncomment everything in `run()` it still leaks some, just less. – Emz Dec 11 '14 at 13:52
  • Those statistics are confusing, especially the processor usage. I would suggest looking for some memory profilers and having a look what and where is going wrong. Memory problems happen for variety of reasons, but mainly bad overall design. – We are Borg Dec 11 '14 at 14:17
  • I am using *Java VisualIVM*. I just don't really know what to look for. – Emz Dec 11 '14 at 14:19
  • 1
    It's easier to understand women than understand the Java memory behavior. Anyway, sometimes if you take a look at the memory snapshot, it seems that a leak is going on, but it is just the GC taking a nap. Try changing memory and GC parameters to see if you get different results. – Gilberto Torrezan Dec 11 '14 at 14:20
  • @Akshay That is what I want to make sure. That it isn't bad design. – Emz Dec 11 '14 at 14:25
  • Oh, unfortunately then you will have to look at the code and use memory profiler. Without seeing the code, one cannot anticipate where is the memory leak happening. Plus Java takes care of memory management, so either it is some small mistake or bad design, that's all I can help. :-( Or if I had access to project, I could have checked in free time on weekend. – We are Borg Dec 11 '14 at 14:27
  • @Akshay The main code is in my post now. However, there are about 2000 more rows of code. I commented away everything extra and tried it with just a Runnable and still a small leak. It seems that there is no leak however. – Emz Dec 11 '14 at 14:29
  • Why don't your start by narrowing down the problem, break your project into various parts such that they are independent, don't execute the other parts, and check when is the leak happening..takes some doing, but well worth it. – We are Borg Dec 11 '14 at 14:31
  • Doing that as we speak. I am pretty sure there is no leak. Will make sure before I answer with my findings. – Emz Dec 11 '14 at 14:32
  • 1
    A saw-tooth graph of memory usage (which, I think, I is what you're describing) is pretty common in Java. Unless you're crashing with an `OutOfMemoryError`, I'd say you probably don't really have a leak going on. – Ian McLaird Dec 11 '14 at 14:35
  • @IanMcLaird It is saw-tooth formed with a small increase. Turns out that increase is from String references as well as the profiling tools themselves. – Emz Dec 11 '14 at 14:48

1 Answers1

1

Short Answer: There is no leak.

Expalanation

Using these questions as a reference.

Simple Class - Is it a Memory Leak?

Tracking down a memory leak / garbage-collection issue in Java

Creating a memory leak with Java

And this article.

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

As I mentioned in my question. I am very new to this. What triggered me was that I checked Windows Task Manager and saw an increase in memory usage by my application. So I decided to dig deeper. I learned a couple of things.

  • Javas Garbage Collection was vastly underestimated by me. It is messy to cause a memory leak if that is your intention. There can be problems with it when there is Threading involved however. Which caught my attention as I am using Threading in my project.

  • The tools that Windows provide are sub-par, I recommend using external tools. I used Java VisuaIVM. In this tool I found that there are loads of classes being loaded the first 2 minutes of the game. And 5 a bit in. Some of the first ones created are String references that JVM makes.

  • "Thread.sleep could be allocating objects under the covers." -- I found out it does these. a total of 5 even. Which explains my initial "5 classes were loaded some time into the run time.". What they do I have no clue as of yet.

  • About 10 - 15 MB was from the profiling that I did. I wish I wasn't such a rookie.

So again no leak that I could find.

Community
  • 1
  • 1
Emz
  • 1,280
  • 1
  • 14
  • 29
  • Finding a memory leak is a bit like looking at Shroedinger's cat. When you look at the program with the profiling tool, you also change its memory state. – Ian McLaird Dec 11 '14 at 14:56