Results 1 to 6 of 6

Thread: Memory allocation of new object

  1. #1
    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.

  2. #2
    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.

  3. #3
    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.

  4. #4
    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.

  5. #5
    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.

  6. #6
    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;
        }
    }

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,052,241.37535 seconds with 17 queries