Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-26-2003, 03:55 PM   #1
Registered: Mar 2003
Location: Iowa, US
Distribution: Gentoo!!, Mandrake 9.1
Posts: 170

Rep: Reputation: 30
Question Java - will this casue a memory leak?

I am undertaking the task of learning Java for a class and have a quick question for any of you more knowledgeable than I....

Below is my code:

Packet p;
p = new Packet(0, 1, mc);

p = new Packet(0, 2, mc);
My question is, will this cause a memory leak when p is reassigned. I believe in C++ there is a delete reserved word that called the destructor for the class. (Packet is a class) This does not seem be be available in Java. If the above will cause problems, what is the correct way to do the above.

Thank you for any information you may have,

Old 03-26-2003, 04:34 PM   #2
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Java has automatic memory management, a.k.a garbage collection. You don't ever need to (and cannot) explicitly delete an object.

Old 03-27-2003, 08:48 AM   #3
Registered: Mar 2003
Location: Iowa, US
Distribution: Gentoo!!, Mandrake 9.1
Posts: 170

Original Poster
Rep: Reputation: 30
Thanks, that is exactly what I needed to know. It still feels odd, but I guess I will trust that Java knows what it is doing.

Old 03-28-2003, 04:56 PM   #4
Registered: Dec 2002
Location: Australia
Distribution: Gentoo
Posts: 161

Rep: Reputation: 30
If you really believe that you are finished with the object, you could simply set it to null.
p = null;
This would then (depending on the garbage collector used) indicate that the object is really ready for collection, rather than the gc having to work it out. Garbage collection is an in depth topic though, and you could spend a good few days reading about it
Old 03-28-2003, 05:38 PM   #5
Registered: Nov 2001
Location: Exeter, UK
Distribution: Gentoo 1.4
Posts: 243

Rep: Reputation: 30
I wouldn't do that it could still cause a segfault. Just leave it to the garbage collector.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory leak?! nyk Programming 10 01-24-2005 07:08 PM
memory leak mfitzpat Linux - Newbie 1 09-24-2004 02:58 PM
memory leak mdk Mandriva 1 09-17-2004 10:54 AM
Memory Leak don_dimo Linux - General 7 03-06-2004 06:51 PM
Memory Leak when using memory debugging C program on SuSE SLES8 babalina Linux - Distributions 0 10-06-2003 09:39 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:38 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration