• Post Reply Bookmark Topic Watch Topic
  • New Topic

tips on cleanup of memory

 
Udayan Kumar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I have an application routine which accepts a custom java object as input. On receipt of this object we start business processing using the same and in the event churn out more objects before finally returning the output which is again a custom java object.

Now we plan to call this application routine in a loop. I would want to know while calling a program routine in a loop will result in possible memory/performance issues.
Would it be a good idea to clean up all such objects after every loop run. If yes do let me know the best way to go about the same.

Thanks


 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cause of any memory leak is to hold on the the object any longer than it is required.
So, in your processing if you are assigning the created objects to any static or long-lived references or returning them from your method to be stored in some long lived reference, it can be a cause of a memory leak.
On the other hand, if all the objects you are creating are assigned to local references and are not "escaping" (returned from the method) then you really do not need to bother about the memory leaks here. Unless of course your method does not ever return and keep on generating new objects adding to a local variable.
 
Udayan Kumar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitesh,

In our code there is no static variable usage. Can you elaborate on long lived references. Yes we do have a return type (custom java object) which includes some other objects part of the program flow.
The very first steps in the routine is all objects used as part of processing as well output objects are initialized to null and then based on the input elements the program proceeds.

Let me know.


 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Udayan Kumar wrote:Yes we do have a return type (custom java object) which includes some other objects part of the program flow.


Is the object returned is one of the many objects you create while your application processing. If yes, then is the returned object stored in a class level variable or is long lived?
See the idea is the following:

If any object instance is returned from a method it has a potential of living longer than it would have otherwise done. The reason being that it otherwise would have been a local variable and local variables are unreferenced as soon as you exit the scope.
However, when you return this object from the method you might potentially have a reference to it which is not a local variable or even if it is, it may have a bigger scope.
So, you need to check your code to find out whether any of the returned objects can potentially be referenced for a longer time than required.

Udayan Kumar wrote:
The very first steps in the routine is all objects used as part of processing as well output objects are initialized to null and then based on the input elements the program proceeds.


This may not be sufficient as you return one of the output objects that might be referred somewhere else. However, as i have not seen your code, my interpretation could be wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!