← Home ← Back to /g/

Thread 106402343

53 posts 8 images /g/
Anonymous No.106402343 [Report] >>106407829 >>106409565 >>106411928 >>106415575
Destructors and Smart Pointers for Java
I want destructors and smart pointers in Java so I can destroy objects inside the Java heap on demand.
>inb4 use JNI/FFM
No, because it allocates objects outside of the Java heap.
Anonymous No.106402450 [Report] >>106403452
Java kinda sucks compared to Squeak for this, pretty much all "object oriented" systems break down at some upper level into non-object-oriented.

With Squeak this is at the VM level where you can inspect, change, destroy anything. Don't like the integer 2? Change it to mean 3 and watch the whole system instantly freeze up. 3 is just an instance of a certain type of object after all, it can be defined to mean anything as a special case.
Anonymous No.106402480 [Report] >>106402546
All Java references are smart pointers. You can have a finalize clause for your resources, but you cannot call the destructor yourself because de-allocation strategy is dependent on the used GC.
Depends what you need to do, maybe look into different types of GCs and switch to one that matches your case. It's also possible you look into this unnecessarily since server RAM is cheap.
Anonymous No.106402546 [Report] >>106402574 >>106405208
>>106402480
but the garbage collector is not deterministic
i would like it if it had swift-style reference counting and destroys objects upon reference count reaching 0
Anonymous No.106402554 [Report] >>106402816
Slap them into a array
Anonymous No.106402574 [Report]
>>106402546
Yeah, it's a trade-off. Having this hidden from the programmer means you can switch the type of collector globally without changing any Java code.
Anonymous No.106402816 [Report]
>>106402554
Array is just an object
Anonymous No.106402906 [Report]
>inb4 just use c++
No, because Java has a fantastic standard library and just a far less aids ecosystem compared to C++.
Just using C++ makes me want throw myself off a cliff
Anonymous No.106403168 [Report] >>106403363 >>106403431
Java is just a better C++ that feels like C++ unlike Rust which adopts alien and gay syntax
Anonymous No.106403239 [Report] >>106403425
can java use :: for namespace resolution instead of .
no one in their right mind thinks . makes sense
Anonymous No.106403354 [Report] >>106403569 >>106403732
Use case for deleting objects from memory yourself?
Anonymous No.106403363 [Report]
>>106403168
I never thought about it that way but it's kind of true.
Anonymous No.106403425 [Report] >>106403569
>>106403239
:: is method reference, not ideal but having lambdas in the language was worth the addition of a couple of hacks
. is shit because you have single argument dispatch as the default (the left side of the period)
Anonymous No.106403431 [Report]
>>106403168
damn
Anonymous No.106403452 [Report]
>>106402450
>Java kinda sucks compared to Squeak for this, pretty much all "object oriented" systems break down at some upper level into non-object-oriented.
>With Squeak this is at the VM level where you can inspect, change, destroy anything. Don't like the integer 2? Change it to mean 3 and watch the whole system instantly freeze up. 3 is just an instance of a certain type of object after all, it can be defined to mean anything as a special case.

Based.
Anonymous No.106403569 [Report]
>>106403425
I wish they chose something else for method referencing, :: does not make sense to me with its precedence as namespace resolution, I wish Java had just used :: and . distinctly between method/field access and namespace resolution

>>106403354
Relying on garbage collection is gay when you are competent enough to use RAII
Anonymous No.106403732 [Report]
>>106403354
garbage collection cycles are expensive and not practical sometimes
Anonymous No.106404335 [Report] >>106404587
Why not just use C#? You can stackalloc arrays and have destructors.
Anonymous No.106404587 [Report]
>>106404335
i dont want to be tied to microjeet
Anonymous No.106405208 [Report] >>106405340 >>106405428
>>106402546
>but the garbage collector is not deterministic
which one?
you know there's like 5 different garbage collectors and some of them are deterministic
like the one that doesn't collect garbage at all, that's very deterministic indeed
this is why Java wins
you can choose how garbage is collected after the fact, no need to rewrite your whole app to do it
Anonymous No.106405340 [Report]
>>106405208
You don't need garbage collection. Get rid of that shit my dude.
Anonymous No.106405428 [Report] >>106405526 >>106405628 >>106407097
>>106405208
>like the one that doesn't collect garbage at all, that's very deterministic indeed
I just want to be able to destroy objects as soon as the reference count reaches 0, not rely on generational GC or just dumping all my shit into the heap and never cleaning it. I am not a shitjeet, I can clean up after my trash
Anonymous No.106405526 [Report] >>106405619
>>106405428
Sounds like you really want C++.
Anonymous No.106405619 [Report] >>106405688
>>106405526
What is a C++?
Anonymous No.106405628 [Report] >>106405692 >>106406517
>>106405428
>I just want to be able to destroy objects as soon as the reference count reaches 0
Oh, you're a rust troon
A doubly linked list blocks your path
Reference count never reaches zero since elements refer to one another, lol
Anonymous No.106405688 [Report]
>>106405619
A satanic deception aimed against Assembly (pbuh)
Anonymous No.106405692 [Report] >>106405727 >>106406517
>>106405628
that is what weak_ptr is for, you retard
Anonymous No.106405727 [Report]
>>106405692
lol, he's mad now that he has been sussed out
Anonymous No.106406517 [Report] >>106411104
>>106405628
Okay jeet, we have WeakReference for that.

