Solved

How is memory leak possible in Java with auto gc?

Posted on 2004-10-18
26
336 Views
Last Modified: 2011-10-03
I have to profile a Java application and I need to know how it's possible for a memory leak in Java when Java has automatic garbage collection.

Thanks,
-Shannon
0
Comment
Question by:ShannonE
  • 9
  • 7
  • 5
  • +3
26 Comments
 
LVL 11

Accepted Solution

by:
cjjclifford earned 50 total points
ID: 12340269
If an long living library (i.e. something that is being used continuously) neglects to release references to no-longer required objects, these cannot be garbage collected... (e.g. a custom schedule that neglects to remove an entry from a collection after triggering a task).

Also, if there is a JNI native library, this library may have a "traditional" memory leak...
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 25 total points
ID: 12340432
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12340506
also, I remember reading somewhere that classes themselves (i.e. the classes loaded by the ClassLoader) are not garbage-collected. Generally not going to be a big concern, but worth noting, especially if you use custom classloaders to repeatedly load/generate classes...

Cheers,
C.
0
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 25 total points
ID: 12340507
Hi ShannonE,

The main memory leak is when an inordinate number of Strings are created. The JVM has a String pool in place, which is a performance optimization. Since the String class is immutable, whenever you perform an operation on a String a new String is created (unless it already exists in the string pool).
E.g. if you have one String instance with the contents "Hello" and you create a new String instance with the text "Hello", they will actually point to the same String in the string pool. They will be two different references (with each their own reference value),  but point to the same string resource.

This approach creates a problem if you're creating many String objects during the execution of your program, as each different String is stored in the string pool, and not available for GC.

\t
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12341275
Much simpler guys,
when you ask for memory and never give it back, you got a memory leak.
To solve the problem is dependent on your application.

The garbage collector must not touch the objects you have in use,
GOOD GRIEVE, IF IT DID!!!!!

;JOOP!
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12341333
>>when you ask for memory and never give it back, you got a memory leak.

Simple and Good answer

Regards
Sudha
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12344921
At your service.
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12344972
True bit it doesn't declare why you get memory leaks in Java. When you exit an iteration of a loop all of the memory that you have allocated within that block is automatically dereferenced.

I personally have found that it can happen that the GC cannot get cpu time when you do some intense processing. This is due to the GC being a set of Daemon threads that can only execute when no other threads are busy. Ppl tend to forget that they still need to have wait or yield calls within a thread in order for it to acutally give something else time to process. If you forget about these calls or they are not placed in the correct strategic places you could hog the CPU and thus hinder the GC from ever executing. This situation is especially true in batch processing.

Hope it helps you a bit.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12345009
I disagree, whenever you say       "new"    a check is done if there's enough pool memory.
If the answer is no, the gc-thread is propelled.

Counterquestion:  was the initial question asked out of interest or does an application 'blow-up'.
If the first case prevails, well there's enough answer to be content with.
In the latter case, do not blame the gc! It simply can not free memory that you keep in use.

Just a tip: did you practice "addEventListener()" or the sort with an object that has heavy strains
of objects linked? Than you must undo the "add" by a "remove" when the event is no longer of concern.
This is a problem that can eat Gb's without a flaw of JAVA itself.
;JOOP!
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12345075
RuadRauFlessa,

> This is due to the GC being a set of Daemon threads that can only
Yes indeed
> execute when no other threads are busy.
True for all threads on a single processor system, not merely for the GC thread. However, the GC thread will execute when there is a need for it, something which is determined by an algorithm which is NOT guided by if there are any other threads running or not.

> Ppl tend to forget that they still need to have wait or yield calls
> within a thread in order for it to acutally give something else time to process.
Only true if you change the priority of the threads you're running, and/or the underlying
operating system does not provide support for pre-emptive multi-tasking.

>  If you forget about these calls or they are not placed in the correct
> strategic places you could hog the CPU and thus hinder the GC from ever executing.
Nope. If this was true, programming in Java would become a guessing game.

\t
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12345526
orangehead911,

>True for all threads on a single processor system

Not necessarily - depends also on the implementation of the JVM, and whether or not it makes use of OS threadding, etc.

Its worth noting that even when you are 100% confident in your own application code, it is still very likely that memory leaks can occur, due to the nature of the number of openly available libraries that get used in many applications. There are also other points of leaks in the JVM itself (listed above). Generally its reasonably important to bounce a long running (i.e. 24/7) Java application regularily to "fix" these.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12345538
> Generally its reasonably important to bounce a long running (i.e. 24/7) Java application regularily to "fix" these.

I had a multi-threaded knowledge based web application running on tomcat under Windows 2000 for almost a year and a half without the need for a reboot, so this *really* depends on what you are doing and why...  Saying that the only way to "fix" java memory leaks is to restart the java application regularly is (imho) misleading...
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12345539
Nonsense, I run 1Gb applications for days.
The cause of most so called memory leaks is that the programmer is not aware
that he/she may abandon an object, but the object is still referenced by an event handler
as I described above.
Need an explanation?

;JOOP!
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 24

Expert Comment

by:sciuriware
ID: 12345545
Tim, the "nonsense" refers the same comment as yours, not YOU of course.
Stupid browser.....
;JOOP!
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12345546
:-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12345552
Hehehe...we had the same thought upon reading cjjcliffords comment  :-)  

I agree with you too, event handlers are the biggest leaks of memory...  look after them and you are 50% of the way to fixing any memory leaks :-)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12345558
ShannonE, where are you? The answer is given!


;JOOP!
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12345570
TimYates, ok, fair cop, but what I was trying to say was if you are using 3rd party libraries that have leaks, which you don't have the ability to change (or fix yourself) then there's not much that can be done.

What volume of traffic would be going through that tomcat application?

sciuiware, I'm not refering to GUI based systems here.... our application processes many GB of ASCII files a day, every day, and also performs quite a lot of complex operations on the resulting data - the system can be seen to grow, slowly but steadily over the course of several days - yes, it doesn't get OutOfMemory very often, even after a week, but our application can take a short amount of downtime (at night when there's noone using the system) without losing data, so we take advantage of this to bounce the JVM, better to be safe than OutOfMemory...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12345598
> What volume of traffic would be going through that tomcat application?

It was an online benefit calculator for the whole of the city...  So the throughput was fairly high.  I didn't have any problems with 3rd party libraries leaking memory, but as I tend to only use the larger libraries (tomcat, xalan, xerces, etc), and any leaks I do find, I report as a bug through the usual methods (bugtraq, mailing list, etc), with a fix if I can find one...

I can't think what it is that is causing your application to go OOM...  My first guess would be you have some sort of cache on the data which is not time or size dependant, or some form of cyclic dependancy so that you are getting "rings" of un-gc-able data...
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12345625
a far lot of work has been put into getting rid of the leaks here, and by-and-large they are mostly gone, the application only grows slowly - the "bounce" is really there to-be-sure-to-be-sure...
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12345675
I don't think ShannonE can do much with such a complex answer; his question was BASIC!

;JOOP!
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12345700
calm down sciuiware... the question was about how memory leaks occur, it didn't say anything about limiting it to a basic level of information...
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12345726
The question was "I thought it was impossible"

... something like:  "I was told that the Titanic couldn't sink .....

We must be aware that a programming language for Dummies doesnot exist.

;JOOP!
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12345739
>a programming language for Dummies doesnot exist

Indeed it does not, and even if it did, sure, a more powerful dummy would be invented :-)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12345747
Agreed, back to business ...

;JOOP!
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12349474
=-)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now