File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Memory loss Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Memory loss" Watch "Memory loss" New topic
Author

Memory loss

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

How can we improve memory loss in our code.?


No Kaustubh No Fun, Know Kaustubh Know Fun..
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

I guess you're asking about memory leaks.

It's hard to create memory leaks in Java, because Java has automatic memory management (garbage collection). There are ways to have memory leaks, but this will only happen if you keep references to objects that you don't need anymore.

Normally memory leaks are not a big problem in Java applications. If you think you have a memory leak, you should use a profiler to monitor the application and find out what the source of the leak is.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

I think you are taking about prevention of memory leak? you no need to worry about it, Java takes care for you. though in some situation you need to code properly such a way that no reference are pointed the obsolete objects.

for example: when you shrink an array , make sure that (removed)references are assigned to a null value.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Jesper beaten me nicely
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

like we if we stuck is some very big infinite loop in such case can we avoid it if it occurs ?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

infinite loop ? its evil. you should rewrite your code then.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

Seetharaman Venkatasamy wrote:infinite loop ? its evil. you should rewrite your code then.


OHK
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

It sounds like there you're talking about memory use, not memory loss. Yes, it's possible to run out of memory if you use too many resources - e.g. by creating lots of objects that are all still in scope at the same time. If you're in a big loop and creating objects that are only in scope within the loop, though, the garbage collector will be able to clean them out when it needs to.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

Q: On server side validation if you've any exception how you going to handle it (Want some solution regarding struts) and which part you're going to see(He mentioned that let it be your DAO)

A.After giving all general answers(As I don't know the main one) I also check memory leaks..

Q. How you recover your memory leaks ? (Put the infinite loop in front of me)

A. Don't know
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Kaustubh G Sharma wrote:
Q. How you recover your memory leaks ? (Put the infinite loop in front of me)

A. Don't know



In Java, for most intents and purposes, a memory leak is defined as keeping unwanted objects reachable. This, of course, means that it is a bug, as why would you ever want to do that?

So to recover from memory leaks doesn't make sense. Just fix the bug so the memory leak is gone. To keep unwanted objects around, and then "recover" the memory when needed serves what purpose?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1277

Aye Aye SIR
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Memory loss