Coder Social home page Coder Social logo

Comments (12)

virusdefender avatar virusdefender commented on August 18, 2024

你是通过malloc分配的空间么?貌似只malloc而不去memset的话就不会被计算在内。

你可以贴一下测试的代码。

from judger.

airkid avatar airkid commented on August 18, 2024

不太明白您说的malloc和memset是指哪里
我是通过设置的preexec_fn来限制的内存

class Limiter:
def init(self, cpu, mem):
self.cpu = cpu
self.mem = mem
self.cpu_time = self.memory_use = 0

    def __call__(self):
        res.setrlimit(res.RLIMIT_CORE, (0, 0))
        res.setrlimit(res.RLIMIT_MEMLOCK, (0, 0))
        res.setrlimit(res.RLIMIT_FSIZE, (Tester.OUTPUT_MAX, Tester.OUTPUT_MAX))
        res.setrlimit(res.RLIMIT_CPU,(self.cpu,self.cpu))
        if self.mem != -1:
            res.setrlimit(res.RLIMIT_AS,(self.mem,self.mem))
        os.nice(10)

def cxx(self):
ofile = TemporaryFile('w+t')
if self.ua:
bin = ANSWER_PATH + self.id + '/x' + self.id
else:
bin = BINARY_PATH + self.id + '/x' + self.id
p = Popen(bin, stdin=self.ifile, stdout=ofile, preexec_fn=Tester.Limiter(self.lcpu, self.lmem),
universal_newlines=True, stderr=DEVNULL)
waitResult = os.wait4(p.pid,0)
print(waitResult)
self.return_code = waitResult[1]

在 2016-03-20 10:38:17,"李扬" [email protected] 写道:

你是通过malloc分配的空间么?貌似只malloc而不去memset的话就不会被计算在内。

你可以贴一下测试的代码。


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub

from judger.

virusdefender avatar virusdefender commented on August 18, 2024

你上面说的

然而就算这个ru_maxrss无论我怎样修改测试cpp代码,数值都是类似的,貌似就是给出的空间而非使用的空间

你可以贴一下测试用的几个cpp代码

from judger.

airkid avatar airkid commented on August 18, 2024
#include<iostream>
using namespace std;
int a,b,c[100000000];

int main()
{
    cin>>a>>b;
    for(int i=1;i<5*1e7;i++);
        cout<<a+b<<endl;
}

您的意思是不是c数组需要一些操作才能检测到内存?

在 2016-03-20 10:45:24,"李扬" [email protected] 写道:

你上面说的

然而就算这个ru_maxrss无论我怎样修改测试cpp代码,数值都是类似的,貌似就是给出的空间而非使用的空间

你可以贴一下测试用的几个cpp代码


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub

from judger.

virusdefender avatar virusdefender commented on August 18, 2024
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    // 1k
    int small_size = 1024;
    // 150m
    int big_size = 150 * 1024 * 1024;

    int *s = NULL, *b = NULL;

    s = (int *)malloc(small_size);
    if(s){
        memset(s, 0, small_size);
        printf("malloc small size succedeed\n");
    }
    else{
        printf("malloc small size failed\n");
        return -1;
    }

    b = (int *)malloc(big_size);
    if(b){
        memset(b, 0, big_size);
        printf("malloc big size succedeed\n");
    }
    else{
        printf("malloc big size failed\n");
        return -2;
    }
    return 0;
}

你把这个分成两部分,一个small,一个big的,看看有区别么?


update:

  • 经过测试,如果不memset,内存确实不会被计算在内的;

from judger.

virusdefender avatar virusdefender commented on August 18, 2024

你的代码的问题可能是数组c没有使用,被编译器优化掉了。

from judger.

airkid avatar airkid commented on August 18, 2024

分成两份运行以后……在linux虚拟机上返回的结果还是一样的

(16508, 0, resource.struct_rusage(ru_utime=0.0, ru_stime=0.0, ru_maxrss=5268, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=478, ru_majflt=0, ru_nswap=0, ru_inblock=0, ru_oublock=8, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=1, ru_nivcsw=2))
malloc big size succedeed 3
Result is -7
(16509, 0, resource.struct_rusage(ru_utime=0.0, ru_stime=0.0, ru_maxrss=5280, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=485, ru_majflt=0, ru_nswap=0, ru_inblock=0, ru_oublock=8, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=1, ru_nivcsw=2))
malloc small size succedeed 3
Result is -7

在 2016-03-20 10:58:59,"李扬" [email protected] 写道:

你的代码的问题可能是数组c没有使用,被编译器优化掉了。


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub

from judger.

airkid avatar airkid commented on August 18, 2024

也换到了服务器上,刚刚那份代码的数组使用了,那份big的也添加了,仍旧是与helloworld返回内存相差无几,但耗时都是准确的

在 2016-03-20 10:58:59,"李扬" [email protected] 写道:

你的代码的问题可能是数组c没有使用,被编译器优化掉了。


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub

from judger.

virusdefender avatar virusdefender commented on August 18, 2024

我上面补充了一下,你看到的有没有memset

from judger.

airkid avatar airkid commented on August 18, 2024

添加了memset以后所有的代码都跟预期一样了,太感谢了!
就是不知道为什么for循环赋值的那种没有修改内存很明显

from judger.

virusdefender avatar virusdefender commented on August 18, 2024

我又看了下你的代码。。

你for循环后面多了一个分号。。

from judger.

airkid avatar airkid commented on August 18, 2024

哦哦那个是我跑耗时的……不管怎么说真的非常感谢!

from judger.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.