>>106405692
Another cnile exposes xerself for being an incompetent nocoder lmao, cniles cannot into smart pointers for all their larp about manual memory management
Anonymous No.106407097 [Report] >>106407835
>>106405428
>I just want to be able to destroy objects as soon as the reference count reaches 0
your desire is busywork that has no relation to any real business problem
Anonymous No.106407829 [Report] >>106408993
>>106402343 (OP)
You can't directly control memory deallocation in Java. Really, Java's GC's are just so absurdly good you really don't need to. If you need throughput, G1 is king. If you need low-latency and concurrent GC, go with ZGC.
Anonymous No.106407835 [Report] >>106410039 >>106415727
>>106407097
how is that bad
swift literally does that
c++ smart pointers do this
Anonymous No.106408103 [Report] >>106409481
Can you even really switch the garbage collector in Java for a deterministic one?
Anonymous No.106408993 [Report] >>106410469 >>106411092
>>106407829
I know Shenandoah is very powerful garbage collection but why even bother with it, why not just be deterministic and destroy as soon as possible, and just use GC for very tiny cases like mutual references
Anonymous No.106409481 [Report] >>106412882 >>106413337
>>106408103
No. The bytecode has no concept of scope or variable no longer used. The compiler would have to insert the calls manually, assuming the VM supports manual destruction. This bloats the bytecode gen and likely consumes a bit more CPU.
Anonymous No.106409565 [Report]
>>106402343 (OP)
Anonymous No.106410039 [Report] >>106412205
>>106407835
>how is wasting time on things that don't add or solve anything bad

>swift literally does that
>c++ smart pointers do this
then use one of those languages, why insist on Java?
Anonymous No.106410469 [Report]
>>106408993
To have less on your mind when doing programming work.
Anonymous No.106411092 [Report]
>>106408993
why don't you write a garbage collector that accomplishes what you want? make it FOSS and it might even get adopted into openjdk
Anonymous No.106411104 [Report] >>106412205
>>106406517
Smart pointers is not a form of manual memory management.
Anonymous No.106411928 [Report] >>106412014 >>106412134
>>106402343 (OP)
well you obviously cant explicitly free memory because the vm is cucked but you could do something like this:
import java.lang.ref.Cleaner;
import jdk.internal.ref.CleanerFactory; // used by JDK; in apps use Cleaner.create()

public final class SensitiveBuffer implements AutoCloseable {
private static final Cleaner CLEANER = CleanerFactory.cleaner(); // or Cleaner.create()
private byte[] data;
private Cleaner.Cleanable cleanable;
private volatile boolean destroyed;

private static final class Zeroer implements Runnable {
private byte[] data;
Zeroer(byte[] data) { this.data = data; }
@Override public void run() {
if (data != null) {
java.util.Arrays.fill(data, (byte) 0);
data = null;
}
}
}

public SensitiveBuffer(int size) {
this.data = new byte[size]; // on-heap
this.cleanable = CLEANER.register(this, new Zeroer(data));
}

public byte read(int i) {
try {
checkAlive();
return data[i];
} finally {
java.lang.ref.Reference.reachabilityFence(this);
}
}

private void checkAlive() {
if (destroyed) throw new IllegalStateException("Destroyed");
}

// Deterministic “destructor”
@Override public void close() {
if (!destroyed) {
destroyed = true;
// invoke cleanup now
cleanable.clean();
cleanable = null;
// make object unusable
data = null;
}
}
}
Anonymous No.106412014 [Report] >>106412182
>>106411928
is that what you wanted OP?? no?? well then maybe stop wanting C++ features in Java and go back to your jeetcamp you fucking retard
Anonymous No.106412134 [Report] >>106412171
>>106411928
CleanerFactory is no longer available.
Anonymous No.106412171 [Report] >>106412196
>>106412134
it absolutely is available
https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/jdk/internal/ref/CleanerFactory.java
Anonymous No.106412182 [Report] >>106415621
>>106412014
No, because C++ sucks and Java is sane. How about you "go" back to Go, Rapedeep?
Anonymous No.106412191 [Report]
you don't think about registers when you program in C.
Anonymous No.106412196 [Report]
>>106412171
it's internal however that's why i fucking annotated that you should use java.lang.ref.Cleaner or Cleaner.create()
Anonymous No.106412205 [Report]
>>106410039
Because I like Java, C++ sucks, and Swift is for Applejeets. Was that so hard to understand?

>>106411104
No one said it was, they said Cniles larp about malloc() and pointer arithmetic yet cannot into smart pointers.
Anonymous No.106412882 [Report]
>>106409481
rustjeet has no idea what he's talking about again
Java is the superior solution for memory safe coding
Anonymous No.106413337 [Report]
>>106409481
explain how, how does that differ from just immediately destroying the object once no references exist to it? is that not how shared pointers work
Anonymous No.106415575 [Report]
>>106402343 (OP)
>java
Anonymous No.106415621 [Report]
>>106412182
I don't know why you faggots think Go is the jeet language when it was invented by White dudes.
Anonymous No.106415727 [Report]
>>106407835
these language features solve a problem that java doesn't have. reference counting is a primitive form of gc, you got much better gcs to choose from. batch processing where throughput is king? parallel gc. web app where max pause times (latency) is important? zgc. the latter one has sub millisecond gc pauses, that's less than 1% of a request