Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

memory leakage

Status
Not open for further replies.

WebGoat

MIS
Jul 16, 2005
85
US
i dont understand "memory leakage" in java. it is called if your program leaks memory , then you are gone because your program may demand 100% CPU utilization and thats a hang condition.

so whats is this deadly "memory leakage" ?

suppose, i did not close the DB connection in my program . will you call it a memory leakage ?

suppose, i did not use "null" when i am done with any object . will you call it a memory leakage ?




i am confused about "memory leakage".Can anybody show me a simple demo example on "memory leakage"?

 
do you understand how memory leaks occur in C/C++ ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 

>do you understand how memory leaks occur in C/C++ ?
No sir.

ok, as you are pointing about C/C++ then i assume you are pointing to malloc or calloc function in C AND New/Delete operator in C++. right ?

ok, let us talk about only C , suppose, i allocated memory for 10 integers by using a malloc() function IN C but DID NOT call free() to free that memory . is that called memory leakage ?

this is a guess only, i am not sure. but in java we do not do that way. is not it ?

will you please explain a little bit about this memory leakage. you are a top member in this forum . i dont want to miss the opportunity to know this concept from you.

kindly, can you explain a bit with a simple example ?

thank you for the response.

 
In C/C++, as in Java, variables and/or objects are assigned either to the stack, or to the heap.

In both langauges, as a general rule, objects/structs are assigned to the heap using the "new" operator (malloc in C).

In C/C++ If you assign an object to the heap, but do not delete it (or free it), you have a memory leak.

Primitive data types (int, double, char etc) get assigned to the stack, and need not be deleted by the programmer. Pointers to objects in the heap are also on the stack.

So when you say :

MyObject* pMyObject= new MyObect();

This assigns space for MyObject to the heap, and assigns a pointer to that memory onto the stack.

If you do not delete that object, you have a memory leak.

In Java, this is basically what is happening also. However in Java, you do not need to call delete because a "garbage collector" (GC) does the job for you.
Whenever an object is created in Java, a pointer is created behind the scenes. Whenever any other object references that pointer, [basically] a reference counter is incremented. When GC runs, if hat object is no longer referenced by anything, the memeory it was using on the heap is freed (Its a bit more complicated than that, but thats the general idea).

Its fairly hard to get memory leaks - but they can occur (generally in the form of notification handlers/listeners).
See .

To answer you questions above :

Not calling close on a JDBC connections is NOT a memory leak per se (but it is a resource leak because you will have an open TCP/IP port against the db).

You do not 'need' to set an object to null in order for it to be GC'd.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
it would be very much appreciated if you provide a faulty code in java which does the memory leak. can you ?
thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top