Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Matthew Francis
6,967 PointsIs there a course that teaches you Memory allocation/efficient memory usage/stuff that is related to memory?
Sorry if this has been answered, but I can't seem to find it in the Java tracks. Maybe it is taught at Android develepment? just wondering.
Matthew Francis
6,967 PointsDerefencing the obj means setting the value of the object to null right?
I never done that before, mind elaborating how you do that/show me an example?
Simon Coates
28,695 Pointsyep, once a variable has no remaining references, it should be a candidate for recycling. In some instances that might mean that the variable has gone out of scope (a local variable or a variable in a while/if block for example may die naturally for example) or this might mean you've set it to null.
String x = "i exist";
x = null;
{
//brackets create a scope - this is a fairly artificial example.
String y = "I exist";
}
//y is no longer accessible, hence can be recycled by collector
Given that the garbage collector doesn't run immediately, it's likely to be a bit difficult to observe the memory clearing for demonstration purposes. But it should free memory at intervals.
Matthew Francis
6,967 PointsMakes sense! just wondering though, when will instance variables be collected for garbage collection, eg:
class Dog{
String dogName = "Brandon";
//Other code
}
I'm guessing after Java is done referencing and executing all the methods in Dog?
So if there are no methods, Java will collect Dog as garbage after I do this:
Dog nDog = new Dog();
and would method invocation in methods be store in heap/stack?
class Dog{
void MethodOne(){
methodTwo();
String call = methodThree(); //This would be called in a stack, since this is a reference var, correct?
}
void methodTwo(){
System.out.print(..);
}
String methodThree(){
return "hey";
}
}
Simon Coates
28,695 PointsI'd assume an instance variable dies when after there is no reference to the object it's on (ie. there is no theoretical way to access the object). As for the heap/stack stuff, i'm a little unsure. I saw http://programmers.stackexchange.com/questions/65281/stack-and-heap-memory-in-java , which might help.
2 Answers
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsJudging by the courses I've taken. There is no such course here, you are right. But if you take a look at Java Content RoadMap:
https://trello.com/b/dBiGr15K/treehouse-java-content-roadmap
There may be "Garbage Collection" course is coming. So vote for it if you want.
The most "memory" related workshop is this one:
https://teamtreehouse.com/library/the-thing-about-strings
It is 10 minutes, but you will learn a bit about memory.
I would say that you will come to memory problems indirectly in many courses.
For example when you work with Java ORM Hibernate, there you will care about making sure that 'Seesion Factory' object is
static and finalso that is only created once and not in every class. There as well you will have to think about lazy instantiation problem, when you fetch resources from database.in Spring Java Web Framework, you care about when and what to "autowire" so that resources are not initilialized over and over again without a reason.
So Follow along Java track and you will see memory management here and there. It is not that it will be like in C language, where you have to allocate memory for object, and then free it. In Java you use pointers here and there by passing classes.
But if you are really interested try googling:A lot of stuff just pops out immediately
http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java
Simon Coates
28,695 PointsSimon Coates
28,695 PointsI don't know if the videos cover memory allocation. The limits of my knowledge on the topic (bit of a noob, sorry) are just to dereference when you finish with a variable, that you have limited control over the precise when of the garbage collector running, and that there a couple memory related methods on the Runtime class. And that there's a throwable error for running our of memory (OutOfMemoryError)