|
| |||||||||
| Tags: java, memory allocation, new keyword, object, programming language |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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]; |
|
#2
| |||
| |||
| 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
| |||
| |||
| Re: Memory allocation of new object
Hello, There will be no conflict if two byte [] are allocated. What you do by Code: b = new |
|
#4
| |||
| |||
| 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) |
|
#5
| |||
| |||
| 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
| ||||
| ||||
| 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;
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Memory allocation of new object" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 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 |
| changing video memory allocation? help | ChumsGum | Vista Help | 4 | 22-06-2009 08:39 PM |
| inetinfo.exe memory allocation | cmjkeegan | Small Business Server | 5 | 14-02-2006 03:28 PM |
| SBSMonitoring/SQL Memory allocation error (run away Memory usage) | David Gill | Small Business Server | 1 | 21-09-2005 12:32 PM |