Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Memory allocation of new object

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 09-03-2010
Member
 
Join Date: Dec 2009
Posts: 292
Memory allocation of new object
  

Hello,
I have a beginners question for you
If I make 2 "new" on the same variable:
Example
Code:
byte[] b = new byte[sz + 100];
...
.../ / code ...
...
b = new byte[sz];
Is that space that was allocated in memory with the first "new", be well removed by the garbage collector before the second allocation? Or is it going to be a conflict because the variable has 2 memory addresses? How the memory is allocated for the new object? Any help on this is appreciated. Thanks in advance.

Reply With Quote
  #2  
Old 09-03-2010
Member
 
Join Date: Nov 2009
Posts: 335
Re: Memory allocation of new object

Hello,
The memory allocated in the first "new" will be removed after the second allocation when the garbage collector will run. In fact you can not really knowing where the Garbage collector reclaim memory. An object is de-allocated when no longer affiliated with the objects in your application. Here seen as "buff" points to the second allocation, the first may be recovered by the Garbage collector.
Reply With Quote
  #3  
Old 09-03-2010
Member
 
Join Date: Nov 2009
Posts: 356
Re: Memory allocation of new object

Hello,
There will be no conflict if two byte [] are allocated. What you do by
Code:
b = new
during your second benefit is that you allocate a new memory for your object, you associate him the reference b. The first allocation was no longer of reference and will be thrown in the trash by the garbage collector. If you need any more help regarding this topic then you can ask and we will try to solve your query.
Reply With Quote
  #4  
Old 09-03-2010
Member
 
Join Date: Nov 2009
Posts: 446
Re: Memory allocation of new object

Hello,
That's right, when an object no longer reference it, it is touchy to be the part of the work by the garbage collector. You can not really know when, even if it is possible to explicitly call the garbage collector using the method
Code:
System.gc () (use only in rare cases very specifically)
If you need more information on the garbage collector then you can visit the sun's official site there you can find more detailed information on the garbage collector.
Reply With Quote
  #5  
Old 09-03-2010
Member
 
Join Date: Nov 2009
Posts: 343
Re: Memory allocation of new object

Hello,
This is possible with Reference and ReferenceQueue (read: Understanding References in Java). I would say especially not to use: In the best case it will do nothing, but he runs it causes a full collection ", i.e a release of up to object but can be quite costly in execution time. Using massively (in a loop for example), this can give disastrous results, for example: Difference of performance Unix / Windows program? In short, the de-allocation is done automatically by the garbage collector, and the best thing to do is let it work in his corner.
Reply With Quote
  #6  
Old 09-03-2010
Member
 
Join Date: Apr 2008
Posts: 264
Re: Memory allocation of new object

Hello,
I do not know that this is what you need exactly, but here is a code, see if it can help you.
Code:
public class SomeObject{

    private String str;

    public SomeObject(String str){
        this.str = str;
    }
    public void setSomeProperty(String str){
        this.str = str;
    }
    public String getSomeProperty(){
        return this.str;
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Memory allocation of new object"
Thread Thread Starter Forum Replies Last Post
Memory allocation limits for applications on x86 systems Mulan Software Development 7 07-08-2010 06:32 AM
Memory allocation in C++ Prashobh Mallu Software Development 5 08-02-2010 09:56 AM
Memory allocation and management AdityaR Software Development 4 22-01-2010 08:42 PM
Memory allocation optimized bitwise Ricky58 Software Development 3 07-05-2009 10:53 AM
SBSMonitoring/SQL Memory allocation error (run away Memory usage) Dilbert Small Business Server 1 21-09-2005 11:32 AM


All times are GMT +5.5. The time now is 02:20 PM.