• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

tips on cleanup of memory

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic